Looking to master JSON Schema for OpenAPI?

A 9+ hour course taught by a member of the JSON Schema Technical Steering Committee

else : Schema

else

Schema

When if is present, and the instance fails to validate against its subschema, then validation succeeds if the instance successfully validates against this keyword’s subschema.

Value This keyword must be set to a valid JSON Schema Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
Kind Applicator
Applies To Any
Base Dialect Draft 7
Changed In None
Introduced In Draft 7
Vocabulary Validation
Specification https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.6.6.3
Metaschema http://json-schema.org/draft-07/schema#
Official Tests draft7/if-then-else.json
Default {}
Annotation None
Affected By
Affects None
Also See

The else keyword restricts instances to validate against the given subschema if the if sibling keyword failed to validate against the instance.

Examples

A schema that constrains numeric instances to be positive when they are odd numbers Schema
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "if": { "multipleOf": 2 },
  "else": { "minimum": 0 }
}
Valid An even number value that is positive is valid Instance
10
Valid An even number value that is negative is valid Instance
-2
Valid An odd number value that is positive is valid Instance
7
Invalid An odd number value that is negative is invalid Instance
-3
Valid A non-number value is valid Instance
"Hello World"
A schema that uses a title for odd numbers Schema
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "if": { "multipleOf": 2 },
  "else": { "title": "The value is an odd number" }
}
Valid An odd number value is valid Instance
7
Valid An even number value is valid Instance
6