1 year ago
#262616
Abhishek Kumar
networknt schema validator is not consistent for "AutoDetect JsonSchema", "Load schema from URL" or "Load schema from Loc"
I tried to validate custom JsonSchema with 3 ways
- AutoDetect JsonSchema From JsonNode - No errors, but 11 WARN
- JsonSchema From Url - 2 Errors, No WARN
- JsonSchema From String contents - 2 Errors, No WARN
I am expecting all 3 schema factory to provide similar result. At this moment, I am not sure which one is correct approach.
Why AutoDetect JsonSchema is not failing?
Based on our requirement, "AutoDetect JsonSchema From JsonNode" is best fit for us.
Detail to replicate the issue:
Sample code -
public class ValidateJsonSchemaTest {
private ObjectMapper mapper = new ObjectMapper();
private JsonNode testJsonSchema = null;
private String DRAFT_7_SCHEMA_URI="https://json-schema.org/draft-07/schema";
private static final Logger LOGGER = LoggerFactory.getLogger(ValidateJsonSchemaTest.class);
@Before
public void setup() throws IOException {
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("json-validation-poc/test-custom-schema.json");
String content = IOUtils.toString(resourceAsStream, Charset.defaultCharset());
testJsonSchema = mapper.readTree(content);
}
@Test
public void testAutoDetectJsonSchemaFromJsonNode() {
JsonSchema schema = getJsonSchemaFromJsonNodeAutomaticVersion(testJsonSchema);
Set<ValidationMessage> errors = schema.validate(testJsonSchema);
if(errors.size() > 0) {
LOGGER.error("testAutoDetectJsonSchemaFromJsonNode :: Validation Error %S", errors);
}
Assert.assertEquals("Error size", 0, errors.size());
}
@Test
public void testJsonSchemaFromUrl() throws URISyntaxException {
JsonSchema schema = getV7JsonSchemaFromUrl();
Set<ValidationMessage> errors = schema.validate(testJsonSchema);
if(errors.size() > 0) {
LOGGER.error("testJsonSchemaFromUrl :: Validation Error '{}'", errors);
}
Assert.assertEquals("Error size", 0, errors.size());
}
@Test
public void testJsonSchemaFromStringContent() throws IOException {
JsonSchema schema = getV7JsonSchemaFromStringContent();
Set<ValidationMessage> errors = schema.validate(testJsonSchema);
if(errors.size() > 0) {
LOGGER.error("testJsonSchemaFromStringContent :: Validation Error '{}'", errors);
}
Assert.assertEquals("Error size", 0, errors.size());
}
protected JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {
VersionFlag versionFlag = SpecVersionDetector.detect(jsonNode);
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(versionFlag);
JsonSchema schema = factory.getSchema(jsonNode);
return schema;
}
protected JsonSchema getV7JsonSchemaFromUrl() throws URISyntaxException {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
JsonSchema schema = factory.getSchema(new URI(DRAFT_7_SCHEMA_URI));
return schema;
}
protected JsonSchema getV7JsonSchemaFromStringContent() throws IOException {
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("json-validation-poc/json-draft-07-schema.json");
String schemaContent = IOUtils.toString(resourceAsStream, Charset.defaultCharset());
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7);
return factory.getSchema(schemaContent);
}
}
Refer ValidateJsonSchemaTest.java for more details.
version used for testing: com.networknt:json-schema-validator:1.0.66
Screenshot of test failure:
Log screenshot:
Reported this issue to Github-networknt/json-schema-validator as well
json
jsonschema
json-schema-validator
0 Answers
Your Answer