conditional subschemas (writing conditions inside a JSON Schema)

Following keywords are used to implement conditional schema, which means validation of JSON will be happening with some defined condition.

All these keywords work together to implement the conditional application of a subschema based on the outcome of another subschema.

These keywords must not interact with each other across subschema boundaries. In other words, an "if" in one branch of an "allOf" must not have an impact on a "then" or "else" in another branch.

There is no default behavior for any of these keywords when they are not present. In particular, they must not be treated as if present with an empty schema, and when "if" is not present, both "then" and "else" must be entirely ignored.

if :

This keyword's value must be a valid JSON Schema. This keyword's subschema has no direct effect on the overall validation result. Rather, it controls which of the "then" or "else" keywords are evaluated.

keyword "if" is present (condition is satisfied) then subschema value of "then" will be validated. Else not then subschema value of "else" will be validated

If annotations are collected from this keyword's subschema in the usual way, including when the keyword is present without either "then" or "else".

then :

This keyword's value must be a valid JSON Schema.

When "if" is present, and the instance successfully validates against its subschema, then validation succeeds against this keyword if the instance also successfully validates against this keyword's subschema.

else :

This keyword's value must be a valid JSON Schema.

When "if" is present, and the instance fails to validate against its subschema, then validation succeeds against this keyword if the instance successfully validates against this keyword's subschema.

"then" & "else" keywords have no effect when "if" is absent, or when the instance successfully validates against its subschema. Implementations must not validate the instance against "then" & "else" keywords, for either validation or annotation collection purposes, in such cases.

Applying subschemas with boolean logic (keywords):

allOf :

This keyword's value must be a non-empty array. Each item of the array must be a valid JSON Schema. All schemas defined by this keyword's values are needed to be validated successfully.

anyOf :

This keyword's value must be a non-empty array. Each item of the array must be a valid JSON Schema. At least one schema defined by this keyword's needed to be validated successfully.

oneOf :

This keyword's value must be a non-empty array. Each item of the array must be a valid JSON Schema. Exactly one schema defined by this keyword's value needs to validated successfully.

not :

This keyword's value must be a valid JSON Schema. The instance needs to be failed with the schema defined with this keyword's value.

Comments

Popular posts from this blog

JSON Schema validation keywords with more explanation

nodejs/javascript client for Rabbitmq