Registry / testing / ajv
library8.20.0jsnpmunverified

Ajv is the fastest JSON Schema validator for Node.js and browser, supporting JSON Schema drafts 04/06/07/2019-09/2020-12 (draft-04 requires ajv-draft-04) and JSON Type Definition RFC8927. Current stable version is 8.20.0, released with monthly cadence. It is widely used, highly performant, and strictly validates data against schemas. Key differentiators include speed, strict mode, standalone code generation, and full support for the latest JSON Schema specifications. It ships with TypeScript types and provides a comprehensive API for validation, compilation, and error handling.

npm install ajv
INSTALL
IMPORT
SIG · AJV
A
ajv
testingjavascriptv8.20.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Ajv
import Ajv from 'ajv'
const Ajv = require('ajv').default
Default export is the Ajv class. In CommonJS, use require('ajv').default or the whole module.
ValidateFunction
import Ajv, { ValidateFunction } from 'ajv'
const { ValidateFunction } = require('ajv')
Used for typing compiled validation functions.
ErrorObject
import type { ErrorObject } from 'ajv'
import { ErrorObject } from 'ajv'
Use 'import type' in TypeScript for type-only imports to avoid runtime inclusion.

Compiles a JSON Schema and validates a simple object. Demonstrates compile, validate, and error handling.

import Ajv from 'ajv'; const ajv = new Ajv({ allErrors: true }); const schema = { type: 'object', properties: { foo: { type: 'integer' }, bar: { type: 'string' } }, required: ['foo'], additionalProperties: false }; const validate = ajv.compile(schema); const valid = validate({ foo: 1, bar: 'abc' }); if (!valid) console.log(validate.errors);
Debug
Known issues
breakingAjv v8 drops support for JSON Schema draft-04 by default. Use ajv-draft-04 package for draft-04 schemas.
fix
Install ajv-draft-04 and use new Ajv() with { schemaId: 'id' } or use standalone validator.
affects: >=8.0.0
breakingundefined keyword no longer allowed in schema; strict mode throws by default.
fix
Use ajv.addKeyword to add custom keywords, or disable strict mode with { strict: false }.
affects: >=8.0.0
deprecatedajv.errorsText() is deprecated and removed in v8. Use schema.errors to get errors array.
fix
Manually build error text from validate.errors using map/join.
affects: >=8.0.0
gotchatype coercion (e.g., string to number) requires enabling coerceTypes option and applying coerce rules via ajv.addFormat or coerce plugin.
fix
Use ajv.addFormat to define coercion logic, or use ajv-keywords with mod plugin.
affects: <=8.0.0
Errors
Common errors & fixes
Cannot find module 'ajv/dist/2020'
Incorrect import path for JSON Schema 2020-12 support; the correct path is 'ajv/dist/2020' (not 'ajv/2020').
fix
Use import Ajv2020 from 'ajv/dist/2020' (or 'ajv/2020' with proper alias in tsconfig).
strict mode: unknown keyword "if"
The keyword 'if' is from JSON Schema draft-07 and not recognized in strict mode without schema definition.
fix
Set { strict: false } or use a schema with $schema pointing to a draft that supports 'if'.
Schema is not valid: data/type must be string, array, or object
The 'type' keyword expects a string or array, but an invalid value was provided.
fix
Correct the schema: 'type' should be a string like 'object' or an array like ['string', 'null'].
Upgrade
Version history
8.20.0latest on npm
Audit
Dependencies
ajv-formatsoptionalProvides format keywords (date-time, uri, email, etc.) not built into the main Ajv package.
ajv-draft-04optionalRequired for JSON Schema draft-04 support.
ajv-keywordsoptionalProvides additional validation keywords (uniqueItemProperties, etc.).
Agent activity
56 hits · last 30 days
node
48
OpenAI (training)
1
Resources
packageajv
ajv — npm install ajv · libregistry