Ajv: Another JSON Schema Validator

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019...

README

Ajv logo

 

Ajv JSON schema validator


The fastest JSON validator for Node.js and browser.

Supports JSON Schema draft-04/06/07/2019-09/2020-12 (draft-04 support requires ajv-draft-04 package) and JSON Type Definition RFC8927.
build npm npm downloads Coverage Status SimpleX Gitter GitHub Sponsors

Ajv sponsors


[Mozilla](https://www.mozilla.org)[](https://opencollective.com/ajv)

[Microsoft](https://opensource.microsoft.com)[](https://opencollective.com/ajv)[](https://opencollective.com/ajv)

[Retool](https://retool.com/?utm_source=sponsor&utm_campaign=ajv)[Tidelift](https://tidelift.com/subscription/pkg/npm-ajv?utm_source=npm-ajv&utm_medium=referral&utm_campaign=enterprise)[SimpleX](https://github.com/simplex-chat/simplex-chat)[](https://opencollective.com/ajv)

Contributing


More than 100 people contributed to Ajv, and we would love to have you join the development. We welcome implementing new features that will benefit many users and ideas to improve our documentation.


Documentation


All documentation is available on the Ajv website.

Some useful site links:


## Please [sponsor Ajv development](https://github.com/sponsors/epoberezkin)

Since I asked to support Ajv development 40 people and 6 organizations contributed via GitHub and OpenCollective - this support helped receiving the MOSS grant!

Your continuing support is very important - the funds will be used to develop and maintain Ajv once the next major version is released.

Please sponsor Ajv via:

- GitHub sponsors page (GitHub will match it)

Thank you.

Open Collective sponsors



Performance


Ajv generates code to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization.

Currently Ajv is the fastest and the most standard compliant validator according to these benchmarks:

- json-schema-benchmark - 50% faster than the second place
- jsck benchmark - 20-190% faster

Performance of different validators by json-schema-benchmark:
performance

Features


- Ajv implements JSON Schema draft-06/07/2019-09/2020-12 standards (draft-04 is supported in v6):
  - all validation keywords (see JSON Schema validation keywords)
  - OpenAPI extensions:
    - NEW: keyword discriminator.
    - keyword nullable.
  - full support of remote references (remote schemas have to be added with addSchema or compiled to be available)
  - support of recursive references between schemas
  - correct string lengths for strings with unicode pairs
  - JSON Schema formats (with ajv-formats plugin).
- NEW: supports JSON Type Definition:
  - all keywords (see JSON Type Definition schema forms)
  - meta-schema for JTD schemas
  - "union" keyword and user-defined keywords (can be used inside "metadata" member of the schema)
- supports browsers and Node.js 10.x - current
- asynchronous loading of referenced schemas during compilation
- "All errors" validation mode with option allErrors
- error messages with parameters describing error reasons to allow error message generation
- i18n error messages support with ajv-i18n package
- assigning defaults to missing properties and items
- coercing data to the types specified intype keywords
- additional extension keywords with ajv-keywords package
- \$data reference to use values from the validated data as values for the schema keywords
- asynchronous validation of user-defined formats and keywords

Install


To install version 8:

  1. ```
  2. npm install ajv
  3. ```

## Getting started

Try it in the Node.js REPL: https://runkit.com/npm/ajv

In JavaScript:

  1. ``` js
  2. // or ESM/TypeScript import
  3. import Ajv from "ajv"
  4. // Node.js require:
  5. const Ajv = require("ajv")

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

  7. const schema = {
  8.   type: "object",
  9.   properties: {
  10.     foo: {type: "integer"},
  11.     bar: {type: "string"},
  12.   },
  13.   required: ["foo"],
  14.   additionalProperties: false,
  15. }

  16. const data = {
  17.   foo: 1,
  18.   bar: "abc",
  19. }

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

Learn how to use Ajv and see more examples in the Guide: getting started

Changes history






Code of conduct


Please review and follow the Code of conduct.

Please report any unacceptable behaviour to ajv.validator@gmail.com - it will be reviewed by the project team.

Security contact


To report a security vulnerability, please use the
Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerabilities via GitHub issues.

Open-source software support


Ajv is a part of Tidelift subscription - it provides a centralised support to open-source software users, in addition to the support provided by software maintainers.

License