JSON Schema Cheat Sheet: Draft 2020-12 Keywords with Examples
Checked against JSON Schema Draft 2020-12 (current per json-schema.org); run on Ajv 8.20.0 + ajv-formats 3.0.1 and jsonschema 4.26.0 (Python) on 28 November 2026. Each section links to the official docs it was checked against.
This sheet is for developers writing schemas to validate API payloads, config files or LLM tool output. It covers draft 2020-12, which json-schema.org lists as the current version (the previous one was 2019-09). Every example on this page was validated with Ajv 8.20.0 in Node and with the Python jsonschema 4.26.0 package.
When to use what
Start every schema with "$schema": "https://json-schema.org/draft/2020-12/schema", a type, and for objects a required list. Most bugs I see come from schemas that are too loose, not too strict: without type, keywords such as minimum or minLength silently pass values of the wrong type.
For closed objects, additionalProperties: false works until you compose schemas with allOf. It only sees properties declared next to it, so extending a closed base schema rejects the new fields. Use unevaluatedProperties: false at the outer level instead; it sees properties evaluated by subschemas too.
Prefer anyOf to oneOf unless the branches truly can't overlap. oneOf fails when two branches both match, which is easy to trigger by accident (number and integer, or two objects without a distinguishing field). Use if/then/else when one field decides what another must look like.
Don't count on format to reject bad data. In 2020-12 it is an annotation by default; Python's jsonschema ignores it unless you pass a format checker, and Ajv needs ajv-formats. To try schemas, use the JSON Schema validator, draft one from sample data with the schema generator, and produce test payloads with the JSON Schema faker. If you validate in TypeScript, JSON to Zod and the Zod validation patterns post cover the code-first route.
{"type": "string"}Accepts only strings; other types fail with must be string
{"type": ["string", "null"]}Type list: accepts a string or null, the usual way to allow nullable fields
{"type": "integer"}Integer check is by value: 1.0 passes, 1.5 fails
{"enum": ["admin", "editor", "viewer"]}Value must equal one item; error is must be equal to one of the allowed values
{"const": "v1"}Value must equal exactly this; good for discriminator fields such as kind
{"not": {"type": "null"}}Inverts a subschema: here anything except null is valid
Floating point bites multipleOf: both Ajv and Python jsonschema reported 19.99 as not a multiple of 0.01. Ajv has a multipleOfPrecision option; otherwise validate money as integer cents.
{"minLength": 2, "maxLength": 50}Length in characters (code points): a single emoji is length 1, so it fails minLength: 2
{"pattern": "^[0-9]{5}$"}ECMA-262 regex; it is not anchored, so add ^ and $ ([0-9]{3} matched abc1234xyz)
{"minimum": 1, "maximum": 100}Inclusive bounds; error must be >= 1
{"exclusiveMinimum": 0}In 2020-12 this is a number, not a boolean: 0 fails with must be > 0
{"multipleOf": 0.01}Value divided by this must be an integer; see the floating-point note above
{"type": "string", "format": "date-time"}RFC 3339 timestamp when format is asserted; 2026-11-28 10:00 failed in Ajv with ajv-formats
"properties": {"id": {"type": "integer"}}Schemas for named properties; properties not listed are allowed unless you close the object
"required": ["id", "email"]Lists properties that must be present; properties alone never makes a field mandatory
"additionalProperties": falseRejects keys not in properties or patternProperties of the same subschema
"unevaluatedProperties": falseRejects keys that no subschema (including allOf branches) evaluated; use when composing
"dependentRequired": {"creditCard": ["billingAddress"]}If creditCard is present, billingAddress must be too
"patternProperties": {"^x-": {}}Applies a schema to every key matching a regex, e.g. vendor extensions
"items": {"type": "string"}Every element must match this schema
"prefixItems": [{"type": "number"}, {"type": "number"}], "items": falseTuple: two numbers and nothing more; [1, 2, 3] fails must NOT have more than 2 items
"minItems": 2Add this to a tuple, or [1] still passes because prefixItems doesn't require length
"uniqueItems": trueNo duplicates; error names the indexes, e.g. items ## 1 and 0 are identical
"contains": {"type": "integer"}, "minContains": 2At least two elements must be integers; maxContains caps the count
"$defs": {"address": {...}}Holds reusable subschemas inside the same document (replaced definitions)
{"$ref": "#/$defs/address"}Points at a subschema with a JSON Pointer fragment; errors report the instance path such as /address/zip
"allOf": [A, B]Must match every subschema; pair with unevaluatedProperties rather than additionalProperties
"anyOf": [A, B]Must match at least one; 5 against number and integer branches passes
"oneOf": [A, B]Must match exactly one; 5 fails with must match exactly one schema in oneOf (passingSchemas 0 and 1)
"if": {...}, "then": {...}, "else": {...}Conditional: when if passes, then applies, otherwise else. An if with only properties passes on {}
import Ajv2020 from "ajv/dist/2020"Ajv class for 2020-12; the default ajv import failed with no schema with key or ref for a 2020-12 $schema
addFormats(ajv)From ajv-formats; without it Ajv throws unknown format "email" ignored in schema
new Ajv2020({ allErrors: true })Collect every error instead of stopping at the first
new Ajv2020({ strict: false })Strict mode is on by default and throws on typos such as maxlength; turn off only for third-party schemas
Draft202012Validator(s, format_checker=Draft202012Validator.FORMAT_CHECKER)Python jsonschema: assert format; without the checker, "nope" passed format: email
jsonschema.validators.validator_for(schema)Python: picks the validator class from $schema; with no $schema it returned Draft202012Validator
Worked example: Validate a user payload and read the errors
Here is a 2020-12 schema for a user object with a reusable address definition:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": { "type": "integer", "minimum": 1 },
"email": { "type": "string", "format": "email" },
"role": { "enum": ["admin", "editor", "viewer"] },
"tags": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
"address": { "$ref": "#/$defs/address" }
},
"required": ["id", "email"],
"additionalProperties": false,
"$defs": {
"address": { "type": "object", "properties": { "zip": { "type": "string", "pattern": "^[0-9]{5}$" } }, "required": ["zip"] }
}
}
I validated this payload with Ajv 8.20.0, allErrors: true and ajv-formats:
{ "id": 0, "email": "not-an-email", "role": "owner", "tags": ["a", "a"], "address": { "zip": "1234" }, "nickname": "x" }
"" additionalProperties must NOT have additional properties (nickname)
/id minimum must be >= 1
/email format must match format "email"
/role enum must be equal to one of the allowed values
/tags uniqueItems must NOT have duplicate items (items ## 1 and 0 are identical)
/address/zip pattern must match pattern "^[0-9]{5}$"
Each error has an instance path (a JSON Pointer into the payload) and the keyword that failed. The empty path means the root object, which is where additionalProperties lives. The /address/zip error shows that $ref validation reports against the data, not against $defs. Before I added ajv-formats, the same schema didn't even compile: Ajv refused the unknown email format. Python's jsonschema takes the opposite default and silently accepts the bad email unless you pass a format checker.
Common mistakes
Putting
required: trueinside a property, the way some ORMs and OpenAPI 2 docs do.List required names in an array on the parent object:
"required": ["id"].Assuming
format: emailorformat: daterejects bad strings.Turn on format assertion in your validator (ajv-formats in Ajv, a format checker in Python) or add a
pattern.Extending a base schema that has
additionalProperties: falseviaallOfand then wondering why every new field fails.Remove it from the base and put
unevaluatedProperties: falseon the outer schema.Using
oneOffor branches that can both match, likenumberandinteger.Use
anyOf, or make the branches exclusive with aconstdiscriminator field.Writing an
ifthat tests a property's value without requiring the property.An absent property makes the
ifpass, sothenruns on{}. Add"required": ["country"]inside theif.Upgrading a draft-07 schema by only changing
$schema, leaving tupleitems: [...]anddefinitions.Rename tuple
itemsarrays toprefixItems, oldadditionalItemstoitems, and movedefinitionsto$defswith matching$refpaths.
Related cheat sheets
Sources
- https://json-schema.org/specification
- https://json-schema.org/draft/2020-12/json-schema-core
- https://json-schema.org/draft/2020-12/json-schema-validation
- https://json-schema.org/understanding-json-schema/reference/conditionals
- https://json-schema.org/understanding-json-schema/reference/object
- https://ajv.js.org/json-schema.html
- https://ajv.js.org/options.html
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.