JSON Schema Syntax Validator
Hello! I'm new here. This project was recommended by java-json-tools, our current validator. Since it is no longer maintained, we are moving away from it.
They had functionality to validate the syntax of the schema itself. Is there something similar in this project? If so, could you please point me to the documentation? If not, do you have a recommendation on how to ensure schemas are valid?
@hmthomax normally, we don't need to validate the schema explicitly as we assume it is valid. The library is designed to be used at runtime to validate the HTTP request and response so we cannot validate schema in every call. However, if you want to double-check your schema design, you can validate it against the Meta Schema. In this case, the Meta Schema is the JSON schema and your newly created schema is the data. A version of Meta Schemas is chosen during the initialization and here an example.
https://github.com/networknt/json-schema-validator/blob/master/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java#L39
Thanks @stevehu! I got this working using a meta schema.
Just a side note, our system allows for super users to create their own schemas as we allow for many different data models, so we actually do validate schemas at runtime hence this use case.
Excited to see what else we can utilize from this project. Thanks again.
Thanks @stevehu! I got this working using a meta schema.
Just a side note, our system allows for super users to create their own schemas as we allow for many different data models, so we actually do validate schemas at runtime hence this use case.
Can you share the meta schema you used? I have a similar use case, in which I need to check the syntax of my schema before persisting and using it.
@SirMoM The meta schemas are published by json-schema.org and we are using them to create meta schemas for different draft versions.
https://github.com/networknt/json-schema-validator/blob/master/src/main/java/com/networknt/schema/JsonMetaSchema.java#L122
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2019-09/schema",
"title": "Person",
"type": "object",
"additionalProperties": false,
"properties": {
"firstName": {
"type": "string",
"minLength": 3,
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "foo",
"minimum": 0
}
}
}
@stevehu Then let me rephrase that: How can I ensure that the type "foo" which isn't defined anywhere is not validated?
JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909).getSchema(URI("https://json-schema.org/draft/2019-09/schema"), config).validate(schemaToValidate)
This returns an empty Set and I don't know if this is intended or not?
But if I change the overall type from "object" to "bar" then I get the desired message:
[$.type: does not have a value in the enumeration [array, boolean, integer, null, number, object, string], $.type: should be valid to any of the schemas array]
This seams inconsistent to me since nested "type"-Keywords are not validated.
But maybe I am just missing something.
Thanks in advance.
@hmthomax Would you be able to help @SirMoM? It would be much better if you guys can figure out and write a one-page document int the doc folder.
@stevehu can you reopen this issue? The behavior of the validation of nested keywords isn't consistent.
@SirMoM I am sorry that I missed your inconsistency issue. Using meta schema to validate a schema file uses the same logic as using a schema file to validate a JSON. I am afraid this is a bug in the type validation. I am wondering if you could create a test case to validate nested types in a normal validation. Also, it would be great if you could submit both test cases so that more people can reproduce it and understand the issue. Thanks.
@stevehu I've created a gist to showcase my problem: ValidationTests.java. I hope this helps to understand the problem.
If you need any additional Information please contact me.
@SirMoM Thanks a lot for the gist. I have put it into the test case with this commit. I am sure this is related to the issue313. What I have found is that the meta schema for 2019-09 is significantly different than V7 and I guess we are not parsing it correctly. I am so busy these days and any help would be great from the community.
d5a861affba41159f1c4187662144297fe7cf8c8
I've continued the discussion in #313.