json schemajson validationapi validationdata validationdeveloper tools

JSON Schema Validator: Catch Data Errors Before They Reach Production

Learn how to use JSON Schema to define the shape of your data and validate JSON documents at development time, in CI, and at API boundaries.

9 min read

Related Tool

JSON Schema Validator

Open tool

JSON is everywhere — API responses, configuration files, database exports, event payloads. The problem with JSON is that it has no built-in type system. A field that should be a number arrives as a string. A required field is missing. An array contains an unexpected null. These issues reach production, cause runtime errors, and take time to debug.

JSON Schema is the answer. It is a vocabulary for describing the structure and constraints of JSON data, and a JSON Schema validator checks a document against a schema and reports exactly which fields violate which rules.

What is JSON Schema?

JSON Schema is a JSON document that describes another JSON document. A schema can specify:

  • Typestring, number, integer, boolean, array, object, null
  • Required fields — which properties must be present
  • String constraintsminLength, maxLength, pattern (regex)
  • Number constraintsminimum, maximum, multipleOf
  • Array constraintsminItems, maxItems, items (schema for each element)
  • Object constraintsproperties, additionalProperties, patternProperties
  • CompositionallOf, anyOf, oneOf, not for combining schemas

A simple schema for a user object looks like this:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["id", "email", "role"],
  "properties": {
    "id": { "type": "integer", "minimum": 1 },
    "email": { "type": "string", "format": "email" },
    "role": { "type": "string", "enum": ["admin", "user", "viewer"] },
    "name": { "type": "string", "maxLength": 100 }
  },
  "additionalProperties": false
}

This schema says: the object must have id, email, and role. The id must be a positive integer. The email must be a string in email format. The role must be one of three specific values. No extra properties are allowed.

Why Use a JSON Schema Validator?

Catch API contract violations early

When your service consumes an external API, the API provider may change their response shape without warning. Running incoming payloads through a schema validator surfaces these changes immediately instead of letting them cause silent failures downstream.

Document your data contracts

A JSON Schema is machine-readable documentation. Unlike a comment or a README, a schema can actually be enforced. When a new team member asks "what does this event payload look like?", point them to the schema.

Validate configuration files

Configuration files like package.json, tsconfig.json, and custom application config all have specific shapes. A schema validator catches typos in field names, wrong value types, and missing required settings before the application tries to use the config.

Gate data in CI pipelines

Add schema validation to your CI pipeline to ensure test fixtures, mock data, and seed files stay consistent with your data contracts as your schema evolves.

Using the DevHexLab JSON Schema Validator

The validator gives you a two-panel interface: paste your schema on the left and your JSON document on the right. Click Validate and the tool reports errors with full JSON Pointer paths so you know exactly which field failed and why.

Reading validation errors

Errors include the path, the failing keyword, and a human-readable message. For example:

  • /role: must be equal to one of the allowed values — the role field has a value not in the enum
  • /email: must match format "email" — the email string does not look like an email address
  • must have required property 'id' — a required field is missing at the root

Draft versions

JSON Schema has several draft versions: draft-04, draft-07, 2019-09, and 2020-12. The DevHexLab validator supports all major drafts. Specify the draft in the $schema field of your schema to ensure the correct dialect is used.

Common Schema Patterns

Nullable fields

In draft 2020-12, use type: ["string", "null"] to allow a field to be either a string or null.

Conditional validation

Use if/then/else to apply different validation rules depending on another field's value:

{
  "if": { "properties": { "type": { "const": "premium" } } },
  "then": { "required": ["billingId"] }
}

Reusable definitions

Use $defs to define sub-schemas once and reference them with $ref:

{
  "$defs": {
    "address": {
      "type": "object",
      "required": ["street", "city"],
      "properties": {
        "street": { "type": "string" },
        "city": { "type": "string" }
      }
    }
  },
  "properties": {
    "billingAddress": { "$ref": "#/$defs/address" },
    "shippingAddress": { "$ref": "#/$defs/address" }
  }
}

Conclusion

JSON Schema validation is one of the cheapest quality improvements you can add to a project. A few minutes writing a schema saves hours of debugging malformed data. Use the DevHexLab JSON Schema Validator to iterate on your schemas quickly in the browser before integrating them into your codebase.