const : Any

const

Any

Validation succeeds if the instance is equal to this keyword’s value.

Kind Assertion
Applies To Any
Dialect 2020-12
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
Also see

The const keyword in 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 and enum.

Examples

Schema with a specific string value Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "const": "hello"
}
An instance matching the const value is valid Instance
"hello"
An instance not matching the const value is invalid. Instance
"world"
Schema with a specific number value Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "const": 3.14159
}
An instance matching the const value is valid Instance
3.14159
An instance not matching the const value is invalid. Instance
"pi"
Schema with a fixed object structure Schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "const": { "name": "John Doe", "age": 30 }
}
An empty object is invalid Instance
{}
An instance matching the exact object structure is valid Instance
 { "name": "John Doe", "age": 30 }
An instance not matching the exact object structure is invalid Instance
 { "name": "Robert", "age": 30 }