JSON Schema Validation

Practical JSON Schema examples for required fields, types, enums, arrays, nested objects, and validation workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Schema basics

Use JSON Schema to describe allowed structure and values.

Basic object schema

Require a string name and boolean active flag.

jsonANYjsonschemaobject
json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "active": { "type": "boolean" }
  },
  "required": ["name", "active"],
  "additionalProperties": false
}
Notes

A good baseline for validating a small payload shape.

Enum restriction

Restrict a value to a known set of strings.

jsonANYjsonschemaenum
json
{
  "type": "object",
  "properties": {
    "status": {
      "type": "string",
      "enum": ["draft", "published", "archived"]
    }
  },
  "required": ["status"]
}
Notes

Enums are useful for states, roles, and constrained option lists.

Array item validation

Validate every item in an array.

jsonANYjsonschemaarrays
json
{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": { "type": "integer" },
      "email": { "type": "string", "format": "email" }
    },
    "required": ["id", "email"]
  }
}
Notes

Use `items` to define the shape of each array element.

Schema validation workflows

Run JSON Schema validation from common tooling.

Validate with Ajv CLI

Validate a JSON file against a schema file.

bashANYjsonschemaajvvalidation
bash
npx ajv-cli validate -s schema.json -d data.json
Notes

A convenient Node-based workflow for JSON Schema validation.

Validate multiple files with Ajv

Validate a whole set of JSON documents.

bashANYjsonschemaajvci
bash
npx ajv-cli validate -s schema.json -d 'fixtures/*.json'
Notes

Useful in CI or fixture validation pipelines.

Recommended next

No recommendations yet.