enum : Array<Any>

enum

Array<Any>

Validation succeeds if the instance is equal to one of the elements in this keyword’s array value.

Kind Assertion
Applies To Any
Dialect 2020-12
Introduced In Draft 1
Vocabulary Validation
Specification https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.1.2
Metaschema https://json-schema.org/draft/2020-12/meta/validation
Also see

The enum keyword specifies a validation constraint for an instance, defining a set of permissible values. The value of the enum keyword must be an array containing at least one element, and these elements should be unique. The validation succeeds if the value of the instance matches one of the elements in the enum array.

Note: Using the type keyword along the enum keyword is considered an anti-pattern, as enum constraints instances tighter than type.

Examples

Schema with string enum Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "enum": [ "red", "green", "blue" ]
}
Instance with value present in the enum is valid Instance
"green"
Instance with value not present in the enum is invalid Instance
"black"
Schema with number enum Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "enum": [ 2, 45, 100 ]
}
Instance with value present in the enum is valid Instance
45
Instance with value not present in the enum is invalid Instance
70
Instance with value having different datatype is invalid Instance
"2"
Schema with mixed-type enum Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "enum": [ "red", 123, true, { "foo": "bar" }, [ 1, 2 ], null ]
}
Instance with value present in the enum is valid Instance
true
Instance with value not present in the enum is invalid Instance
{ "foo": "baz" }
  • Without specifying a type, you can utilize enum to accept values of various types.