open-api-spex / open_api_spex

Open API Specifications for Elixir Plug applications
Mozilla Public License 2.0
681 stars 177 forks source link

Custom error messages #609

Open GregorGrasselli opened 1 month ago

GregorGrasselli commented 1 month ago

It is great that it is possible to create custom validation using the "x-validate" vendor extension. But having created a custom validator, I would like to be able to also have a custom error message, so the user will have an easier time of understanding how to fix it.

From a quick check I think this could be easily done by adding an additional reason option to OpenApiSpex.Cast.Error.reason. Maybe :custom, and then adding a function head for creating messages with a %{reason: :custom, meta: %{message: message}} error, that just returns the message.

Do you think it makes sense to add something like this, or am I missing something?

P.S.: I am happy to make the PR if adding this makes sense.

mbuhot commented 2 days ago

Hi @GregorGrasselli yes a PR for custom error messages on custom validators sounds good to me 👍