enum : Array<Any>
enum
Array<Any>Validation succeeds if the instance is equal to one of the elements in this keyword’s array value.
Value | This keyword must be set to a non-empty array of unique JSON values |
---|---|
Kind | Assertion |
Applies To | Any |
Dialect | 2020-12 |
Changed In | None |
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 |
Official Tests | draft2020-12/enum.json |
Default | None |
Annotation | None |
Affected By | None |
Affects | None |
Also See |
|
The enum
keyword specifies a validation constraint for an instance, defining a set of permissible values. 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": "https://json-schema.org/draft/2020-12/schema",
"enum": [ "red", "green", "blue" ]
}
"green"
"black"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ 2, 45, 100 ]
}
45
70
"2"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ "red", 123, true, { "foo": "bar" }, [ 1, 2 ], null ]
}
true
{ "foo": "baz" }
- Without specifying a type, you can utilize enum to accept values of various types.