if : Schema
if
SchemaThis keyword declares a condition based on the validation result of the given schema.
Value | This keyword must be set to a valid JSON Schema |
---|---|
Kind | Applicator |
Applies To | Any |
Dialect | 2020-12 |
Changed In | None |
Introduced In | Draft 7 |
Vocabulary | Applicator |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.2.2.1 |
Metaschema | https://json-schema.org/draft/2020-12/meta/applicator |
Official Tests | draft2020-12/if-then-else.json |
Default | None |
Annotation | None |
Affected By | None |
Affects |
|
Also See |
|
The if
keyword is used to conditionally apply a subschema based on whether a certain condition is met. It allows you to define different validation rules depending on whether an instance satisfies a condition or not. The validation outcome of 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.
- If an instance passes the validation against the
if
subschema, then it must also be validated against thethen
subschema, if present. - If an instance fails the validation against the
if
subschema, then it must also be validated against theelse
subschema, if present. - This keyword is meaningless without
then
andelse
. - The annotations are collected from this keyword’s subschema in the usual way, irrespective of whether
then
andelse
are present or not.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"role": { "enum": [ "HOD", "professor" ] },
"HOD_Id": { "type": "integer" },
"professor_Id": { "type": "integer" }
},
"if": {
"properties":
{ "role": { "const": "HOD" }
}
},
"then": { "required": [ "HOD_Id" ] },
"else": { "required": [ "professor_Id" ] }
}
{ "name": "John Doe", "role": "HOD", "HOD_Id": 2844 }
{ "role": "professor" }
{ "professor_Id": 2899, "HOD_Id": 2844 }
{ "name": "John Doe", "role": "HOD", "HOD_Id": "2844" }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties":
{ "foo": { "const": "foo" }
}
},
"then": { "required": [ "bar" ] }
}
{ "foo": "foo", "bar": "bar" }
{ "foo": "foo" }
{ "foo": "not foo", "baz": "baz" }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties":
{ "foo": { "const": "foo" }
}
},
"else": { "required": [ "baz" ] }
}
{ "foo": "not foo", "baz": "baz" }
{ "foo": "not foo" }
{ "foo": "foo", "baz": "baz" }
- Note: If an instance passes the
if
subschema and thethen
subschema is not present, then thethen
subschema behaves as a boolean true schema. Similarly, if an instance fails theif
subschema and theelse
subschema is not present, then theelse
subschema behaves as a boolean true schema.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties": {
"foo": {
"title": "This is foo!",
"const": "foo"
}
}
}
}
{ "foo": "foo" }
{ "keyword": "/if/properties", "instance": "", "value": [ "foo" ] }
{ "keyword": "/if/properties/foo/title", "instance": "", "value": "This is foo!" }
- Here, the annotations are collected from
if
’s subschema in the usual way, irrespective of whetherthen
andelse
are present or not.