UNPKG

13.6 kBMarkdownView Raw
1<img align="right" alt="Ajv logo" width="160" src="https://ajv.js.org/img/ajv.svg">
2
3&nbsp;
4
5# Ajv JSON schema validator
6
7The fastest JSON validator for Node.js and browser.
8
9Supports JSON Schema draft-04/06/07/2019-09/2020-12 ([draft-04 support](https://ajv.js.org/json-schema.html#draft-04) requires ajv-draft-04 package) and JSON Type Definition [RFC8927](https://datatracker.ietf.org/doc/rfc8927/).
10
11[![build](https://github.com/ajv-validator/ajv/workflows/build/badge.svg)](https://github.com/ajv-validator/ajv/actions?query=workflow%3Abuild)
12[![npm](https://img.shields.io/npm/v/ajv.svg)](https://www.npmjs.com/package/ajv)
13[![npm downloads](https://img.shields.io/npm/dm/ajv.svg)](https://www.npmjs.com/package/ajv)
14[![Coverage Status](https://coveralls.io/repos/github/ajv-validator/ajv/badge.svg?branch=master)](https://coveralls.io/github/ajv-validator/ajv?branch=master)
15[![SimpleX](https://img.shields.io/badge/chat-on%20SimpleX-%2307b4b9)](https://simplex.chat/contact#/?v=1&smp=smp%3A%2F%2Fu2dS9sG8nMNURyZwqASV4yROM28Er0luVTx5X1CsMrU%3D%40smp4.simplex.im%2Fap4lMFzfXF8Hzmh-Vz0WNxp_1jKiOa-h%23MCowBQYDK2VuAyEAcdefddRvDfI8iAuBpztm_J3qFucj8MDZoVs_2EcMTzU%3D)
16[![Gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv)
17[![GitHub Sponsors](https://img.shields.io/badge/$-sponsors-brightgreen)](https://github.com/sponsors/epoberezkin)
18
19## Ajv sponsors
20
21[<img src="https://ajv.js.org/img/mozilla.svg" width="45%" alt="Mozilla">](https://www.mozilla.org)<img src="https://ajv.js.org/img/gap.svg" width="9%">[<img src="https://ajv.js.org/img/reserved.svg" width="45%">](https://opencollective.com/ajv)
22
23[<img src="https://ajv.js.org/img/microsoft.png" width="31%" alt="Microsoft">](https://opensource.microsoft.com)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/reserved.svg" width="31%">](https://opencollective.com/ajv)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/reserved.svg" width="31%">](https://opencollective.com/ajv)
24
25[<img src="https://ajv.js.org/img/retool.svg" width="22.5%" alt="Retool">](https://retool.com/?utm_source=sponsor&utm_campaign=ajv)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/tidelift.svg" width="22.5%" alt="Tidelift">](https://tidelift.com/subscription/pkg/npm-ajv?utm_source=npm-ajv&utm_medium=referral&utm_campaign=enterprise)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/simplex.svg" width="22.5%" alt="SimpleX">](https://github.com/simplex-chat/simplex-chat)<img src="https://ajv.js.org/img/gap.svg" width="3%">[<img src="https://ajv.js.org/img/reserved.svg" width="22.5%">](https://opencollective.com/ajv)
26
27## Contributing
28
29More 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.
30
31Please review [Contributing guidelines](./CONTRIBUTING.md) and [Code components](https://ajv.js.org/components.html).
32
33## Documentation
34
35All documentation is available on the [Ajv website](https://ajv.js.org).
36
37Some useful site links:
38- [Getting started](https://ajv.js.org/guide/getting-started.html)
39- [JSON Schema vs JSON Type Definition](https://ajv.js.org/guide/schema-language.html)
40- [API reference](https://ajv.js.org/api.html)
41- [Strict mode](https://ajv.js.org/strict-mode.html)
42- [Standalone validation code](https://ajv.js.org/standalone.html)
43- [Security considerations](https://ajv.js.org/security.html)
44- [Command line interface](https://ajv.js.org/packages/ajv-cli.html)
45- [Frequently Asked Questions](https://ajv.js.org/faq.html)
46
47## <a name="sponsors"></a>Please [sponsor Ajv development](https://github.com/sponsors/epoberezkin)
48
49Since I asked to support Ajv development 40 people and 6 organizations contributed via GitHub and OpenCollective - this support helped receiving the MOSS grant!
50
51Your continuing support is very important - the funds will be used to develop and maintain Ajv once the next major version is released.
52
53Please sponsor Ajv via:
54
55- [GitHub sponsors page](https://github.com/sponsors/epoberezkin) (GitHub will match it)
56- [Ajv Open Collective](https://opencollective.com/ajv)
57
58Thank you.
59
60#### Open Collective sponsors
61
62<a href="https://opencollective.com/ajv"><img src="https://opencollective.com/ajv/individuals.svg?width=890"></a>
63
64<a href="https://opencollective.com/ajv/organization/0/website"><img src="https://opencollective.com/ajv/organization/0/avatar.svg"></a>
65<a href="https://opencollective.com/ajv/organization/1/website"><img src="https://opencollective.com/ajv/organization/1/avatar.svg"></a>
66<a href="https://opencollective.com/ajv/organization/2/website"><img src="https://opencollective.com/ajv/organization/2/avatar.svg"></a>
67<a href="https://opencollective.com/ajv/organization/3/website"><img src="https://opencollective.com/ajv/organization/3/avatar.svg"></a>
68<a href="https://opencollective.com/ajv/organization/4/website"><img src="https://opencollective.com/ajv/organization/4/avatar.svg"></a>
69<a href="https://opencollective.com/ajv/organization/5/website"><img src="https://opencollective.com/ajv/organization/5/avatar.svg"></a>
70<a href="https://opencollective.com/ajv/organization/6/website"><img src="https://opencollective.com/ajv/organization/6/avatar.svg"></a>
71<a href="https://opencollective.com/ajv/organization/7/website"><img src="https://opencollective.com/ajv/organization/7/avatar.svg"></a>
72<a href="https://opencollective.com/ajv/organization/8/website"><img src="https://opencollective.com/ajv/organization/8/avatar.svg"></a>
73<a href="https://opencollective.com/ajv/organization/9/website"><img src="https://opencollective.com/ajv/organization/9/avatar.svg"></a>
74<a href="https://opencollective.com/ajv/organization/10/website"><img src="https://opencollective.com/ajv/organization/10/avatar.svg"></a>
75<a href="https://opencollective.com/ajv/organization/11/website"><img src="https://opencollective.com/ajv/organization/11/avatar.svg"></a>
76<a href="https://opencollective.com/ajv/organization/12/website"><img src="https://opencollective.com/ajv/organization/12/avatar.svg"></a>
77<a href="https://opencollective.com/ajv/organization/13/website"><img src="https://opencollective.com/ajv/organization/13/avatar.svg"></a>
78<a href="https://opencollective.com/ajv/organization/14/website"><img src="https://opencollective.com/ajv/organization/14/avatar.svg"></a>
79<a href="https://opencollective.com/ajv/organization/15/website"><img src="https://opencollective.com/ajv/organization/15/avatar.svg"></a>
80<a href="https://opencollective.com/ajv/organization/16/website"><img src="https://opencollective.com/ajv/organization/16/avatar.svg"></a>
81<a href="https://opencollective.com/ajv/organization/17/website"><img src="https://opencollective.com/ajv/organization/17/avatar.svg"></a>
82<a href="https://opencollective.com/ajv/organization/18/website"><img src="https://opencollective.com/ajv/organization/18/avatar.svg"></a>
83<a href="https://opencollective.com/ajv/organization/19/website"><img src="https://opencollective.com/ajv/organization/19/avatar.svg"></a>
84<a href="https://opencollective.com/ajv/organization/20/website"><img src="https://opencollective.com/ajv/organization/20/avatar.svg"></a>
85<a href="https://opencollective.com/ajv/organization/21/website"><img src="https://opencollective.com/ajv/organization/21/avatar.svg"></a>
86<a href="https://opencollective.com/ajv/organization/22/website"><img src="https://opencollective.com/ajv/organization/22/avatar.svg"></a>
87<a href="https://opencollective.com/ajv/organization/23/website"><img src="https://opencollective.com/ajv/organization/23/avatar.svg"></a>
88<a href="https://opencollective.com/ajv/organization/24/website"><img src="https://opencollective.com/ajv/organization/24/avatar.svg"></a>
89
90## Performance
91
92Ajv generates code to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization.
93
94Currently Ajv is the fastest and the most standard compliant validator according to these benchmarks:
95
96- [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark) - 50% faster than the second place
97- [jsck benchmark](https://github.com/pandastrike/jsck#benchmarks) - 20-190% faster
98- [z-schema benchmark](https://rawgit.com/zaggino/z-schema/master/benchmark/results.html)
99- [themis benchmark](https://cdn.rawgit.com/playlyfe/themis/master/benchmark/results.html)
100
101Performance of different validators by [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark):
102
103[![performance](https://chart.googleapis.com/chart?chxt=x,y&cht=bhs&chco=76A4FB&chls=2.0&chbh=62,4,1&chs=600x416&chxl=-1:|ajv|@exodus&#x2F;schemasafe|is-my-json-valid|djv|@cfworker&#x2F;json-schema|jsonschema&chd=t:100,69.2,51.5,13.1,5.1,1.2)](https://github.com/ebdrup/json-schema-benchmark/blob/master/README.md#performance)
104
105## Features
106
107- Ajv implements JSON Schema [draft-06/07/2019-09/2020-12](http://json-schema.org/) standards (draft-04 is supported in v6):
108 - all validation keywords (see [JSON Schema validation keywords](https://ajv.js.org/json-schema.html))
109 - [OpenAPI](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md) extensions:
110 - NEW: keyword [discriminator](https://ajv.js.org/json-schema.html#discriminator).
111 - keyword [nullable](https://ajv.js.org/json-schema.html#nullable).
112 - full support of remote references (remote schemas have to be added with `addSchema` or compiled to be available)
113 - support of recursive references between schemas
114 - correct string lengths for strings with unicode pairs
115 - JSON Schema [formats](https://ajv.js.org/guide/formats.html) (with [ajv-formats](https://github.com/ajv-validator/ajv-formats) plugin).
116 - [validates schemas against meta-schema](https://ajv.js.org/api.html#api-validateschema)
117- NEW: supports [JSON Type Definition](https://datatracker.ietf.org/doc/rfc8927/):
118 - all keywords (see [JSON Type Definition schema forms](https://ajv.js.org/json-type-definition.html))
119 - meta-schema for JTD schemas
120 - "union" keyword and user-defined keywords (can be used inside "metadata" member of the schema)
121- supports [browsers](https://ajv.js.org/guide/environments.html#browsers) and Node.js 10.x - current
122- [asynchronous loading](https://ajv.js.org/guide/managing-schemas.html#asynchronous-schema-loading) of referenced schemas during compilation
123- "All errors" validation mode with [option allErrors](https://ajv.js.org/options.html#allerrors)
124- [error messages with parameters](https://ajv.js.org/api.html#validation-errors) describing error reasons to allow error message generation
125- i18n error messages support with [ajv-i18n](https://github.com/ajv-validator/ajv-i18n) package
126- [removing-additional-properties](https://ajv.js.org/guide/modifying-data.html#removing-additional-properties)
127- [assigning defaults](https://ajv.js.org/guide/modifying-data.html#assigning-defaults) to missing properties and items
128- [coercing data](https://ajv.js.org/guide/modifying-data.html#coercing-data-types) to the types specified in `type` keywords
129- [user-defined keywords](https://ajv.js.org/guide/user-keywords.html)
130- additional extension keywords with [ajv-keywords](https://github.com/ajv-validator/ajv-keywords) package
131- [\$data reference](https://ajv.js.org/guide/combining-schemas.html#data-reference) to use values from the validated data as values for the schema keywords
132- [asynchronous validation](https://ajv.js.org/guide/async-validation.html) of user-defined formats and keywords
133
134## Install
135
136To install version 8:
137
138```
139npm install ajv
140```
141
142## <a name="usage"></a>Getting started
143
144Try it in the Node.js REPL: https://runkit.com/npm/ajv
145
146In JavaScript:
147
148```javascript
149// or ESM/TypeScript import
150import Ajv from "ajv"
151// Node.js require:
152const Ajv = require("ajv")
153
154const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}
155
156const schema = {
157 type: "object",
158 properties: {
159 foo: {type: "integer"},
160 bar: {type: "string"}
161 },
162 required: ["foo"],
163 additionalProperties: false,
164}
165
166const data = {
167 foo: 1,
168 bar: "abc"
169}
170
171const validate = ajv.compile(schema)
172const valid = validate(data)
173if (!valid) console.log(validate.errors)
174```
175
176Learn how to use Ajv and see more examples in the [Guide: getting started](https://ajv.js.org/guide/getting-started.html)
177
178## Changes history
179
180See [https://github.com/ajv-validator/ajv/releases](https://github.com/ajv-validator/ajv/releases)
181
182**Please note**: [Changes in version 8.0.0](https://github.com/ajv-validator/ajv/releases/tag/v8.0.0)
183
184[Version 7.0.0](https://github.com/ajv-validator/ajv/releases/tag/v7.0.0)
185
186[Version 6.0.0](https://github.com/ajv-validator/ajv/releases/tag/v6.0.0).
187
188## Code of conduct
189
190Please review and follow the [Code of conduct](./CODE_OF_CONDUCT.md).
191
192Please report any unacceptable behaviour to ajv.validator@gmail.com - it will be reviewed by the project team.
193
194## Security contact
195
196To report a security vulnerability, please use the
197[Tidelift security contact](https://tidelift.com/security).
198Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerabilities via GitHub issues.
199
200## Open-source software support
201
202Ajv is a part of [Tidelift subscription](https://tidelift.com/subscription/pkg/npm-ajv?utm_source=npm-ajv&utm_medium=referral&utm_campaign=readme) - it provides a centralised support to open-source software users, in addition to the support provided by software maintainers.
203
204## License
205
206[MIT](./LICENSE)