then : Schema
then
SchemaWhen 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.
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.2 |
Metaschema | https://json-schema.org/draft/2020-12/meta/applicator |
Official Tests | draft2020-12/if-then-else.json |
Default |
{}
|
Annotation | None |
Affected By |
|
Affects | None |
Also See |
|
The then
keyword is used in conjunction with if
to define a schema to be applied when a condition specified in the if
keyword is true. It allows you to apply additional validation logic based on whether certain conditions are met.
- This keyword has no effect when
if
is absent. - This keyword has no effect when the instance fails validation against the
if
subschema.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties":
{ "foo": { "const": "foo" }
}
},
"then": { "required": [ "bar" ] },
"else": { "required": [ "baz" ] }
}
{ "foo": "foo", "bar": "bar" }
{ "foo": "foo" }
{ "foo": "not foo", "baz": "baz" }
{ "foo": "not foo" }
{
"$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" }