then : Schema

then

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.

Kind Applicator
Applies To Any
Dialect 2020-12
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
Affected By
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.

  • The value of this keyword must be a valid JSON Schema.
  • 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 with 'if', 'then' and 'else' keyword Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "if": {
    "properties":
      { "foo": { "const": "foo" }
    }
  },
  "then": { "required": [ "bar" ] },
  "else": { "required": [ "baz" ] }
}
An object instance that conforms to both the 'if' and 'then' subschemas is valid Instance
{ "foo": "foo", "bar": "bar" }
An object instance conforming to the 'if' subschema and not conforming to the 'then' subschema is invalid Instance
{ "foo": "foo" }
An object instance not conforming to the 'if' subschema but conforming to the 'else' subschema is valid Instance
{ "foo": "not foo", "baz": "baz" }
An object instance that does not conform to both the 'if' and 'else' subschemas is invalid Instance
{ "foo": "not foo" }
Schema with 'if' and 'then' without 'else' Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "if": {
    "properties":
      { "foo": { "const": "foo" }
    }
  },
  "then": { "required": [ "bar" ] }
}
An object instance conforming to both the 'if' and 'then' subschemas is valid Instance
{ "foo": "foo", "bar": "bar" }
If an instance conforms to the 'if' subschema, then it must also conform to the 'then' subschema Instance
{ "foo": "foo" }
An object instance not conforming to the 'if' subschemas is always valid Instance
{ "foo": "not foo", "baz": "baz" }