Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding error messages #36

Open
jfpedroza opened this issue Jan 22, 2025 · 3 comments
Open

Overriding error messages #36

jfpedroza opened this issue Jan 22, 2025 · 3 comments

Comments

@jfpedroza
Copy link

Hi. Enjoying the library so far.

I find that changing the error message of a schematic is quite cumbersome. It would be nice to have an easy to set the error message in a schematic. In some cases, the default error message is too long or too generic.

For example, oneof/1 with two schema schematics provides an error with the full signature of both modules.

I tried doing %Schematic{oneof([schematic1(), schematic2()]) | message: fn -> ... end}, but that is ignored because the unify function references the default message function.

Another example is that I had to do the following to get the messages to be consistent with the included schematics.

  def decimal do
    schematic =
      raw(
        fn
          int when is_integer(int) ->
            true

          %Decimal{} ->
            true

          string when is_binary(string) ->
            case Decimal.parse(string) do
              {%Decimal{}, ""} -> true
              _ -> false
            end

          _ ->
            false
        end,
        message: "expected a decimal"
      )

    %{schematic | message: fn -> "a decimal" end}
  end

all/1 is another that would benefit from being able to set a single error message regardless of which schematic fails.

@mhanberg
Copy link
Owner

I agree that the error messages become not great with any non-trivial schematic.

I have started working on a new system for errors that should be much more flexible.

I'll share the design I've been looking into later.

@jfpedroza
Copy link
Author

Another odd thing is that raw doesn't allow having different messages based on the input. Instead, I end up using oneof for some custom validations.

@mhanberg
Copy link
Owner

Another odd thing is that raw doesn't allow having different messages based on the input. Instead, I end up using oneof for some custom validations.

That was actually what I was initially looking at, then decided it would just be a bandaid and a more general solution is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants