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.
Running the following with a recent version of json schema validator.
Yields the following results