UNPKG

11.2 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[![Gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv)
16[![GitHub Sponsors](https://img.shields.io/badge/$-sponsors-brightgreen)](https://github.com/sponsors/epoberezkin)
17
18## Platinum sponsors
19
20[<img src="https://ajv.js.org/img/mozilla.svg" width="45%">](https://www.mozilla.org)<img src="https://ajv.js.org/img/gap.svg" width="8%">[<img src="https://ajv.js.org/img/reserved.svg" width="45%">](https://opencollective.com/ajv)
21
22## Ajv online event - May 20, 10am PT / 6pm UK
23
24We will talk about:
25- new features of Ajv version 8.
26- the improvements sponsored by Mozilla's MOSS grant.
27- how Ajv is used in JavaScript applications.
28
29Speakers:
30- [Evgeny Poberezkin](https://github.com/epoberezkin), the creator of Ajv.
31- [Mehan Jayasuriya](https://github.com/mehan), Program Officer at Mozilla Foundation, leading the [MOSS](https://www.mozilla.org/en-US/moss/) and other programs investing in the open source and community ecosystems.
32- [Matteo Collina](https://github.com/mcollina), Technical Director at NearForm and Node.js Technical Steering Committee member, creator of Fastify web framework.
33- [Kin Lane](https://github.com/kinlane), Chief Evangelist at Postman. Studying the tech, business & politics of APIs since 2010. Presidential Innovation Fellow during the Obama administration.
34- [Ulysse Carion](https://github.com/ucarion), the creator of JSON Type Definition specification.
35
36[Gajus Kuizinas](https://github.com/gajus) will host the event.
37
38Please [register here](https://us02web.zoom.us/webinar/register/2716192553618/WN_erJ_t4ICTHOnGC1SOybNnw).
39
40## Contributing
41
42More 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.
43
44Please review [Contributing guidelines](./CONTRIBUTING.md) and [Code components](https://ajv.js.org/components.html).
45
46## Documentation
47
48All documentation is available on the [Ajv website](https://ajv.js.org).
49
50Some useful site links:
51- [Getting started](https://ajv.js.org/guide/getting-started.html)
52- [JSON Schema vs JSON Type Definition](https://ajv.js.org/guide/schema-language.html)
53- [API reference](https://ajv.js.org/api.html)
54- [Strict mode](https://ajv.js.org/strict-mode.html)
55- [Standalone validation code](https://ajv.js.org/standalone.html)
56- [Security considerations](https://ajv.js.org/security.html)
57- [Command line interface](https://ajv.js.org/packages/ajv-cli.html)
58- [Frequently Asked Questions](https://ajv.js.org/faq.html)
59
60## <a name="sponsors"></a>Please [sponsor Ajv development](https://github.com/sponsors/epoberezkin)
61
62Since I asked to support Ajv development 40 people and 6 organizations contributed via GitHub and OpenCollective - this support helped receiving the MOSS grant!
63
64Your continuing support is very important - the funds will be used to develop and maintain Ajv once the next major version is released.
65
66Please sponsor Ajv via:
67
68- [GitHub sponsors page](https://github.com/sponsors/epoberezkin) (GitHub will match it)
69- [Ajv Open Collective️](https://opencollective.com/ajv)
70
71Thank you.
72
73#### Open Collective sponsors
74
75<a href="https://opencollective.com/ajv"><img src="https://opencollective.com/ajv/individuals.svg?width=890"></a>
76
77<a href="https://opencollective.com/ajv/organization/0/website"><img src="https://opencollective.com/ajv/organization/0/avatar.svg"></a>
78<a href="https://opencollective.com/ajv/organization/1/website"><img src="https://opencollective.com/ajv/organization/1/avatar.svg"></a>
79<a href="https://opencollective.com/ajv/organization/2/website"><img src="https://opencollective.com/ajv/organization/2/avatar.svg"></a>
80<a href="https://opencollective.com/ajv/organization/3/website"><img src="https://opencollective.com/ajv/organization/3/avatar.svg"></a>
81<a href="https://opencollective.com/ajv/organization/4/website"><img src="https://opencollective.com/ajv/organization/4/avatar.svg"></a>
82<a href="https://opencollective.com/ajv/organization/5/website"><img src="https://opencollective.com/ajv/organization/5/avatar.svg"></a>
83<a href="https://opencollective.com/ajv/organization/6/website"><img src="https://opencollective.com/ajv/organization/6/avatar.svg"></a>
84<a href="https://opencollective.com/ajv/organization/7/website"><img src="https://opencollective.com/ajv/organization/7/avatar.svg"></a>
85<a href="https://opencollective.com/ajv/organization/8/website"><img src="https://opencollective.com/ajv/organization/8/avatar.svg"></a>
86<a href="https://opencollective.com/ajv/organization/9/website"><img src="https://opencollective.com/ajv/organization/9/avatar.svg"></a>
87
88## Performance
89
90Ajv generates code to turn JSON Schemas into super-fast validation functions that are efficient for v8 optimization.
91
92Currently Ajv is the fastest and the most standard compliant validator according to these benchmarks:
93
94- [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark) - 50% faster than the second place
95- [jsck benchmark](https://github.com/pandastrike/jsck#benchmarks) - 20-190% faster
96- [z-schema benchmark](https://rawgit.com/zaggino/z-schema/master/benchmark/results.html)
97- [themis benchmark](https://cdn.rawgit.com/playlyfe/themis/master/benchmark/results.html)
98
99Performance of different validators by [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark):
100
101[![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)
102
103## Features
104
105- Ajv implements JSON Schema [draft-06/07/2019-09/2020-12](http://json-schema.org/) standards (draft-04 is supported in v6):
106 - all validation keywords (see [JSON Schema validation keywords](https://ajv.js.org/json-schema.html))
107 - [OpenAPI](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md) extensions:
108 - NEW: keyword [discriminator](https://ajv.js.org/json-schema.html#discriminator).
109 - keyword [nullable](https://ajv.js.org/json-schema.html#nullable).
110 - full support of remote references (remote schemas have to be added with `addSchema` or compiled to be available)
111 - support of recursive references between schemas
112 - correct string lengths for strings with unicode pairs
113 - JSON Schema [formats](https://ajv.js.org/guide/formats.html) (with [ajv-formats](https://github.com/ajv-validator/ajv-formats) plugin).
114 - [validates schemas against meta-schema](https://ajv.js.org/api.html#api-validateschema)
115- NEW: supports [JSON Type Definition](https://datatracker.ietf.org/doc/rfc8927/):
116 - all keywords (see [JSON Type Definition schema forms](https://ajv.js.org/json-type-definition.html))
117 - meta-schema for JTD schemas
118 - "union" keyword and user-defined keywords (can be used inside "metadata" member of the schema)
119- supports [browsers](https://ajv.js.org/guide/environments.html#browsers) and Node.js 10.x - current
120- [asynchronous loading](https://ajv.js.org/guide/managing-schemas.html#asynchronous-schema-loading) of referenced schemas during compilation
121- "All errors" validation mode with [option allErrors](https://ajv.js.org/options.html#allerrors)
122- [error messages with parameters](https://ajv.js.org/api.html#validation-errors) describing error reasons to allow error message generation
123- i18n error messages support with [ajv-i18n](https://github.com/ajv-validator/ajv-i18n) package
124- [removing-additional-properties](https://ajv.js.org/guide/modifying-data.html#removing-additional-properties)
125- [assigning defaults](https://ajv.js.org/guide/modifying-data.html#assigning-defaults) to missing properties and items
126- [coercing data](https://ajv.js.org/guide/modifying-data.html#coercing-data-types) to the types specified in `type` keywords
127- [user-defined keywords](https://ajv.js.org/guide/user-keywords.html)
128- additional extension keywords with [ajv-keywords](https://github.com/ajv-validator/ajv-keywords) package
129- [\$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
130- [asynchronous validation](https://ajv.js.org/guide/async-validation.html) of user-defined formats and keywords
131
132## Install
133
134To install version 8:
135
136```
137npm install ajv
138```
139
140## <a name="usage"></a>Getting started
141
142Try it in the Node.js REPL: https://runkit.com/npm/ajv
143
144In JavaScript:
145
146```javascript
147// or ESM/TypeScript import
148import Ajv from "ajv"
149// Node.js require:
150const Ajv = require("ajv")
151
152const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}
153
154const schema = {
155 type: "object",
156 properties: {
157 foo: {type: "integer"},
158 bar: {type: "string"}
159 },
160 required: ["foo"],
161 additionalProperties: false,
162}
163
164const data = {
165 foo: 1,
166 bar: "abc"
167}
168
169const validate = ajv.compile(schema)
170const valid = validate(data)
171if (!valid) console.log(validate.errors)
172```
173
174Learn how to use Ajv and see more examples in the [Guide: getting started](https://ajv.js.org/guide/getting-started.html)
175
176## Changes history
177
178See [https://github.com/ajv-validator/ajv/releases](https://github.com/ajv-validator/ajv/releases)
179
180**Please note**: [Changes in version 8.0.0](https://github.com/ajv-validator/ajv/releases/tag/v8.0.0)
181
182[Version 7.0.0](https://github.com/ajv-validator/ajv/releases/tag/v7.0.0)
183
184[Version 6.0.0](https://github.com/ajv-validator/ajv/releases/tag/v6.0.0).
185
186## Code of conduct
187
188Please review and follow the [Code of conduct](./CODE_OF_CONDUCT.md).
189
190Please report any unacceptable behaviour to ajv.validator@gmail.com - it will be reviewed by the project team.
191
192## Security contact
193
194To report a security vulnerability, please use the
195[Tidelift security contact](https://tidelift.com/security).
196Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerabilities via GitHub issues.
197
198## Open-source software support
199
200Ajv 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.
201
202## License
203
204[MIT](./LICENSE)