then : Schema
then
SchemaWhen if
is present, and the instance successfully validates against its subschema, then validation succeeds 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 |
Base 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 restricts instances to validate against the given subschema if the if sibling keyword successfully validated against the instance.
Common Pitfall
This keyword has no effect if the
if
keyword is not declared within the same subschema.
Best Practice
The
if
, then
,
and else
keywords can be thought of as imperative
variants of the anyOf
keyword, and both approaches are
equally capable of describing arbitrary conditions. Choose the one that more
elegantly describes your desired constraints.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": { "multipleOf": 2 },
"then": { "minimum": 0 }
}
10
-2
7
-3
"Hello World"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": { "multipleOf": 2 },
"then": { "title": "The value is an even number" }
}
10
{ "keyword": "/then/title", "instance": "", "value": [ "The value is an even number" ] }
5