Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add external format validators option #85

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/jesse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
, error_handler/0
, error_list/0
, external_validator/0
, external_format_validator/0
, external_format_validators_map/0
, json_term/0
, schema/0
, schema_id/0
Expand Down Expand Up @@ -71,6 +73,9 @@
-type external_validator() :: fun((json_term(), any()) -> any())
| undefined.

-type external_format_validator() :: fun((json_term()) -> ok | error).
-type external_format_validators_map() :: #{binary() => external_format_validator()}.

%% From https://github.com/erlang/otp/blob/OTP-20.2.3/lib/inets/doc/src/http_uri.xml#L57
-type http_uri_uri() :: string() | unicode:unicode_binary().

Expand All @@ -95,6 +100,7 @@
| {default_schema_ver, schema_ver()}
| {error_handler, error_handler()}
| {external_validator, external_validator()}
| {external_format_validators, external_format_validators_map()}
| {meta_schema_ver, schema_ver()}
| {parser_fun, parser_fun()}
| {schema_loader_fun, schema_loader_fun()}.
Expand Down
11 changes: 11 additions & 0 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
-export([ add_to_path/2
, get_allowed_errors/1
, get_external_validator/1
, get_external_format_validator/2
, get_current_path/1
, get_current_schema/1
, get_current_schema_id/1
Expand Down Expand Up @@ -59,6 +60,7 @@
, error_handler :: jesse:error_handler()
, error_list :: jesse:error_list()
, external_validator :: jesse:external_validator()
, external_format_validators :: jesse:external_format_validators_map()
, id :: jesse:schema_id()
, root_schema :: jesse:schema()
, schema_loader_fun :: jesse:schema_loader_fun()
Expand Down Expand Up @@ -142,6 +144,10 @@ new(JsonSchema, Options) ->
ExternalValidator = proplists:get_value( external_validator
, Options
),
ExternalFormatValidators = proplists:get_value( external_format_validators
, Options
, #{}
),
LoaderFun = proplists:get_value( schema_loader_fun
, Options
, ?default_schema_loader_fun
Expand All @@ -154,6 +160,7 @@ new(JsonSchema, Options) ->
, default_schema_ver = DefaultSchemaVer
, schema_loader_fun = LoaderFun
, external_validator = ExternalValidator
, external_format_validators = ExternalFormatValidators
},
set_current_schema(NewState, JsonSchema).

Expand Down Expand Up @@ -393,3 +400,7 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->
%% @private
get_external_validator(#state{external_validator = Fun}) ->
Fun.

-spec get_external_format_validator(binary(), state()) -> jesse:external_format_validator() | undefined.
get_external_format_validator(Format, #state{external_format_validators = Validators}) ->
maps:get(Format, Validators, undefined).
15 changes: 13 additions & 2 deletions src/jesse_validator_draft3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,8 @@ check_enum(Value, Enum, State) ->
handle_data_invalid(?not_in_enum, Value, State)
end.

check_format(_Value, _Format, State) ->
State.
check_format(Value, Format, State) ->
maybe_external_check_format(Value, Format, State).

%% @doc 5.24. divisibleBy
%%
Expand Down Expand Up @@ -1051,3 +1051,14 @@ maybe_external_check_value(Value, State) ->
Fun ->
Fun(Value, State)
end.

maybe_external_check_format(Value, Format, State) ->
case jesse_state:get_external_format_validator(Format, State) of
undefined -> State;
Fun when is_function(Fun, 1) ->
abogosyan marked this conversation as resolved.
Show resolved Hide resolved
case Fun(Value) of
ok -> State;
error ->
handle_data_invalid(?wrong_format, Value, State)
abogosyan marked this conversation as resolved.
Show resolved Hide resolved
end
end.
15 changes: 13 additions & 2 deletions src/jesse_validator_draft4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -980,8 +980,8 @@ check_format(Value, _Format = <<"ipv6">>, State) when is_binary(Value) ->
check_format(Value, _Format = <<"uri">>, State) when is_binary(Value) ->
%% not yet supported
State;
check_format(_Value, _Format, State) ->
State.
check_format(Value, Format, State) ->
maybe_external_check_format(Value, Format, State).

%% @doc 5.1.1. multipleOf
%%
Expand Down Expand Up @@ -1379,3 +1379,14 @@ maybe_external_check_value(Value, State) ->
Fun ->
Fun(Value, State)
end.

maybe_external_check_format(Value, Format, State) ->
abogosyan marked this conversation as resolved.
Show resolved Hide resolved
case jesse_state:get_external_format_validator(Format, State) of
undefined -> State;
Fun when is_function(Fun, 1) ->
case Fun(Value) of
ok -> State;
error ->
handle_data_invalid(?wrong_format, Value, State)
end
end.