Overview
JSON Schema validation asserts constraints on the structure of instance data. An instance location that satisfies all asserted constraints is then annotated with any keywords that contain non-assertion information. If all locations within the instance satisfy all asserted constraints, then the instance is said to be valid against the schema.Each schema object is independently evaluated against each instance location to which it applies. This greatly simplifies implementation requirements by ensuring validators do not need to maintain state across the document-wide validation process.
How Validation Works
Validation is a recursive process. A JSON value is considered valid against a schema if, and only if, it satisfies the constraints defined by every keyword in that schema.Recursive Evaluation
Some keywords contain one or more subschemas. These keywords create complex constraints or describe compound values like arrays and objects.- The root schema declares the instance MUST be an object
- The
propertieskeyword applies separate schemas to each property - Each property schema is evaluated recursively
- The instance is valid only if all recursive evaluations succeed
Evaluation Process
Evaluating an instance against a schema involves:- Processing applicator keywords until a schema object with no applicators is reached
- Evaluating assertion keywords at the appropriate location in the instance
- Combining keyword results according to the rules for each keyword
- Collecting annotations from keywords that produce them
Evaluation of a parent schema object can complete once all of its subschemas have been evaluated. Some evaluations may be short-circuited based on assertion results, but when collecting annotations, all applicable subschemas must be examined.
Assertions vs Annotations
Keywords in JSON Schema have different behaviors:Assertion Keywords
Assertion keywords produce a boolean result indicating whether the instance satisfies a constraint.- Type Assertions
- Numeric Assertions
- String Assertions
"hello"→ ✓ Valid42→ ✗ Invalid
Annotation Keywords
Annotation keywords attach metadata to instance locations for application use. They do not affect validation results.Annotations like
title, description, and default provide information for documentation, UI generation, and other application-specific purposes. They never cause validation to fail.Assertion Behavior and Instance Types
Most assertion keywords only constrain values within a certain primitive type. When the instance type doesn’t match the keyword’s target type, the instance is considered to conform to the assertion.- The
maxLengthkeyword only restricts strings that are too long - If the instance is a number, boolean, null, array, or object, it’s valid against
maxLength - The
typekeyword independently restricts the instance to string or null - A null value passes both
typeandmaxLengthvalidations
This behavior allows keywords to be used easily with instances that can be of multiple primitive types. Each keyword is evaluated separately unless explicitly specified otherwise.
Validation Keywords by Type
Any Instance Type
type - Validates the instance is one of the specified types:
enum - Validates the instance equals one of the specified values:
const - Validates the instance equals a specific value:
Numeric Instances
For numbers and integers:minimum/maximum- Inclusive boundsexclusiveMinimum/exclusiveMaximum- Exclusive boundsmultipleOf- Instance must be a multiple of the specified number
String Instances
minLength/maxLength- Length constraints (in Unicode code points)pattern- Regular expression the string must match
Array Instances
minItems/maxItems- Size constraintsuniqueItems- Whether all items must be unique
Object Instances
minProperties/maxProperties- Number of propertiesrequired- Array of required property namesdependentRequired- Properties required when another property is present
Validation Examples
Simple Validation
- Schema
- Valid Instance
- Invalid Instance
Complex Validation
- Schema
- Valid Examples
- Invalid Example
Short-Circuit Evaluation
For efficiency, implementations may use “short-circuit” evaluation for assertions:- If the instance fails the first schema (
type: string), validation can stop immediately - The remaining schemas don’t need to be evaluated
Validation Result
JSON Schema implementations produce a single boolean result when evaluating an instance against schema assertions:true- The instance is valid: all assertions passedfalse- The instance is invalid: at least one assertion failed
An instance can only fail an assertion that is present in the schema. Omitted keywords have default behavior that does not cause validation to fail.
- The instance location that failed validation
- The schema location (keyword) that produced the failure
- The evaluation path taken to reach the failure
- An error message describing why validation failed