Guide · Open source
How to use ajv: install it and validate JSON with schemas
Updated · written with AI from the project's own documentation and checked against our catalogue data
Ajv is a JavaScript and TypeScript library that checks JSON data against a schema, for example to confirm that an API request or form submission has the required fields and types. It runs on Node.js and in the browser. This guide shows how to install it, what it supports, and where to find the detailed documentation.
Quick start
- 1Check Node.js is installed. Ajv requires Node.js, so confirm it is available in your environment before you start.
- 2Install the ajv package. Run npm install ajv in your project folder to add Ajv as a dependency.
- 3Decide which schema draft you use. Ajv supports drafts 06, 07, 2019-09 and 2020-12 directly. If your schemas use draft-04, add the ajv-draft-04 package.
- 4Write a JSON Schema. Describe the fields, types and required properties your data must have.
- 5Follow the Getting started guide. Use the official guide at ajv.js.org to wire your schema into your code and validate data.
- 6Read the strict mode and security pages. Review the strict mode and security considerations documentation before using Ajv with untrusted input.
What Ajv does and who it is for
Ajv takes a JSON Schema, which is a description of the shape your data should have, and checks whether a piece of JSON data matches it. The README describes it as the fastest JSON validator for Node.js and browsers.
It suits Node.js developers validating API request or response bodies, front-end developers validating form or config data in the browser, and teams already writing JSON Schema who want a standards-based checker.
- Supports JSON Schema draft-04, 06, 07, 2019-09 and 2020-12
- Supports JSON Type Definition (RFC 8927)
- Draft-04 support requires the separate ajv-draft-04 package
What you need before installing Ajv
The only prerequisite listed in the README is Node.js. Ajv is installed with npm, the package manager that comes with Node.js.
The README only mentions Node.js and browsers as targets. If your project needs a different runtime, check the docs before committing to Ajv.
- Node.js installed on your machine or build environment
- npm available on the command line
Install Ajv with npm
Run the following command in your project folder to add the ajv package, as given in the README.
npm install ajvWrite a schema and validate data
Ajv works by taking a schema and generating validation code from it, which the README says is how it reaches its speed. You describe the required fields and their types in a JSON Schema, then use Ajv to check incoming data against that schema.
For step-by-step code, the official Getting started guide on the Ajv website is the right place to go. This page does not repeat API examples, so you do not have to rely on memorised syntax.
- Read the Getting started guide at ajv.js.org/guide/getting-started.html
- Read the API reference at ajv.js.org/api.html
Use draft-04 schemas and other drafts
Ajv supports drafts 06, 07, 2019-09 and 2020-12 directly. If your existing schemas use draft-04, install the separate ajv-draft-04 package, as the README notes.
The README also links to a page on JSON Schema vs JSON Type Definition, so check it if you are choosing between the two schema languages.
- Draft-04 needs the ajv-draft-04 package
- The schema language guide is at ajv.js.org/guide/schema-language.html
Learn the advanced features in the docs
The README lists several topics that are covered in the documentation. Read these when you move beyond basic validation.
Strict mode, standalone validation code, the command line interface and security considerations each have their own page on the Ajv website.
- Strict mode: ajv.js.org/strict-mode.html
- Standalone validation code: ajv.js.org/standalone.html
- Command line interface (ajv-cli): ajv.js.org/packages/ajv-cli.html
- Security considerations: ajv.js.org/security.html
What people use it for
- Checking that an API request body has the required fields and types before processing it
- Validating form or configuration data in a browser application
- Checking API responses against a schema in a Node.js service
- Using a standards-based JSON Schema checker across a team that already writes JSON Schema
Frequently asked questions
How do I install ajv?
Run npm install ajv in your project folder. The README gives this as the install command. Node.js is the prerequisite.
Is ajv free to use?
Ajv is released under the MIT licence, as listed on its project page. Check the licence file in the repository for the full terms.
Does ajv need Node.js?
The README lists Node.js as the prerequisite and says Ajv is for Node.js and browsers. It does not mention other runtimes, so check the docs if you need one.
Why does ajv not support draft-04 by default?
Draft-04 support is provided by the separate ajv-draft-04 package, according to the README. Install that package if your schemas use draft-04.
What are the alternatives to ajv?
The project lists zod, joi, is-my-json-valid and @exodus/schemasafe as comparable projects. Pick based on whether you already write JSON Schema.
Is ajv faster than other validators?
The README says Ajv is the fastest and most standard-compliant validator and links several benchmarks. Those benchmarks were not verified for this guide, so read them directly before relying on the claim.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
Ajv is a good fit for Node.js and browser developers who already write JSON Schema or want a standards-based checker for API and form data. If you are new to programming and want a simple form library, or you only need to check a few values by hand, a simpler tool such as a plain function or a higher-level library like zod may suit you better.
Install options & downloads