3
votes

I'm writing a custom Ecto.Type for colors (serialized and parsed as CSS colors).

The behaviour specifies that the cast/1 callback should return :error unless successful. It does not allow for returning an error reason, and will generate the generic "is invalid" error description when used with a Changeset.

What is the best way to allow better error messages on in changesets on invalid (uncasteable) input? I would like to create a validate_color function to be used on changesets, but if the cast failed the field will already have the generic error.

1

1 Answers

2
votes

This is possible as of Ecto v3.0.0:

When returning {:error, keyword()}, the returned keyword list will be preserved in the changeset errors, similar to Changeset.add_error/4. Passing a :message key, will override the default message.

link to docs