In modern software architecture, services communicate via JSON. But how do you ensure that Service A sends the data Service B expects? If the "age" field is meant to be a number, but creates a string "twenty", the application might crash.

JSON Schema is the solution. It is a declarative language (itself written in JSON) that allows you to annotate and validate JSON documents. Think of it as "Type Checking for JSON".

The JSON Schema Validator allows you to paste your Data (left) and your Schema (right) to instantly verify compliance. It highlights errors down to the specific property violation.

Supported Draft Versions

JSON Schema has evolved over time. This tool is built on the industry-standard AJV library and supports:

  • Draft 7 (Most popular)
  • Draft 4 (Legacy)
  • Draft 2019-09
  • Draft 2020-12 (Latest)

Core Keywords Explained

1. Type Validation

"type": "string" | "number" | "boolean" | "object" | "array" | "null"

Enforces the primitive data type.

2. Required Properties

"required": ["id", "email"]

Ensures critical fields are present. Missing fields trigger a validation error.

3. Pattern (Regex)

"pattern": "^[a-z]+$"

Validates strings against regular expressions (e.g., enforcing email format, dates, or phone numbers).

4. Enumerated Values

"enum": ["red", "green", "blue"]

Restricts the value to a specific set of options.

Real-World Example: User Profile

Schema:

{
  "type": "object",
  "properties": {
    "name": { "type": "string", "minLength": 2 },
    "age": { "type": "integer", "minimum": 0 },
    "role": { "enum": ["admin", "user"] }
  },
  "required": ["name", "role"]
}
    

Valid Data ✅:

{"name": "Alice", "age": 30, "role": "admin"}

Invalid Data ❌:

{"age": -5, "role": "superuser"}

Errors: "age" must be >= 0; "role" must be equal to one of the allowed values.

Why Use JSON Schema?

  • Automated Testing: Integate validation into your CI/CD pipeline to catch breaking API changes.
  • Form Generation: Libraries like react-jsonschema-form can automatically build UI forms based on your schema.
  • Documentation: A well-written schema serves as unambiguous documentation for API consumers.