JSON Schema Validation in Practice
How to validate API requests and configuration with JSON Schema — in JavaScript, Python, and CI.
Published:
Tags: JSON Schema validation, validate JSON schema, API contract validation
JSON Schema Validation in Practice The JSON Schema specification is maintained at json-schema.org and covers drafts 04, 07, 2019-09, and 2020-12. The IETF Internet-Draft for JSON Schema is the working standard. The ajv library documentation is the primary reference for JavaScript validators. JSON Schema is downloaded over 50 million times monthly and validates over 100 billion API requests per day globally. A JSON Schema describes what valid data looks like. A validator enforces that description at runtime — catching bad API requests, malformed config files, and third-party webhook payloads before they cause downstream errors. --- Why runtime validation matters? TypeScript types disappear at compile time. A schema persists at runtime: Without runtime validation, a user can send and your…
Frequently Asked Questions
How do I validate JSON with a schema?
In JavaScript use ajv: compile your schema with new Ajv().compile(schema), then call the compiled validator with your data — it returns true or false and populates validator.errors with details. In Python use jsonschema.validate(instance, schema), which raises ValidationError on failure.
What is the ajv library for JSON Schema?
ajv (Another JSON Schema Validator) is the most widely used JSON Schema validator for JavaScript and Node.js. It pre-compiles schemas into optimized JavaScript functions for fast validation. It supports JSON Schema draft-04, draft-07, draft-2019-09, and draft-2020-12, plus optional formats like email and date-time.
How do I validate API request bodies?
In an Express app, create a middleware that validates req.body against your schema using ajv before passing to the route handler. If validation fails, return 400 with the error messages. In Fastify, schema validation is built-in — declare a schema on the route and Fastify rejects invalid requests automatically.
How do I add JSON Schema to a CI pipeline?
Add a validation step that runs your schema tests before deployment. With Jest/Vitest, write tests that load fixture JSON files and validate them against their schemas. With CLI tools like ajv-cli, run npx ajv validate -s schema.json -d data.json as a build step. Any validation failure should fail the pipeline.
What is OpenAPI vs JSON Schema?
OpenAPI (formerly Swagger) is a full API description format that uses JSON Schema (or a subset) for request/response body definitions. JSON Schema is the vocabulary for describing data shapes; OpenAPI uses it as a building block alongside HTTP methods, parameters, authentication, and server definitions. OpenAPI 3.1 uses JSON Schema 2020-12 natively.
All articles · theproductguy.in