JSON GUIDE · 4 MIN READ

Validate a contract with JSON Schema

Define required properties and value constraints, then interpret validation errors.

Syntax is only the first check

A document can be valid JSON yet wrong for your application. A schema describes rules such as object shape, required properties, string lengths, numeric limits, and allowed values. JSONSlate's schema validator supports Draft 7, so declare that dialect when writing your schema.

The properties keyword defines checks for named fields; it does not by itself require them. Put mandatory field names in required. Additional properties are allowed unless you constrain them, so close an object deliberately rather than assuming a list of properties does so.

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {"age": {"type": "integer", "minimum": 0}},
  "required": ["age"]
}

Design from more than one example

A generated schema is a starting point. A sample with age 31 does not establish a maximum age, and a sample with an empty list does not reveal the intended item type. Review optional fields and nullable values with the people who own the contract.

Validate good and intentionally bad examples. An error path points into the instance; the failed keyword explains the constraint. Keep references self-contained for private local validation: JSONSlate does not silently fetch remote schemas. The format keyword is an annotation here, so it does not enforce an email or date format.

Put it into practice

Further reading

← All JSON guides