Ajv
By the Ajv open-source team
Ajv is a JSON Schema validator for JavaScript that checks whether a given piece of data conforms to a JSON Schema definition, compiling schemas into highly optimized validation functions for speed. It supports multiple JSON Schema draft…
Definition
Ajv is a JSON Schema validator for JavaScript that checks whether a given piece of data conforms to a JSON Schema definition, compiling schemas into highly optimized validation functions for speed. It supports multiple JSON Schema draft versions and can be extended with custom keywords and formats. Ajv is widely used as the underlying validation engine inside other tools, including API frameworks and schema-driven form libraries, rather than only as a standalone library developers call directly.
Overview
Ajv exists to validate data against the JSON Schema specification, an open standard for describing the structure of JSON documents, and to do so as fast as possible. Many JavaScript projects need to check that incoming data, whether an API request body, a configuration file, or a message on a queue, matches an expected structure, and JSON Schema provides a portable, language-independent way to describe that structure once and validate against it from many tools. What distinguishes Ajv mechanically from many validators is that it compiles each JSON Schema into a dedicated JavaScript validation function ahead of time, rather than interpreting the schema at validation time. This compile-then-execute approach is significantly faster for repeated validations, such as validating every incoming HTTP request against the same schema, because the cost of interpreting the schema is paid once rather than on every call. Ajv supports several JSON Schema draft versions, custom keyword extensions, format validators for things like email addresses and dates, and can generate human-readable error messages describing exactly which part of the data failed. Within the Node.js validation landscape, Ajv's defining trait is standards compliance: because it implements JSON Schema rather than a bespoke schema language, schemas written for Ajv can be shared with other JSON-Schema-aware tools, such as OpenAPI documentation generators, whereas schemas written in Joi's or Zod's own syntax cannot be used directly outside those libraries. This makes Ajv the natural choice when validation needs to interoperate with an existing JSON Schema, as opposed to when a project is defining its own validation rules from scratch. In practice, Ajv is used directly for API request and response validation, and it is embedded inside larger tools: it powers schema validation in Fastify, is used by AJV-based OpenAPI validators, and appears inside build tools and linters that validate configuration files against a published schema. Its speed makes it a common choice wherever validation happens on a hot path, such as every incoming request to a high-traffic service. The trade-off for that speed and standards compliance is that JSON Schema itself can be verbose and less ergonomic to write by hand than the fluent, chainable APIs of Joi or Zod, and Ajv does not provide TypeScript type inference from schemas the way Zod does. Teams that value developer ergonomics over raw interoperability, or that want compile-time types derived from their schemas, often choose a schema-first TypeScript library instead and reserve Ajv for cases where JSON Schema compliance or maximum validation speed is the priority.
Key Features
- Compiles JSON Schemas into optimized JavaScript validation functions
- Supports multiple JSON Schema draft specification versions
- Extensible with custom keywords and format validators
- Detailed, structured error output describing validation failures
- Used internally by frameworks like Fastify for request validation
- Enables schema reuse across tools that also support JSON Schema