mirror of
https://github.com/valitydev/jesse.git
synced 2024-11-06 09:35:23 +00:00
refactor #20, add remoteRef fix to draft3 as well
This commit is contained in:
parent
e2404b91f0
commit
a1ab58c42b
@ -38,7 +38,7 @@
|
||||
, set_error_list/2
|
||||
, find_schema/2
|
||||
, resolve_reference/2
|
||||
, leave_reference/2
|
||||
, undo_resolve_reference/2
|
||||
]).
|
||||
|
||||
-export_type([ state/0
|
||||
@ -213,10 +213,13 @@ resolve_reference(State, Reference) ->
|
||||
end
|
||||
end.
|
||||
|
||||
%% @doc Revert changes, made by resolve_reference
|
||||
-spec leave_reference(state(), state()) -> state().
|
||||
leave_reference(RefState, #state{root_schema = RootSchema, id = Id}) ->
|
||||
RefState#state{root_schema = RootSchema, id = Id}.
|
||||
%% @doc Revert changes made by resolve_reference
|
||||
-spec undo_resolve_reference(state(), state()) -> state().
|
||||
undo_resolve_reference(RefState, OriginalState) ->
|
||||
RefState#state{ root_schema = OriginalState#state.root_schema
|
||||
, current_schema = OriginalState#state.current_schema
|
||||
, id = OriginalState#state.id
|
||||
}.
|
||||
|
||||
%% @doc Retrive a specific part of a schema
|
||||
%% @private
|
||||
|
@ -209,8 +209,9 @@ check_value(Value, [{?EXTENDS, Extends} | Attrs], State) ->
|
||||
NewState = check_extends(Value, Extends, State),
|
||||
check_value(Value, Attrs, NewState);
|
||||
check_value(Value, [{?REF, RefSchemaURI} | Attrs], State) ->
|
||||
NewState = check_ref(Value, RefSchemaURI, State),
|
||||
check_value(Value, Attrs, NewState);
|
||||
NewState = resolve_ref(Value, RefSchemaURI, State),
|
||||
NewState2 = check_value(Value, Attrs, NewState),
|
||||
undo_resolve_ref(NewState2, State);
|
||||
check_value(_Value, [], State) ->
|
||||
State;
|
||||
check_value(Value, [_Attr | Attrs], State) ->
|
||||
@ -897,11 +898,14 @@ check_extends_array(Value, Extends, State) ->
|
||||
).
|
||||
|
||||
%% @private
|
||||
check_ref(Value, Reference, State) ->
|
||||
resolve_ref(Value, Reference, State) ->
|
||||
NewState = jesse_state:resolve_reference(State, Reference),
|
||||
Schema = get_current_schema(NewState),
|
||||
jesse_schema_validator:validate_with_state(Schema, Value, NewState).
|
||||
|
||||
undo_resolve_ref(State, OriginalState) ->
|
||||
jesse_state:undo_resolve_reference(State, OriginalState).
|
||||
|
||||
%%=============================================================================
|
||||
%% @doc Returns `true' if given values (instance) are equal, otherwise `false'
|
||||
%% is returned.
|
||||
|
@ -247,8 +247,8 @@ check_value(Value, [{?NOT, Schema} | Attrs], State) ->
|
||||
check_value(Value, Attrs, NewState);
|
||||
check_value(Value, [{?REF, Reference} | Attrs], State) ->
|
||||
NewState = resolve_ref(Value, Reference, State),
|
||||
RefState = check_value(Value, Attrs, NewState),
|
||||
leave_ref(RefState, State);
|
||||
NewState2 = check_value(Value, Attrs, NewState),
|
||||
undo_resolve_ref(NewState2, State);
|
||||
check_value(_Value, [], State) ->
|
||||
State;
|
||||
check_value(Value, [_Attr | Attrs], State) ->
|
||||
@ -1190,11 +1190,8 @@ resolve_ref(Value, Reference, State) ->
|
||||
Schema = get_current_schema(NewState),
|
||||
jesse_schema_validator:validate_with_state(Schema, Value, NewState).
|
||||
|
||||
%% @doc Revert some parts of state when reference goes out of scope
|
||||
leave_ref(RefState, OrigState) ->
|
||||
%% Replace `root_schema` and `id` in RefState from State.
|
||||
jesse_state:leave_reference(RefState, OrigState).
|
||||
|
||||
undo_resolve_ref(State, OriginalState) ->
|
||||
jesse_state:undo_resolve_reference(State, OriginalState).
|
||||
|
||||
%%=============================================================================
|
||||
%% @doc Returns `true' if given values (instance) are equal, otherwise `false'
|
||||
|
63
test/JSON-Schema-Test-Suite-extra/draft4/remoteRefExtra.json
Normal file
63
test/JSON-Schema-Test-Suite-extra/draft4/remoteRefExtra.json
Normal file
@ -0,0 +1,63 @@
|
||||
[
|
||||
{
|
||||
"description": "state corruption $ref in 'items'",
|
||||
"schema": {
|
||||
"properties": {
|
||||
"0_list": {
|
||||
"items": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
|
||||
},
|
||||
"1_any": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "will trigger state corruption before this fix",
|
||||
"data": {"0_list": 1,
|
||||
"1_any": 1},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "state corruption $ref in 'properties'",
|
||||
"schema": {
|
||||
"properties": {
|
||||
"0_object": {
|
||||
"properties": {
|
||||
"0_prop": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
|
||||
}
|
||||
},
|
||||
"1_any": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "will trigger state corruption before this fix",
|
||||
"data": {"0_obj": {"0_prop": 1},
|
||||
"1_any": 1},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"description": "state corruption $ref in 'patternProperties'",
|
||||
"schema": {
|
||||
"properties": {
|
||||
"0_object": {
|
||||
"patternProperties": {
|
||||
"0_prop": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
|
||||
}
|
||||
},
|
||||
"1_any": {"$ref": "http://localhost:1234/subSchemas.json#/integer"}
|
||||
}
|
||||
},
|
||||
"tests": [
|
||||
{
|
||||
"description": "will trigger state corruption before this fix",
|
||||
"data": {"0_obj": {"0_prop": 1},
|
||||
"1_any": 1},
|
||||
"valid": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
@ -137,3 +137,6 @@ uniqueItems(Config) ->
|
||||
|
||||
itemsExtra(Config) ->
|
||||
do_test("itemsExtra", Config).
|
||||
|
||||
remoteRefExtra(Config) ->
|
||||
do_test("remoteRefExtra", Config).
|
||||
|
@ -153,5 +153,5 @@ uniqueItems(Config) ->
|
||||
itemsExtra(Config) ->
|
||||
do_test("itemsExtra", Config).
|
||||
|
||||
remote_ref_regression_case(Config) ->
|
||||
do_test("remoteRefRegression_19", Config).
|
||||
remoteRefExtra(Config) ->
|
||||
do_test("remoteRefExtra", Config).
|
||||
|
Loading…
Reference in New Issue
Block a user