Skip to content

Add ValidationError formatter#44

Open
NickNeck wants to merge 6 commits intomainfrom
inspect-opts
Open

Add ValidationError formatter#44
NickNeck wants to merge 6 commits intomainfrom
inspect-opts

Conversation

@NickNeck
Copy link
Copy Markdown
Member

This PR adds functionalities to customise the error messages for JsonXema.ValidationError.

JsonXema gets a new behaviour called JsonXema.ValidationError.Formatter and the def0ault formatter JsonXema.ValidationError.DefaultFormatter. This isolates the formatting to one module and the formatter module can be configured (e.g. config :json_xema, ValidationError, formatter: MyValidationErrorFormatter).

The JsonXema.ValidationError.DefaultFormatter excepts now options that are passed to the internal inspect(value, opts) calls. With this change all Inspect.Opts can be used with JsonXeam.ValidationError.format_error/2 (e.g. JsonXema.ValidationError.format_error(error, inspect_fun: my_inspect_fun)).

Examples:

iex(1)> schema = ~s|{"type": "integer"}| |> Jason.decode!() |> JsonXema.new()
%JsonXema{schema: %Xema.Schema{type: :integer}, refs: %{}}
iex(2)> error = JsonXema.validate(schema, %{one: 1})
{:error,
 %JsonXema.ValidationError{
   message: nil,
   reason: %{type: "integer", value: %{one: 1}}
 }}
iex(3)> JsonXema.ValidationError.format_error(error)
"Expected \"integer\", got %{one: 1}."
iex(4)> inspect_fun = fn value, _opts -> Jason.encode!(value) end
#Function<41.105768164/2 in :erl_eval.expr/6>
iex(5)> JsonXema.ValidationError.format_error(error, inspect_fun: inspect_fun)
"Expected \"integer\", got {\"one\":1}."
iex(6)> Application.put_env(:json_xema, ValidationError, inspect_fun: inspect_fun)
:ok
iex(7)> JsonXema.ValidationError.format_error(error)
"Expected \"integer\", got {\"one\":1}."
iex(1)> defmodule MyValidationErrorFormatter do
...(1)>   alias JsonXema.ValidationError
...(1)>
...(1)>   @behaviour JsonXema.ValidationError.Formatter
...(1)>
...(1)>   @impl true
...(1)>   def format(error, _opts) do
...(1)>     error
...(1)>     |> ValidationError.travers_errors([], &format_error/3)
...(1)>     |> Enum.reverse()
...(1)>     |> Enum.join("\n")
...(1)>   end
...(1)>
...(1)>   def format_error(error, path, acc) do
...(1)>     [inspect([error: error.reason, path: path], pretty: true) | acc]
...(1)>   end
...(1)> end
{:module, MyValidationErrorFormatter,
 <<70, 79, 82, 49, 0, 0, 8, 244, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 78,
   0, 0, 0, 31, 33, 69, 108, 105, 120, 105, 114, 46, 77, 121, 86, 97, 108, 105,
   100, 97, 116, 105, 111, 110, 69, 114, 114, ...>>, {:format_error, 3}}
iex(2)> Application.put_env(:json_xema, ValidationError,
...(2)>   formatter: MyValidationErrorFormatter)
:ok
iex(3)> schema = ~s|{"type": "integer"}| |> Jason.decode!() |> JsonXema.new()
%JsonXema{schema: %Xema.Schema{type: :integer}, refs: %{}}
iex(4)> schema |> JsonXema.validate("one") |> elem(1) |> Exception.message() |> IO.puts()
[error: %{type: "integer", value: "one"}, path: []]
:ok

@coveralls
Copy link
Copy Markdown

coveralls commented Mar 25, 2024

Coverage Status

coverage: 98.864% (+0.05%) from 98.81%
when pulling df9310b on inspect-opts
into 3288577 on main.

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

Successfully merging this pull request may close these issues.

2 participants