Skip to content

Commit

Permalink
Merge pull request #81 from seriyps/oneof-anyof-allowed_errors-prev_e…
Browse files Browse the repository at this point in the history
…rror

Exclude earlier errors when checking oneOf/anyOf with allowed_errors.…
  • Loading branch information
seriyps authored Aug 14, 2019
2 parents 4ca7e9b + f476bff commit 512f206
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/jesse_tests_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ test_schema(DefaultSchema, Opts0, Schema, SchemaTests) ->
true ->
{ok, Instance} = Result;
false ->
{error, _} = Result
{error, _} = Result;
ExpectedErrors ->
{error, Errors} = Result,
GotErrors =
[atom_to_binary(E, utf8)
|| {data_invalid, _, E, _, _} <- Errors],
(ExpectedErrors == GotErrors)
orelse error({unexpected_error, GotErrors})
end
catch ?EXCEPTION(C,R,Stacktrace)
ct:pal( "Error: ~p:~p~n"
Expand Down
10 changes: 6 additions & 4 deletions src/jesse_validator_draft4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1139,10 +1139,11 @@ check_any_of(_Value, _InvalidSchemas, State) ->
check_any_of_(Value, [], State) ->
handle_data_invalid(?any_schemas_not_valid, Value, State);
check_any_of_(Value, [Schema | Schemas], State) ->
NumErrsBefore = length(jesse_state:get_error_list(State)),
case validate_schema(Value, Schema, State) of
{true, NewState} ->
case jesse_state:get_error_list(NewState) of
[] -> NewState;
case length(jesse_state:get_error_list(NewState)) of
NumErrsBefore -> NewState;
_ -> check_any_of_(Value, Schemas, State)
end;
{false, _} -> check_any_of_(Value, Schemas, State)
Expand Down Expand Up @@ -1176,10 +1177,11 @@ check_one_of_(Value, [], State, 0) ->
check_one_of_(Value, _Schemas, State, Valid) when Valid > 1 ->
handle_data_invalid(?not_one_schema_valid, Value, State);
check_one_of_(Value, [Schema | Schemas], State, Valid) ->
NumErrsBefore = length(jesse_state:get_error_list(State)),
case validate_schema(Value, Schema, State) of
{true, NewState} ->
case jesse_state:get_error_list(NewState) of
[] -> check_one_of_(Value, Schemas, NewState, Valid + 1);
case length(jesse_state:get_error_list(NewState)) of
NumErrsBefore -> check_one_of_(Value, Schemas, NewState, Valid + 1);
_ -> check_one_of_(Value, Schemas, State, Valid)
end;
{false, _} ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[
{
"description": "anyOf/oneOf with allowed_errors",
"options": {
"allowed_errors": "infinity"
},
"schema": {
"$schema": "http://json-schema.org/draft-04/schema#",
"additionalProperties": false,
"properties": {
"known_field2": {
"properties": {
"some_ip": {
"oneOf": [
{ "format": "ipv4" },
{ "format": "ipv6" }
]
}
}
},
"known_field1": {
"properties": {
"some_id": {
"anyOf": [
{
"pattern": "^[0-9]{6}[+-]?[0-9]{3}[0-9A-Z]{1}$"
},
{
"pattern": "^[8-9][0-9]{8}$"
}
]
}
}
}
}
},
"tests": [
{
"description": "anyOf: only additionalProperties error",
"data": {
"unknown_field1":true,
"known_field1":{
"some_id":"410391-8351"
},
"known_field2":{
"some_ip":"127.0.0.1"
}
},
"valid": ["no_extra_properties_allowed"]
}
]
}
]
3 changes: 3 additions & 0 deletions test/jesse_tests_draft4_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ anyOfOneOfAllowedErrorsOneExtra(Config) ->
anyOfOneOfAllowedErrorsInfinityExtra(Config) ->
do_test("anyOfOneOfAllowedErrorsInfinityExtra", Config).

anyOfOneOfAllowedErrorsInfinityPrevError(Config) ->
do_test("anyOfOneOfAllowedErrorsInfinityPrevError", Config).

unicodePatternProperties(Config) ->
do_test("unicodePatternProperties", Config).

Expand Down

0 comments on commit 512f206

Please sign in to comment.