failFast fails too early in case of AnyOf validator
I have found the problem in failFast implementation. Consider this small change in the product schema
{
"type": "object",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "A product from Acme's catalog",
"title": "Product",
"properties": {
"id": {
"type": "integer",
"description": "The unique identifier for a product"
},
"name": {
"type": "string",
"maxLength": 200,
"minLength": 10,
"description": "Name of the product"
},
"price": {
"anyOf": [
{
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
{
"type": "string",
"minLength": 0
}
]
}
},
"required": [
"id",
"name",
"price"
]
}
Now if the json object is like this,
{
"id": 1,
"name": "A valid product name",
"price": "10.99"
}
Now if failFast is set true, this schema will not validate. The reason behind this is that AnyOf collect errors in a loop, and there it calls buildValidationMessage which throws exception at once since failFast is set to true. However AnyOf validator is not finished yet, i.e. here price is not number but its a valid string, but we never let AnyOf validator to complete its validation completely and give its verdict. Therefore, we need not to call buildValidationMessage.
Instead, AnyOf should throw JsonSchemaException after it is finished with its loop.
Or @stevehu you can guide me how should AnyOf should react in a more suitable way.
@khiftikhar AllOf, AnyOf, and OneOf work in a similar way with a context passed to the subschema. There are other behavior changes with other validators and that is why we have introduced the context. Take a look if we can figure out if the parent is AnyOf when deciding if throw the exception. @ddobrin As you are working on these aggregated validators, do you have any recommendation?
I am out of the office until Wednesday and can look into it afterwards.
The delta between OpenAPI and JSON schemas will have to be bridged via the context, I would assume
if/then/else- condition also doesn´t work with failFast. When if-condition is not valid it fails before checking then-condition.
@TDog92 Thanks for capturing it. We definitely need to revisit the fail-fast implementation.