From bf8e4dd7664ef4543b31126c08ea13196ff915ba Mon Sep 17 00:00:00 2001 From: Arjan Scherpenisse Date: Fri, 10 Nov 2023 17:44:11 +0100 Subject: [PATCH] Fix dialyzer; only support custom fmt validator as root option --- README.md | 2 ++ lib/ex_json_schema/schema/root.ex | 2 +- lib/ex_json_schema/validator/format.ex | 1 - 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 92bef93..d53358e 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,8 @@ The custom function can also be an anonymous function: ExJsonSchema.Schema.resolve(%{"format" => "custom"}, custom_format_validator: fn format, data -> true end) ``` +> Note that the anonymous function version of the custom validator is only available in the option to Schema.resolve/2 and not as Application config, as using anonymous functions as configuration is not allowed. + [format-spec]: https://json-schema.org/understanding-json-schema/reference/string.html#format ## License diff --git a/lib/ex_json_schema/schema/root.ex b/lib/ex_json_schema/schema/root.ex index bf2cd5f..604dd3b 100644 --- a/lib/ex_json_schema/schema/root.ex +++ b/lib/ex_json_schema/schema/root.ex @@ -12,6 +12,6 @@ defmodule ExJsonSchema.Schema.Root do location: :root | String.t(), definitions: %{String.t() => ExJsonSchema.Schema.resolved()}, version: non_neg_integer | nil, - custom_format_validator: {module(), atom()} | nil + custom_format_validator: {module(), atom()} | (String.t(), any() -> boolean | {:error, any()}) | nil } end diff --git a/lib/ex_json_schema/validator/format.ex b/lib/ex_json_schema/validator/format.ex index 49b8a62..76e5fc4 100644 --- a/lib/ex_json_schema/validator/format.ex +++ b/lib/ex_json_schema/validator/format.ex @@ -99,7 +99,6 @@ defmodule ExJsonSchema.Validator.Format do defp do_validate(%Root{custom_format_validator: nil}, format, data) do case Application.fetch_env(:ex_json_schema, :custom_format_validator) do :error -> [] - {:ok, validator} when is_function(validator, 2) -> validate_with_custom_validator(validator, format, data) {:ok, validator = {_mod, _fun}} -> validate_with_custom_validator(validator, format, data) end end