else : Schema

else

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.

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.3
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 else keyword is used in conjunction with if to define a schema to be applied when a condition specified in the if keyword is false. It allows you to define alternative validation rules for instances that do not satisfy the conditions specified in the if keyword.

  • This keyword has no effect when if is absent.
  • This keyword has no effect when the instance passes 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" ] }
}
Valid An object instance that conforms to both the 'if' and 'then' subschemas is valid Instance
{ "foo": "foo", "bar": "bar" }
Invalid An object instance conforming to the 'if' subschema and not conforming to the 'then' subschema is invalid Instance
{ "foo": "foo" }
Valid An object instance not conforming to the 'if' subschema but conforming to the 'else' subschema is valid Instance
{ "foo": "not foo", "baz": "baz" }
Invalid An object instance that does not conform to both the 'if' and 'else' subschemas is invalid Instance
{ "foo": "not foo" }
Schema with 'if' and 'else' without 'then' Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "if": {
    "properties":
      { "foo": { "const": "foo" }
    }
  },
  "else": { "required": [ "baz" ] }
}
Valid An object instance that does not conform to the 'if' subschema but conforms to the 'else' subschemas is valid Instance
{ "foo": "not foo", "baz": "baz" }
Invalid If an instance does not conform to the 'if' subschema, then it must conform to the 'else' subschema Instance
{ "foo": "not foo" }
Valid An object instance conforming to the 'if' subschemas is always valid Instance
{ "foo": "foo", "baz": "baz" }