Closed
Description
After an upgrade to 1.5.6, we are seeing a change of behaviour walking JSON schema with non-required properties defined with "anyOf" and "oneOf" keywords in the JSON schema. Where the JSON/YAML being validated does not contain the node matching these properties, the nodes are still validated and the validation errors are returned in the validation result.
The code below, will generate the following validation error when ran with "oneOf":
$.prop1: must be valid to one and only one schema, but 2 are valid with indexes '0, 1'
Changing the "oneOf" to "anyOf", returns:
$.prop1: unknown found, string expected
$.prop1: unknown found, integer expected
In both cases, I expected no messages to be returned as the property wasn't required.
Test code:
import com.networknt.schema.InputFormat;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationResult;
public class SchemaTest {
public static void main(String[] args) {
String schemaContents = """
{
"type": "object",
"properties": {
"prop1": {
"oneOf": [
{
"type": "string"
},
{
"type": "integer"
}
]
}
},
"additionalProperties": false
}
""";
String jsonContents = """
{}
""";
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
JsonSchema schema = factory.getSchema(schemaContents);
ValidationResult result = schema.walk(jsonContents, InputFormat.JSON, true);
result.getValidationMessages().forEach(m -> System.out.println(m));
}
}
Metadata
Metadata
Assignees
Labels
No labels