Closed schema with undefined required property – valid or invalid? #143
-
If you have a closed schema The common interpretation I see is that the schema is valid. Would there be merit in classifying should a schema as invalid to prevent ambiguity for tools enabling schema authorship and for the schema authors? Example Schema:
It seems possible that you can get to a situation where the instance cannot honor the schema. Instances trying to conform to the schema generally result in a dead-end from a validation failure perspective:
I've spotted some implementation nuances regarding how this is interpreted (not exhaustive):
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There are many ways in JSON Schema to construct schemas that always "dead-end" as you put it. A JSON Schema is just a collection of assertions. If a validator knows how to evaluate each assertion, the schema is valid. But, if you add two assertions that are mutually exclusive, you'll get a dead-end (Example: if (x < 5 && x > 7) {
console.log("I see dead code");
} Most programming languages have tools that are often called "linters" to statically (with out running the code) analyze the code to look for potential problems like unreachable code. www.json-schema-linter.com isn't a validator, it's a linter. That's why it detected your dead-end case and the others didn't. Its purpose is to detect those kinds of problems. It's telling you that your schema probably has a problem, not that it's invalid. All the other implementations you linked to are validators. Their purpose is to execute schemas. As long as they understand the assertions, they can do their job. We definitely want to see more and better linters for JSON Schema to help detect these kinds of problems in the future. |
Beta Was this translation helpful? Give feedback.
There are many ways in JSON Schema to construct schemas that always "dead-end" as you put it. A JSON Schema is just a collection of assertions. If a validator knows how to evaluate each assertion, the schema is valid. But, if you add two assertions that are mutually exclusive, you'll get a dead-end (Example:
{ "allOf": [{ "type": "string" }, { "type": "boolean" }] }
). That's a whole other class of problem and one all programming languages have. Consider that the following is a valid program that also has a dead-end.Most programming languages have tools that are often called "linters" to statically (with out running the code) analy…