const : Any
const
AnyValidation succeeds if the instance is equal to this keyword’s value.
Value | This keyword must be set to a JSON value |
---|---|
Kind | Assertion |
Applies To | Any |
Dialect | 2020-12 |
Changed In | None |
Introduced In | Draft 6 |
Vocabulary | Validation |
Specification | https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.1.3 |
Metaschema | https://json-schema.org/draft/2020-12/meta/validation |
Official Tests | draft2020-12/const.json |
Default | None |
Annotation | None |
Affected By | None |
Affects | None |
Also See |
|
The const
keyword restricts an instance to a specific value. Its usage is functionally similar to an enum
with a single value. Instances validate successfully only if their property value deeply matches the specified constant.
- Applies to various JSON data types, including numbers, strings, booleans, objects, and arrays.
- Takes precedence over other validation keywords like
type
andenum
.
Best Practice
It is best practice to avoid using the
type
keyword or any other validation keyword with const
, as const
takes precedence over them. Therefore, it is better not to use them together.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"const": "hello"
}
"hello"
"world"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"const": 3.14159
}
3.14159
"pi"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"const": { "name": "John Doe", "age": 30 }
}
{}
{ "name": "John Doe", "age": 30 }
{ "name": "Robert", "age": 30 }