JSON Schema · Dataintermediate
JSON Schema (Draft 2020-12)
A JSON Schema 2020-12 document for a user record: types, required fields, formats, enums, $defs with $ref, and additionalProperties: false. The same dialect is used by OpenAPI 3.1, MCP tool schemas and LLM structured outputs.
json-schemavalidation2020-12structured-outputsopenapi
Preview
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/user.json",
"title": "User",
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"email": {
"type": "string",
"format": "email",
"maxLength": 254
},
"name": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"role": {
"enum": [
"admin",
"editor",
"viewer"
]
},
"age": {
"type": "integer",
"minimum": 0,
"maximum": 150
},
"nickname": {
"type": [
"string",
"null"
]
},
"address": {
"$ref": "#/$defs/address"
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"maxItems": 20
}
},
"required": [
"id",
"email",
"name",
"role"
],
"additionalProperties": false,
"$defs": {
"address": {
"type": "object",
"properties": {
"street": {
"type": "string"
},
"city": {
"type": "string"
},
"country": {
"type": "string",
"pattern": "^[A-Z]{2}$"
}
},
"required": [
"city",
"country"
],
"additionalProperties": false
}
}
}AI actions
Documentation
Purpose
Describe the shape a JSON document must have, so validators, code generators, editors and LLMs can check or produce it.
When to use
Validating API payloads or config files, generating types, adding editor autocomplete with $schema, or constraining an LLM to return valid JSON.
Required fields
- $schema: the dialect URI, "https://json-schema.org/draft/2020-12/schema"
- type: the root type, usually "object"
Optional fields
- $id: a unique URI for this schema
- properties / required: the fields and which must be present
- additionalProperties: false rejects unknown keys
- $defs + $ref: reusable sub-schemas ($defs replaced "definitions" in 2019-09)
- enum / const / format / pattern / minimum / maxLength: value constraints
- oneOf / anyOf / allOf / if-then-else: composition
Best practices
- Always declare $schema so tools pick the right dialect.
- Set additionalProperties: false on objects you control; it catches typos.
- Use $defs and $ref instead of copy-pasting sub-schemas.
- Remember "format" is an annotation by default; turn on format assertion in your validator if you rely on it.
Security considerations
- Do not let validators fetch remote $ref URLs from untrusted schemas.
- Bound string lengths and array sizes (maxLength, maxItems) on public inputs.
- Validation checks shape, not authorization; still check permissions server-side.