ajv
Ajv (Another JSON Schema Validator) is the fastest JSON Schema validator for JavaScript and TypeScript, implementing the JSON Schema specification (drafts 04, 0…
Installation
npm install ajv
yarn add ajv
pnpm add ajv
Import
import Ajv from 'ajv';
Quick Example
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
const ajv = new Ajv();
addFormats(ajv);
const schema = {
type: 'object',
properties: {
name: { type: 'string', minLength: 1 },
email: { type: 'string', format: 'email' },
age: { type: 'integer', minimum: 0 },
},
required: ['name', 'email'],
};
const validate = ajv.compile(schema);
const valid = validate({ name: 'Alice', email: '[email protected]' });About ajv
Ajv (Another JSON Schema Validator) is the fastest JSON Schema validator for JavaScript and TypeScript, implementing the JSON Schema specification (drafts 04, 06, 07, 2019-09, and 2020-12) with full compliance and exceptional performance. Ajv compiles JSON Schema into highly optimized validation functions at initialization time, making subsequent validations extremely fast — this compilation approach makes Ajv suitable for high-throughput scenarios like API request validation where the same schema is validated millions of times. The library supports all JSON Schema keywords including $ref for schema composition, if/then/else for conditional validation, allOf/anyOf/oneOf for combining schemas, pattern for regex validation, and format for built-in format checks (email, uri, date-time, uuid). Ajv provides plugins for additional formats (ajv-formats), custom keywords for extending the schema vocabulary, asynchronous validation for custom async checks, and schema coercion for automatic type conversion. The library generates human-readable error messages with full JSON Pointer paths to invalid data. Ajv is used internally by many tools and frameworks including ESLint, webpack, and various API frameworks for configuration and request validation. Ajv's JSON Schema compliance makes schemas portable across languages and tools that support the JSON Schema standard.
Quick Facts
| Package | ajv |
| Category | Validation |
| Weekly Downloads | 60M+ |
| License | MIT |
| Install | npm install ajv |
Related Packages
Zod is a TypeScript-first schema validation library that provides a concise, chainable API for defin…
Joi is the most powerful schema description language and data validator for JavaScript, originally c…
Yup is a schema validation library for JavaScript that provides a declarative, chainable API for def…
TypeBox is a JSON Schema type builder that creates in-memory JSON Schema objects that can be statica…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.