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
16 changes: 13 additions & 3 deletions src/jesse_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
, get_allowed_errors/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 Down Expand Up @@ -96,6 +97,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 @@ -183,7 +192,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 +203,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 +214,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 +226,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
14 changes: 9 additions & 5 deletions src/jesse_validator_draft3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,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, [_Attr | Attrs], State) ->
check_value(Value, Attrs, State).

Expand Down Expand Up @@ -898,6 +896,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
14 changes: 9 additions & 5 deletions src/jesse_validator_draft4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,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, [{?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, [_Attr | Attrs], State) ->
check_value(Value, Attrs, State).

Expand Down Expand Up @@ -1220,6 +1218,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