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

2600hz changes #42

Closed
wants to merge 16 commits into from
1 change: 1 addition & 0 deletions src/jesse_schema_validator.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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').
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the sugar-power for this change, and I've been tempted to do similar, but explain to me why wouldn't you first validate with jesse and then validate for more (e.g. realtime semantics), rather than call a function for all nested values (while I assume you're only interested in validate a handful) ?


%%
-define(not_found, not_found).
Expand Down
25 changes: 22 additions & 3 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
%% 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
, get_default_schema_ver/1
, get_error_handler/1
, get_error_list/1
Expand All @@ -48,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 @@ -67,6 +70,7 @@
jesse:json_term() |
?not_found
)
, extra_validator :: extra_validator()
, id :: binary() | 'undefined'
}
).
Expand Down Expand Up @@ -96,6 +100,14 @@ get_current_path(#state{current_path = CurrentPath}) ->
get_current_schema(#state{current_schema = CurrentSchema}) ->
CurrentSchema.

%% @doc Getter for `current_schema_id'.
-spec get_current_schema_id(State :: state()) -> binary() | undefined.
get_current_schema_id(#state{current_schema = CurrentSchema
,root_schema = RootSchema
}) ->
Default = jesse_json_path:value(?ID, RootSchema, ?not_found),
jesse_json_path:value(?ID, CurrentSchema, Default).

%% @doc Getter for `default_schema_ver'.
-spec get_default_schema_ver(State :: state()) -> binary().
get_default_schema_ver(#state{default_schema_ver = SchemaVer}) ->
Expand Down Expand Up @@ -139,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 @@ -183,7 +199,7 @@ resolve_ref(State, Reference) ->
Path = jesse_json_path:parse(Pointer),
case load_local_schema(State#state.root_schema, Path) of
?not_found ->
jesse_error:handle_schema_invalid(?schema_invalid, State);
jesse_error:handle_schema_invalid(Reference, State);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the $ref already part of the error?
I've cherry-picked your change, and instead output the canonical reference (the output from combine_id). That is slightly more valuable i.e. see how the $ref was expanding, and see what jesse tried to find and load.

Schema ->
set_current_schema(State, Schema)
end;
Expand All @@ -194,7 +210,7 @@ resolve_ref(State, Reference) ->
),
case load_schema(State, BaseURI) of
?not_found ->
jesse_error:handle_schema_invalid(?schema_invalid, State);
jesse_error:handle_schema_invalid(Reference, State);
RemoteSchema ->
SchemaVer =
jesse_json_path:value(?SCHEMA, RemoteSchema, ?default_schema_ver),
Expand All @@ -205,7 +221,7 @@ resolve_ref(State, Reference) ->
Path = jesse_json_path:parse(Pointer),
case load_local_schema(RemoteSchema, Path) of
?not_found ->
jesse_error:handle_schema_invalid(?schema_invalid, State);
jesse_error:handle_schema_invalid(Reference, State);
Schema ->
set_current_schema(NewState, Schema)
end
Expand All @@ -217,6 +233,7 @@ resolve_ref(State, Reference) ->
undo_resolve_ref(RefState, OriginalState) ->
RefState#state{ root_schema = OriginalState#state.root_schema
, current_schema = OriginalState#state.current_schema
, default_schema_ver = OriginalState#state.default_schema_ver
, id = OriginalState#state.id
}.

Expand Down Expand Up @@ -367,3 +384,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.
28 changes: 20 additions & 8 deletions src/jesse_validator_draft3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,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 @@ -208,13 +209,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, [{?REF, RefSchemaURI}], State) ->
{NewState0, Schema} = resolve_ref(RefSchemaURI, State),
NewState =
jesse_schema_validator:validate_with_state(Schema, Value, NewState0),
undo_resolve_ref(NewState, State);
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 @@ -898,6 +897,12 @@ check_extends_array(Value, Extends, State) ->
, Extends
).

%% @private
validate_ref(Value, Reference, State) ->
{NewState, Schema} = resolve_ref(Reference, State),
ResultState = jesse_schema_validator:validate_with_state(Schema, Value, NewState),
undo_resolve_ref(ResultState, State).

%% @private
resolve_ref(Reference, State) ->
NewState = jesse_state:resolve_ref(State, Reference),
Expand Down Expand Up @@ -1014,3 +1019,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.
29 changes: 20 additions & 9 deletions src/jesse_validator_draft4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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 @@ -245,15 +246,13 @@ 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, [{?REF, RefSchemaURI}], State) ->
{NewState0, Schema} = resolve_ref(RefSchemaURI, State),
NewState =
jesse_schema_validator:validate_with_state(Schema, Value, NewState0),
undo_resolve_ref(NewState, State);
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).
check_value(Value, Attrs, State).

%%% Internal functions
%% @doc Adds Property to the current path and checks the value
Expand Down Expand Up @@ -1220,6 +1219,12 @@ validate_schema(Value, Schema, State0) ->
throw:Errors -> {false, Errors}
end.

%% @private
validate_ref(Value, Reference, State) ->
{NewState, Schema} = resolve_ref(Reference, State),
ResultState = jesse_schema_validator:validate_with_state(Schema, Value, NewState),
undo_resolve_ref(ResultState, State).

%% @doc Resolve a JSON reference
%% The "id" keyword is taken care of behind the scenes in jesse_state.
%% @private
Expand Down Expand Up @@ -1347,3 +1352,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.