# Ajv — card for AI agents

Fast JSON Schema validator for runtime schema validation and serialization.

Category: Forms, validation and schemas · npm package `ajv` · latest version 8.20.0 (checked 2026-11-01)
Install: `npm install ajv`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (paste into AGENTS.md or CLAUDE.md)

## Ajv
- Compile each schema only once and reuse the compiled validation function. The docs say compilation is relatively slow and that the best performance comes from functions returned by `compile` or `getSchema`.
- Copy the `validate.errors` array reference to another variable if you need it later. Every validation call overwrites it.
- `properties` alone does not require any keys or an object type. Use `required` for keys and `type` for the type.
- `additionalProperties: false` only considers `properties` and `patternProperties` in the same schema object. Properties declared inside `oneOf`/`anyOf` subschemas count as additional.
- Add the `ajv-formats` plugin for `format` keywords. Add remote schemas with `addSchema` or compile them so `$ref` can resolve them.
- Compiled JTD parsers return `undefined` instead of throwing. Check the return value and read the parser's `message` and `position` properties.
- Do not put validated data into logged error messages. Take property names from the error's `params` object when you build your own messages.

## What it is
- Ajv is a JSON validator for Node.js and the browser. It is published as `ajv` on npm under the MIT license. The GitHub repository was created on 2015-05-19. The latest listed release is v8.20.0, published 2026-04-24, which adds support for Node 22 and 24. Ajv supports JSON Schema drafts 04, 06, 07, 2019-09 and 2020-12, plus JSON Type Definition (RFC8927).
- You give Ajv a schema, and `ajv.compile` returns a function. Call that function on your data and it returns `true` or `false`. When it returns `false`, the reasons are on the function's `errors` property. The docs describe the compile step as converting the schema into "a very efficient JavaScript code".
- The project site says Ajv runs in Node.js, browsers, Electron apps and WeChat mini-apps. It pitches the library as a way to put validation logic in declarative schemas "without writing code", so data is checked once it is received.

## Caveats
- Compiling is the slow part: Running a compiled validator is fast, but building one is not. The docs tell you to compile each schema once and reuse the function. Ajv caches compiled functions and uses the schema object as the key.
- `errors` is overwritten on every call: The next validation replaces the `errors` property. If you need the errors later, for example in a callback, copy the array reference to another variable first.
- No errors-returning method is coming: The FAQ says returning a boolean is a deliberate choice, made for history and performance. The maintainers answer "No" to adding a method that returns errors. They suggest putting any API you prefer in your app's own validation module.
- Compiled parsers return `undefined` and do not throw: Unlike `JSON.parse`, a compiled JTD parser does not throw on bad input. It returns `undefined` and reports the problem through the parser's `message` and `position` properties.
- `additionalProperties` ignores subschemas: Properties declared inside `oneOf` or `anyOf` branches still count as additional. The FAQ stresses that the standard requires this, so it is not an Ajv limitation. The same rule causes failures when you use `removeAdditional` with these keywords.

## Numbers (AgentGid data)
- Gid Score: 77.6 (#141 of all tools, 4 of 63 in its category)
- Used in 154 public projects set up for AI coding agents (2.5%)
- Downloads last week: 546,687,013 (as of 2026-11-24), +37.5% over 4 weeks
- GitHub stars: 14,885 (as of 2026-11-24)
- Last push: 2026-09-06

## Often used with
ajv-formats 37.7%, yaml 31.8%, Commander 30.5%, ws 29.9%, chalk 28.6%, Express 27.9%

## Checked alternatives (same job)
- Zod: https://agentgid.com/tools/zod/agent.md
- Valibot: https://agentgid.com/tools/valibot/agent.md
- TypeBox: https://agentgid.com/tools/sinclair-typebox/agent.md
- Yup: https://agentgid.com/tools/yup/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/ajv/
- Homepage: https://ajv.js.org
- Repository: https://github.com/ajv-validator/ajv
- Source: https://github.com/ajv-validator/ajv#readme
- Source: https://github.com/ajv-validator/ajv
- Source: https://github.com/ajv-validator/ajv/releases
- Source: https://ajv.js.org
- Source: https://ajv.js.org/guide/getting-started.html
- Source: https://ajv.js.org/faq.html


## Ajv: check JSON data against a schema in JavaScript

Ajv is a free JavaScript library that checks whether incoming data has the shape you expect, such as a sign-up form or a request to your server. You describe the rules once in a JSON Schema file. Ajv turns them into a fast check and tells you exactly which field is wrong and why.

### Where to use it
- Check what reaches your server: Before your app saves a new order or sign-up, it checks the data against one set of rules and refuses anything with missing or wrong fields.
- Show every form mistake at once: A sign-up form lists all problems together, such as a bad email and a too-young age, instead of one at a time.
- Fill in defaults, drop extras: When a request leaves out the plan, it becomes "free"; fields you never asked for, like an admin flag, are removed before you save.
- Check JSON from an AI model: When you ask an AI model for JSON, check the answer against your rules before your app uses it, and ask again if it fails. (idea, not from the official pages)
- Strict pages without eval: If your site's security settings block code built on the fly, compile the checks while you build the site and ship them as plain files.
- Catch typos in settings files: Check a settings file people edit by hand when your tool starts, and name the line that is wrong instead of failing later. (idea, not from the official pages)

### How to start
- Install version 8 into your project (the older draft-04 schemas need version 6).

```
npm install ajv
```

- Compile a schema once, then call the returned function on each piece of data; read validate.errors when it returns false.

```
// or ESM/TypeScript import
import Ajv from "ajv"
// Node.js require:
const Ajv = require("ajv")

const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}

const schema = {
  type: "object",
  properties: {
    foo: {type: "integer"},
    bar: {type: "string"},
  },
  required: ["foo"],
  additionalProperties: false,
}

const data = {
  foo: 1,
  bar: "abc",
}

const validate = ajv.compile(schema)
const valid = validate(data)
if (!valid) console.log(validate.errors)
```

- Add ajv-formats if your schema uses formats such as email, date or uri.

```
npm install ajv-formats
```


### License
Free to use in any project, including paid and closed-source ones, under the MIT license. Keep the license notice when you ship Ajv's own files.

### Good to know
- Ajv treats a schema like your own program code. Do not run schemas that strangers send you without limits on size and depth.
- Some rules, like text patterns on very long strings, can make a check slow. Add length limits and keep the "all errors" mode out of live servers.
- In a web page with a strict security policy, Ajv's on-the-fly checks need 'unsafe-eval'; checks compiled ahead of time avoid that.
- Email, date and similar formats are not built in; they come from the separate ajv-formats package.
- The newest schema standard (2020-12) needs its own Ajv class; you cannot mix it with older schema versions in one instance.

Checked 2026-11-10 against: https://ajv.js.org/, https://ajv.js.org/guide/why-ajv.html, https://ajv.js.org/guide/getting-started.html, https://ajv.js.org/guide/formats.html, https://ajv.js.org/guide/modifying-data.html, https://ajv.js.org/guide/managing-schemas.html, https://ajv.js.org/guide/environments.html, https://ajv.js.org/json-schema.html, https://ajv.js.org/strict-mode.html, https://ajv.js.org/security.html, https://ajv.js.org/v6-to-v8-migration.html, https://ajv.js.org/options.html, https://github.com/ajv-validator/ajv/blob/master/README.md, https://github.com/ajv-validator/ajv/blob/master/LICENSE, https://github.com/ajv-validator/ajv/releases, https://registry.npmjs.org/ajv, https://registry.npmjs.org/ajv/8.20.0

### Mistakes to avoid
- Don't: new Ajv() for a schema that uses draft 2020-12 keywords like prefixItems → Do: const Ajv2020 = require("ajv/dist/2020"); new Ajv2020() (The default class is for draft-07; in our run it threw "strict mode: unknown keyword: prefixItems", and Ajv2020 accepted the same schema.)
- Don't: {type: "string", format: "email"} with a bare new Ajv() → Do: const addFormats = require("ajv-formats"); addFormats(ajv) before compiling (Formats moved out of Ajv in version 7; in our run compile threw unknown format "email".)
- Don't: Reading err.dataPath from validate.errors (Ajv 6 code and old answers online) → Do: Read err.instancePath (for example "/email"); messages now say "must" instead of "should" (Ajv 8 renamed the error field, so old code silently gets undefined.)
- Don't: Calling ajv.compile(schema) inside every request handler → Do: Compile each schema once at startup and reuse the validate function (Compiling is the slow part; running a compiled check is fast.)
- Don't: Keeping a reference to validate.errors and reading it after the next validate() call → Do: Copy the errors array right away if you need it later (Each call overwrites the errors property.)

### Questions
- Can I use Ajv in a paid, closed-source app? Yes. It is MIT licensed; keep its copyright and license notice in copies you ship.
- Does it work in the browser? Yes, in browsers and in Node.js 18 and newer. Pages with a strict security policy should ship checks compiled ahead of time.
- Why does my schema with format "email" throw an error? Formats are not built into Ajv 8. Add the ajv-formats plugin, or the schema fails to compile.
- My old schemas use draft-04. Which version do I need? Ajv 6. Version 8 supports draft-06 and newer.
- Why does Ajv reject a schema with an extra keyword like "placeholder"? Strict mode is on by default and stops on keywords it does not know. Declare them with addKeyword or turn strict mode off.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
