Validation

JSON Schema Validation: Build a Reliable API Contract

Build a JSON Schema for an API payload, understand required fields and extra properties, and test meaningful failures without confusing syntax with validation.

THE KEY IDEA

A schema makes structural expectations executable. Test it with failing examples as well as successful ones, and keep business rules separate.

Valid JSON is only the first check

Suppose your API expects an order with an identifier, a nonnegative quantity, and a status. Both {"quantity":2} and {"quantity":"two"} can be valid JSON. Only one might fit your API contract, and both could be incomplete orders.

JSON Schema expresses expectations about the data, such as types, required properties, ranges, and allowed values. Use it to make assumptions visible to producers and consumers. A schema is especially helpful when several applications depend on the same payload and a casual field change could break another team’s code.

Keep the boundary clear: a schema can check that quantity is a nonnegative integer, but it cannot establish that a warehouse has enough stock or that the caller is authorized to place the order. Those checks still belong in the application.

Write a small contract before generating a large one

This original draft-07 schema describes a deliberately small order. properties defines constraints for named fields, while required says which fields must be present. Listing a field in properties alone does not require it. An enum limits status to the declared values.

The schema rejects unknown fields through additionalProperties: false. That can catch a typo such as quanitity, but it also rejects a newly added field from a future producer. Choose that policy consciously: strict input boundaries and evolving response consumers may need different compatibility rules.

Example JSON Schema draft-07 contract
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": ["orderId", "quantity", "status"],
  "properties": {
    "orderId": {"type": "string", "minLength": 1},
    "quantity": {"type": "integer", "minimum": 0},
    "status": {"enum": ["pending", "shipped"]}
  },
  "additionalProperties": false
}

References: JSON Schema: object properties, required, and additional properties · JSON Schema: enumerated values

Test one failure at a time

Start with the valid order below. Then remove orderId, change quantity to a string, try a negative quantity, use an unknown status, and add an unexpected field. Each mutation checks a specific rule. If one unexpectedly passes, investigate the schema or validator configuration rather than assuming the example proves correctness.

Missing and null are different cases. A required orderId set to null is present, but still fails this schema’s string constraint. If null is a real business state, model it explicitly and describe what it means. Avoid broadening types merely to make every fixture pass.

In JSONSlate, put the order in the main input and the schema in the secondary document of the JSON Schema Validator. Run validation and inspect the returned paths and keywords. The result remains a preview; validation does not repair the order or silently insert missing fields.

A valid order for the example schema
{
  "orderId": "ord-0042",
  "quantity": 2,
  "status": "pending"
}

Check the schema draft and validator settings

A schema is interpreted by a validator with a particular dialect and configuration. JSONSlate uses draft-07 through Ajv, does not fetch remote schema references, and treats format as an annotation. Local fragment references are supported. A format label such as email therefore does not enforce email syntax in this tool.

The JSON Schema documentation explains that format behavior can depend on the validator. A successful local result is not evidence that every production validator uses the same configuration. Match the draft and settings in your application’s automated tests, especially when adding date, email, or custom format rules.

For reusable local definitions, keep them inside the schema and refer to them with a fragment such as #/definitions/orderId. That avoids a network dependency during local validation. If your real contract relies on externally hosted schemas, assemble the references through your own controlled tooling before testing it here.

References: JSON Schema: string format and validation behavior

Evolve the contract with your consumers

Schema inference from a sample can provide a starting point, but a sample does not reveal every optional field or legal value. One observed order cannot tell you whether quantity zero is permitted or whether another status will appear next month. Review generated schemas against the actual business contract.

Keep a small collection of accepted and rejected fixtures alongside the schema. When changing it, run both old and new examples through the validator used in production. Describe which consumers must change and when. The useful outcome is a dependable agreement between applications, not just a green validation result for today’s sample.

Put it into practice

Explore the examples in your own browser. Core JSON processing stays on your device.

Continue learning: JSON Schema quick guide.

All articles