Search code examples
javajsonjsonschemajson-schema-validator

Java - JSON schema validation does NOT fail when array elements do not follow pattern


I am trying to validate JSON strings in Java 17. Currently I am using: com.fasterxml.jackson.core and com.networknt:json-schema-validator

The problem is that when I enter elements in the array that does not follow the pattern in JSON schema, no Error is raised. I dont understand why? If I try validating using https://www.jsonschemavalidator.net/ it fails as it should.

The elements in the array should be 13 numbers. All else should give errors!

JSON input

Only the last element is correct

{
    "mylist": ["123Abc", "123456789012", "1234567890123"]
}

JSON schema

{
    "$id" : "https://schema.mysite.se/myschema",
    "$schema" : "https://json-schema.org/draft/2020-12/schema",
    "type" : "object",
    "properties" : {
        "mylist" : {
            "type" : "array",
            "items" : {
                "type" : "string",
                "pattern" : "^[0-9]{13}$"
            },
            "minItems" : 1,
            "uniqueItems" : true
        }
    }
}

Java validation code

jsonData is the json-input as String, inputSchema is the json-schema as String
The assert should find 2 errors but it find zero.

ObjectMapper mapper = new ObjectMapper();
JsonSchemaFactory schemaFactory = 
JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012);
JsonNode node = mapper.readTree(jsonData);
JsonSchema schema = schemaFactory.getSchema(inputSchema);
Set<ValidationMessage> validationResult = schema.validate(node);

assertEquals(2, validationResult.size());

Is there a problem with com.networknt:json-schema-validator?

UPDATE:

I used version 1.0.76 of json-schema-validator, updating the version to 1.4.0 solved the issue.


Solution

  • Running the following with a recent version of json schema validator.

            String schemaData = "{\r\n"
                    + "    \"$id\" : \"https://schema.mysite.se/myschema\",\r\n"
                    + "    \"$schema\" : \"https://json-schema.org/draft/2020-12/schema\",\r\n"
                    + "    \"type\" : \"object\",\r\n"
                    + "    \"properties\" : {\r\n"
                    + "        \"mylist\" : {\r\n"
                    + "            \"type\" : \"array\",\r\n"
                    + "            \"items\" : {\r\n"
                    + "                \"type\" : \"string\",\r\n"
                    + "                \"pattern\" : \"^[0-9]{13}$\"\r\n"
                    + "            },\r\n"
                    + "            \"minItems\" : 1,\r\n"
                    + "            \"uniqueItems\" : true\r\n"
                    + "        }\r\n"
                    + "    }\r\n"
                    + "}";
            
            String instanceData = "{\r\n"
                    + "    \"mylist\": [\"123Abc\", \"123456789012\", \"1234567890123\"]\r\n"
                    + "}";
            JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012);
            JsonSchema schema = factory.getSchema(schemaData);
            schema.validate(instanceData, InputFormat.JSON).forEach(System.out::println);
    

    Yields the following results

    $.mylist[0]: does not match the regex pattern ^[0-9]{13}$
    $.mylist[1]: does not match the regex pattern ^[0-9]{13}$