Skip to content

Commit

Permalink
whitespace, style
Browse files Browse the repository at this point in the history
  • Loading branch information
andreineculau committed May 20, 2017
1 parent c4a5ebf commit 37eb210
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 83 deletions.
22 changes: 11 additions & 11 deletions src/jesse_validator_draft3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -900,25 +900,25 @@ check_extends_array(Value, Extends, State) ->
%% @private
validate_ref(Value, Reference, State) ->
case resolve_ref(Reference, State) of
{error, NewState} ->
undo_resolve_ref(NewState, State);
{ok, NewState, Schema} ->
ResultState = jesse_schema_validator:validate_with_state(Schema, Value, NewState),
undo_resolve_ref(ResultState, State)
end.
{error, NewState} ->
undo_resolve_ref(NewState, State);
{ok, NewState, Schema} ->
ResultState = jesse_schema_validator:validate_with_state(Schema, Value, NewState),
undo_resolve_ref(ResultState, State)
end.

%% @doc Resolve a JSON reference
%% The "id" keyword is taken care of behind the scenes in jesse_state.
%% @private
resolve_ref(Reference, State) ->
CurrentErrors = jesse_state:get_error_list(State),
NewState = jesse_state:resolve_ref(State, Reference),
NewErrors = jesse_state:get_error_list(NewState),
NewErrors = jesse_state:get_error_list(NewState),
case length(CurrentErrors) =:= length(NewErrors) of
true ->
Schema = get_current_schema(NewState),
{ok, NewState, Schema};
false -> {error, NewState}
true ->
Schema = get_current_schema(NewState),
{ok, NewState, Schema};
false -> {error, NewState}
end.

undo_resolve_ref(State, OriginalState) ->
Expand Down
22 changes: 11 additions & 11 deletions src/jesse_validator_draft4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1222,25 +1222,25 @@ validate_schema(Value, Schema, State0) ->
%% @private
validate_ref(Value, Reference, State) ->
case resolve_ref(Reference, State) of
{error, NewState} ->
undo_resolve_ref(NewState, State);
{ok, NewState, Schema} ->
ResultState = jesse_schema_validator:validate_with_state(Schema, Value, NewState),
undo_resolve_ref(ResultState, State)
end.
{error, NewState} ->
undo_resolve_ref(NewState, State);
{ok, NewState, Schema} ->
ResultState = jesse_schema_validator:validate_with_state(Schema, Value, NewState),
undo_resolve_ref(ResultState, State)
end.

%% @doc Resolve a JSON reference
%% The "id" keyword is taken care of behind the scenes in jesse_state.
%% @private
resolve_ref(Reference, State) ->
CurrentErrors = jesse_state:get_error_list(State),
NewState = jesse_state:resolve_ref(State, Reference),
NewErrors = jesse_state:get_error_list(NewState),
NewErrors = jesse_state:get_error_list(NewState),
case length(CurrentErrors) =:= length(NewErrors) of
true ->
Schema = get_current_schema(NewState),
{ok, NewState, Schema};
false -> {error, NewState}
true ->
Schema = get_current_schema(NewState),
{ok, NewState, Schema};
false -> {error, NewState}
end.

undo_resolve_ref(State, OriginalState) ->
Expand Down
142 changes: 81 additions & 61 deletions test/jesse_schema_validator_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -224,86 +224,106 @@ schema_unsupported_test() ->
-ifndef(erlang_deprecated_types).
-ifndef(COMMON_TEST). % see Emakefile
map_schema_test() ->
[map_schema_test_draft(URI)
|| URI <- [<<"http://json-schema.org/draft-03/schema#">>,
<<"http://json-schema.org/draft-04/schema#">>]].
[ map_schema_test_draft(URI)
|| URI <- [ <<"http://json-schema.org/draft-03/schema#">>
, <<"http://json-schema.org/draft-04/schema#">>
]].

map_schema_test_draft(URI) ->
Schema = #{<<"$schema">> => URI,
<<"type">> => <<"object">>,
<<"properties">> =>
#{
<<"foo">> =>
#{<<"type">> => <<"object">>,
<<"properties">> =>
#{<<"subfoo">> => #{<<"type">> => <<"integer">>}}
}
},
<<"patternProperties">> =>
#{<<"^b">> => #{<<"type">> => <<"integer">>}}
Schema = #{ <<"$schema">> => URI
, <<"type">> => <<"object">>
, <<"properties">> =>
#{ <<"foo">> =>
#{ <<"type">> => <<"object">>
, <<"properties">> =>
#{ <<"subfoo">> => #{ <<"type">> => <<"integer">>
}
}
}
}
, <<"patternProperties">> =>
#{ <<"^b">> => #{ <<"type">> => <<"integer">>
}
}
},
ValidJson = {[ {<<"foo">>, {[ {<<"subfoo">>, 42} ]}},
{<<"bar">>, 42},
{<<"baz">>, 42}
]},
ValidJson = {[ {<<"foo">>, {[ {<<"subfoo">>, 42} ]}}
, {<<"bar">>, 42}
, {<<"baz">>, 42}
]},
?assertEqual({ok, ValidJson} ,
jesse_schema_validator:validate(Schema, ValidJson, [])),

InvalidJson = {[ {<<"bar">>, <<"str expect int">>} ]},
?assertThrow([{data_invalid,
#{<<"type">> := <<"integer">>},
wrong_type,
<<"str expect int">>,
[<<"bar">>]}],
?assertThrow([{ data_invalid
, #{<<"type">> := <<"integer">>}
, wrong_type
, <<"str expect int">>
, [<<"bar">>]
}],
jesse_schema_validator:validate(Schema, InvalidJson, [])).


map_data_test() ->
[map_data_test_draft(URI)
|| URI <- [<<"http://json-schema.org/draft-03/schema#">>,
<<"http://json-schema.org/draft-04/schema#">>]].
[ map_data_test_draft(URI)
|| URI <- [ <<"http://json-schema.org/draft-03/schema#">>
, <<"http://json-schema.org/draft-04/schema#">>
]
].


map_data_test_draft(URI) ->
Schema = {[ {<<"$schema">>, URI},
{<<"type">>, <<"object">>},
{<<"properties">>,
{[
{<<"foo">>,
{[ {<<"type">>, <<"object">>},
{<<"properties">>,
{[ {<<"subfoo">>, {[ {<<"type">>, <<"integer">>} ]}} ]}
}
]}
}
]}
},
{<<"patternProperties">>,
{[ {<<"^b">>, {[ {<<"type">>, <<"integer">>} ]}} ]}}
Schema = {[ {<<"$schema">>, URI}
, {<<"type">>, <<"object">>}
, {<<"properties">>
, {[{ <<"foo">>
, {[ {<<"type">>, <<"object">>}
, { <<"properties">>
, {[{ <<"subfoo">>
, {[{ <<"type">>
, <<"integer">>
}]}
}]}
}
]}
}
]}
}
, { <<"patternProperties">>
, {[{ <<"^b">>
, {[{<<"type">>, <<"integer">>}]}
}]}}
]},
ValidJson = #{<<"foo">> => #{<<"subfoo">> => 42},
<<"bar">> => 42,
<<"baz">> => 42
},
?assertEqual({ok, ValidJson} ,
jesse_schema_validator:validate(Schema, ValidJson, [])),
ValidJson = #{ <<"foo">> => #{<<"subfoo">> => 42}
, <<"bar">> => 42
, <<"baz">> => 42
},
?assertEqual( {ok, ValidJson}
, jesse_schema_validator:validate(Schema, ValidJson, [])
),

InvalidJson = #{<<"foo">> => 42,
<<"baz">> => #{}},
InvalidJson = #{ <<"foo">> => 42
, <<"baz">> => #{}
},
%% XXX: order of errors isn't guaranteed
%% In case of future fails it can be replaced with manual catching and sorting
%% of throwed error list, then checked using ?assertMatch
?assertThrow([{data_invalid,
{[ {<<"type">>, <<"object">>} | _ ]},
wrong_type, 42,
[<<"foo">>]}
,{data_invalid,
{[ {<<"type">>, <<"integer">>} ]},
wrong_type, #{},
[<<"baz">>]}
],
jesse_schema_validator:validate(Schema, InvalidJson,
[{allowed_errors, infinity}])).
?assertThrow([ { data_invalid
, {[{<<"type">>, <<"object">>} | _ ]}
, wrong_type
, 42
, [<<"foo">>]
}
, { data_invalid
, {[{<<"type">>, <<"integer">>}]}
, wrong_type
, #{}
, [<<"baz">>]
}
],
jesse_schema_validator:validate( Schema
, InvalidJson
, [{allowed_errors, infinity}]
)).

-endif.
-endif.

0 comments on commit 37eb210

Please sign in to comment.