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
Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
|
---|---|
Kind | Assertion |
Applies To | Any |
Base 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 restricts instances to a finite set of possible values,
which may be of different types.
Best Practice
Constraining instances to a set of possible values by
definition implies the given JSON types. Therefore, combining this keyword with
the type
keyword is redundant (or
even invalid if types don’t agree), and considered an
anti-pattern.
Common Pitfall
There are programming languages, such as JavaScript, that
cannot distinguish between integers and real
numbers. To accomodate for
those cases, JSON Schema considers a real number with a zero fractional part to
be equal to the corresponding integer. For example, in JSON Schema, 1
is
considered to be equal to 1.0
.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ "red", "green", "blue" ]
}
"green"
"black"
2
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ 1, 2.0, 3 ]
}
1
2
5
"Hello"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"enum": [ "red", 123, true, { "foo": "bar" }, [ 1, 2 ], null ]
}
true
{ "foo": "bar" }
{ "foo": "baz" }