Skip to content

Commit

Permalink
allow extra validator
Browse files Browse the repository at this point in the history
this will extra validations for schema elements
example : lookup some value in a database an provide an error
  • Loading branch information
lazedo authored and andreineculau committed May 20, 2017
1 parent 37eb210 commit d71b0a2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
9 changes: 9 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_extra_validator/1
, get_current_path/1
, get_current_schema/1
, get_current_schema_id/1
Expand All @@ -49,6 +50,7 @@
%% Includes
-include("jesse_schema_validator.hrl").

-type extra_validator() :: fun((jesse:json_term(), state()) -> state()) | undefined.
%% Internal datastructures
-record( state
, { root_schema :: jesse:json_term()
Expand All @@ -68,6 +70,7 @@
jesse:json_term() |
?not_found
)
, extra_validator :: extra_validator()
, id :: http_uri:uri() | 'undefined'
}
).
Expand Down Expand Up @@ -148,13 +151,17 @@ new(JsonSchema, Options) ->
, Options
, ?default_schema_loader_fun
),
ExtraValidator = proplists:get_value( extra_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
, extra_validator = ExtraValidator
},
set_current_schema(NewState, JsonSchema).

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

get_extra_validator(#state{extra_validator=Fun}) -> Fun.
14 changes: 11 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_error.

-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) ->
check_external_validation(Value, State);
check_value(Value, [_Attr | Attrs], State) ->
check_value(Value, Attrs, State).

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

%% @private
check_external_validation(Value, State) ->
case jesse_state:get_extra_validator(State) of
undefined -> State;
Fun -> Fun(Value, State)
end.
13 changes: 10 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_error.

-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) ->
check_external_validation(Value, State);
check_value(Value, [_Attr | Attrs], State) ->
check_value(Value, Attrs, State).

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

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

0 comments on commit d71b0a2

Please sign in to comment.