Skip to content

Validation errors returned while walking anyOf and oneOf when node == null #1167

Closed
@chrishodgins

Description

@chrishodgins

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions