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
Hint: Use the jsonschema metaschema and jsonschema lint commands to catch keywords set to invalid values
|
|---|---|
| Kind | Assertion |
| Applies To | Any |
| Base Dialect | Draft 6 |
| Changed In | None |
| Introduced In | Draft 6 |
| Vocabulary | Validation |
| Specification | https://json-schema.org/draft-06/draft-wright-json-schema-validation-01#rfc.section.6.24 |
| Metaschema | http://json-schema.org/draft-06/schema# |
| Official Tests | draft6/const.json |
| Default | None |
| Annotation | None |
| Affected By | None |
| Affects | None |
| Also See |
|
The const keyword (short for
“constant”) restricts instances to a single specific JSON value of any type.
Best Practice
Constraining instances to a constant value by definition
implies the given JSON type. 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 accommodate 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": "http://json-schema.org/draft-06/schema#",
"const": 5
}55.01234"Hello"{
"$schema": "http://json-schema.org/draft-06/schema#",
"const": { "name": "John Doe", "age": 30 }
}{ "name": "John Doe", "age": 30 }{ "name": "Jane Doe", "age": 30 }30