Exclude earlier errors when checking oneOf/anyOf with allowed_errors. Fixes #80.

This commit is contained in:
Sergey Prokhorov 2019-07-05 16:33:47 +02:00
parent 4ca7e9b279
commit f476bff21a
4 changed files with 70 additions and 5 deletions

View File

@ -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"

View File

@ -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)
@ -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, _} ->

View File

@ -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"]
}
]
}
]

View File

@ -166,6 +166,9 @@ anyOfOneOfAllowedErrorsOneExtra(Config) ->
anyOfOneOfAllowedErrorsInfinityExtra(Config) ->
do_test("anyOfOneOfAllowedErrorsInfinityExtra", Config).
anyOfOneOfAllowedErrorsInfinityPrevError(Config) ->
do_test("anyOfOneOfAllowedErrorsInfinityPrevError", Config).
unicodePatternProperties(Config) ->
do_test("unicodePatternProperties", Config).