JSON Schema Intro: Validate JSON Structure Like SQL Constraints

2024-04-13

Valid syntax is not enough—fields can still be wrong. JSON Schema describes allowed shape for runtime or CI checks.

Minimal schema

Root type object or array; properties + required list fields.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["id", "name"],
  "properties": {
    "id": { "type": "integer" },
    "name": { "type": "string", "minLength": 1 }
  }
}

Common keywords

type, enum, items, additionalProperties: false, oneOf, $ref.

In code

JS: ajv; Python: jsonschema; Go: gojsonschema. OpenAPI bodies are JSON Schema subsets.

Vs syntax check

JSON.parse first, schema second—separate syntax errors from contract violations.

Team rollout

Schema in repo; CI validates fixtures; API changes update schema before code.