2025-06-07
JSON Schema: The Contract Between Your Frontend and Backend
JSON Schema is more than documentation — it's a runtime contract that catches API drift before it reaches production. Here's how to use it effectively.
What Is JSON Schema?
JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. Think of it as a type system for JSON that works at runtime, not just compile time.
{
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string", "format": "email" }
},
"required": ["id", "email"],
"additionalProperties": false
}
Why additionalProperties: false Matters
This is the single most valuable line in any JSON Schema. Without it, your schema is a suggestion. With it, it's a contract — any key not in properties causes validation to fail.
This catches the most common API drift scenario: the backend adds a new required field and the frontend silently ignores it until something breaks at runtime.
Generating Schemas From Real Data
The fastest way to write a JSON Schema is to start from real API responses. Paste a real response into our converter and get a draft schema instantly. Then:
- Review the
requiredarray — remove fields that are truly optional - Add
"format"hints for strings:"date-time","email","uuid" - Add
"minimum"/"maximum"for numbers where business logic applies - Set
additionalProperties: falseonce the shape is stable
Validation Libraries
| Language | Library |
|---|---|
| JavaScript/TypeScript | ajv, zod (with .parse) |
| Python | jsonschema, pydantic |
| Go | gojsonschema |
| Java | networknt/json-schema-validator |
| Rust | jsonschema crate |
Schema vs Zod vs TypeScript
These tools are complementary, not competing:
- TypeScript catches type errors at compile time in your codebase
- Zod validates at runtime at your application boundary (API calls, form inputs)
- JSON Schema validates anywhere, in any language, and is the standard for API documentation (OpenAPI uses it)
Use all three for maximum coverage.