Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/jesse_schema_validator.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
-define(not_one_schema_valid, 'not_one_schema_valid').
-define(not_schema_valid, 'not_schema_valid').
-define(wrong_not_schema, 'wrong_not_schema').
-define(external_error, 'external_error').
-define(external, 'external').

%%
-define(not_found, not_found).
Expand Down
13 changes: 13 additions & 0 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
%% API
-export([ add_to_path/2
, get_allowed_errors/1
, get_external_validator/1
, get_current_path/1
, get_current_schema/1
, get_current_schema_id/1
Expand Down Expand Up @@ -68,10 +69,14 @@
jesse:json_term() |
?not_found
)
, external_validator :: external_validator()
, id :: http_uri:uri() | 'undefined'
}
).

-type external_validator() :: fun((jesse:json_term(), state()) -> state())
| undefined.

-opaque state() :: #state{}.

%%% API
Expand Down Expand Up @@ -148,13 +153,17 @@ new(JsonSchema, Options) ->
, Options
, ?default_schema_loader_fun
),
ExternalValidator = proplists:get_value( external_validator
, Options
),
NewState = #state{ root_schema = JsonSchema
, current_path = []
, allowed_errors = AllowedErrors
, error_list = []
, error_handler = ErrorHandler
, default_schema_ver = DefaultSchemaVer
, schema_loader_fun = LoaderFun
, external_validator = ExternalValidator
},
set_current_schema(NewState, JsonSchema).

Expand Down Expand Up @@ -385,3 +394,7 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) ->
%% io:format("load_schema: ~p\n", [{_C, _E, erlang:get_stacktrace()}]),
?not_found
end.

%% @private
get_external_validator(#state{external_validator = Fun}) ->
Fun.
16 changes: 13 additions & 3 deletions src/jesse_validator_draft3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
| ?not_in_range
| ?wrong_length
| ?wrong_size
| ?wrong_type.
| ?wrong_type
| ?external.

-type data_error_type() :: data_error()
| {data_error(), binary()}.
Expand Down Expand Up @@ -209,11 +210,11 @@ check_value(Value, [{?DISALLOW, Disallow} | Attrs], State) ->
check_value(Value, [{?EXTENDS, Extends} | Attrs], State) ->
NewState = check_extends(Value, Extends, State),
check_value(Value, Attrs, NewState);
check_value(_Value, [], State) ->
State;
check_value(Value, [{?REF, RefSchemaURI} | Attrs], State) ->
NewState = validate_ref(Value, RefSchemaURI, State),
check_value(Value, Attrs, NewState);
check_value(Value, [], State) ->
maybe_external_check_value(Value, State);
check_value(Value, [_Attr | Attrs], State) ->
check_value(Value, Attrs, State).

Expand Down Expand Up @@ -1031,3 +1032,12 @@ add_to_path(State, Property) ->
%% @private
remove_last_from_path(State) ->
jesse_state:remove_last_from_path(State).

%% @private
maybe_external_check_value(Value, State) ->
case jesse_state:get_external_validator(State) of
undefined ->
State;
Fun ->
Fun(Value, State)
end.
15 changes: 12 additions & 3 deletions src/jesse_validator_draft4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
| ?too_many_properties
| ?wrong_length
| ?wrong_size
| ?wrong_type.
| ?wrong_type
| ?external.

-type data_error_type() :: data_error()
| {data_error(), binary()}.
Expand Down Expand Up @@ -246,11 +247,11 @@ check_value(Value, [{?ONEOF, Schemas} | Attrs], State) ->
check_value(Value, [{?NOT, Schema} | Attrs], State) ->
NewState = check_not(Value, Schema, State),
check_value(Value, Attrs, NewState);
check_value(_Value, [], State) ->
State;
check_value(Value, [{?REF, RefSchemaURI} | Attrs], State) ->
NewState = validate_ref(Value, RefSchemaURI, State),
check_value(Value, Attrs, NewState);
check_value(Value, [], State) ->
maybe_external_check_value(Value, State);
check_value(Value, [_Attr | Attrs], State) ->
check_value(Value, Attrs, State).

Expand Down Expand Up @@ -1362,3 +1363,11 @@ valid_datetime(DateTimeBin) ->
_ ->
false
end.

maybe_external_check_value(Value, State) ->
case jesse_state:get_external_validator(State) of
undefined ->
State;
Fun ->
Fun(Value, State)
end.