schematic icon indicating copy to clipboard operation
schematic copied to clipboard

Overriding error messages

Open jfpedroza opened this issue 1 year ago • 3 comments

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.

jfpedroza avatar Jan 22 '25 05:01 jfpedroza

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.

mhanberg avatar Jan 22 '25 11:01 mhanberg

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.

jfpedroza avatar Jan 23 '25 07:01 jfpedroza

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.

mhanberg avatar Jan 23 '25 11:01 mhanberg