问题
I'm trying to validate that a JSON Schema is actually a JSON Schema, and not an instance, as I have read, resource for that is validate against meta-schema e.g:
- Core validation meta-schema (http://json-schema.org/draft/2019-09/schema)
- Older versions meta-schema (https://json-schema.org/draft-04/schema)
I have tried with different validation libraries, json-schema-validator for Java, and jsonschema for Python to have more assurance, but I keep on obtaining the funny assertion that this is a valid JSON Schema instance.
{
"hey" : {
"you" : {
"how" : {
"dyd" : "Very well, ty"
}
}
}
}
I'm coming here because it seems obvious I have some big misconception or misunderstanding, as I cannot understand how a clear JSON instance (it declares no data types) can be validated as a JSON Schema instance.
Initial problem I wanted to solve, as I stated on the beginning, is how to validate a JSON Schema, but if any JSON valid instance is too a valid JSON schema (as results are throwing), how to assert this?
回答1:
The short answer is: JSON Schema is designed for extensibility. That means it allows any kind of additional properties to be added as long as they are not conflicting with the known/expected keywords.
In your case, the hey
property is certainly not a known keyword, i.e. it is simply being ignored during validation. That leaves you with the valid JSON Schema {}
which allows any type.
How to ensure something is actually a JSON Schema then? That depends on how much narrower you want to define the term.
- You could enforce that the top-level needs to define a particular
$schema
version. - You could enforce that at least on the top-level there is a valid
type
attribute. - If you know where those JSON Schemas are coming from and that they are not making use of this extensibility, you could manipulate your target Meta JSON Schema and include
”additionalProperties”: false
as top-level property before triggering the validation.
来源:https://stackoverflow.com/questions/60152271/how-can-this-be-a-json-schema