Tuple Validation with prefixItems in JSON Schema
Learn how to validate fixed-length arrays (tuples) using prefixItems in JSON Schema. Each position has its own type, perfect for coordinate pairs and records.
Detailed Explanation
Tuple Schemas: Fixed-Position Arrays
Unlike regular arrays where every element follows the same schema, tuples define a specific schema for each position. JSON Schema Draft 2020-12 uses the prefixItems keyword for this purpose (older drafts used items as an array).
Example: Coordinate Pair
Given a sample like:
[40.7128, -74.0060]
A tuple schema looks like:
{
"type": "array",
"prefixItems": [
{ "type": "number", "minimum": -90, "maximum": 90 },
{ "type": "number", "minimum": -180, "maximum": 180 }
],
"items": false
}
prefixItemsdefines the schema for each position: index 0 is latitude, index 1 is longitude.items: falseprevents any additional elements beyond the defined positions. Without this, extra elements would be allowed.
Example: Mixed-Type Record
Tuples are also useful for CSV-like records:
["Alice", 30, true]
Schema:
{
"type": "array",
"prefixItems": [
{ "type": "string" },
{ "type": "integer", "minimum": 0 },
{ "type": "boolean" }
],
"items": false,
"minItems": 3,
"maxItems": 3
}
Adding minItems and maxItems equal to the number of prefixItems entries enforces an exact length.
Generator Behavior
Most generators produce a standard items schema (homogeneous array) by default. Tuple generation typically requires the generator to detect that the array has a fixed length with differently typed positions. If the generator does not detect this pattern, you can convert the output manually by replacing items with prefixItems.
When to Use Tuples
- Coordinates:
[lat, lng]or[x, y, z] - Ranges:
[start, end]for dates, numbers, or pagination - Heterogeneous records: Rows from CSV imports where each column has a distinct type
- Return values: Functions that return multiple values in a fixed order
Draft Compatibility
- Draft 2020-12: Use
prefixItemsfor tuple schemas. - Draft 7 / 2019-09: Use
itemsas an array plusadditionalItems: false.
Be mindful of which draft your validator supports when choosing the syntax.
Use Case
Use tuple validation for geographic coordinates, date ranges, fixed-format data rows, or any array where each position carries a distinct semantic meaning.
Try It — JSON Schema Generator
Related Topics
Array Items Schema — Validating Array Elements
Array Schemas
JSON Schema Number Constraints — minimum, maximum, multipleOf
Basic Types
oneOf, anyOf, allOf — Schema Composition in JSON Schema
Advanced Patterns
Generate a Simple Object Schema from JSON
Basic Types
Enum Values in JSON Schema — Restricting to Allowed Values
Advanced Patterns