allOf keyword validates an instance against all schemas defined in its array value. This enables combining multiple schema constraints, making it useful for composition and extending schemas.
Syntax
Behavior
An instance validates successfully againstallOf if and only if it validates successfully against all schemas in the array.
- All subschemas are evaluated independently
- The instance must satisfy every subschema
- If any subschema fails, the entire
allOffails - Annotations from all successful subschemas are collected
Examples
Combining Type Constraints
Require a string with both minimum length and pattern constraints:"Hello", "World123"Invalid:
"hi" (too short), "hello" (doesn’t start with uppercase)
Extending Base Schemas
Combine a base schema with additional constraints:Numeric Range Constraints
Combine minimum and maximum constraints:0, 5, 50, 100Invalid:
-5, 3, 105
Schema Composition
Compose object schemas from multiple pieces:Common Use Cases
- Schema composition: Build complex schemas from simpler, reusable pieces
- Adding constraints: Extend a base schema with additional requirements
- Multiple validations: Apply different validation rules that must all pass
- Intersection types: Require an instance to satisfy multiple type definitions
Notes
- When collecting annotations, all subschemas must be examined even if validation could short-circuit
allOfwith a single schema is valid but redundant- Empty arrays are not permitted
- Subschemas are evaluated independently and do not affect each other