byr-gdp / ajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09 and JSON Type Definition (RFC8927)

Home Page:https://ajv.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ajv logo

 

Ajv: Another JSON schema validator

Super fast JSON validator for Node.js and browser.

Supports JSON Schema draft-06/07/2019-09 (draft-04 is supported in version 6) and JSON Type Definition RFC8927.

build npm npm downloads Coverage Status Gitter GitHub Sponsors

Platinum sponsors

Using version 7

Ajv version 7 has these new features:

  • NEW: support of JSON Type Definition RFC8927 (from v7.1.0), including generation of serializers and parsers from JTD schemas that are more efficient than native JSON serialization/parsing, combining JSON string parsing and validation in one function.
  • support of JSON Schema draft-2019-09 features: unevaluatedProperties and unevaluatedItems, dynamic recursive references and other additional keywords.
  • to reduce the mistakes in JSON schemas and unexpected validation results, strict mode is added - it prohibits ignored or ambiguous JSON Schema elements.
  • to make code injection from untrusted schemas impossible, code generation is fully re-written to be safe and to allow code optimization (compiled schema code size is reduced by more than 10%).
  • to simplify Ajv extensions, the new keyword API that is used by pre-defined keywords is available to user-defined keywords - it is much easier to define any keywords now, especially with subschemas. ajv-keywords package was updated to use the new API (in v4.0.0)
  • schemas are compiled to ES6 code (ES5 code generation is also supported with an option).
  • to improve reliability and maintainability the code is migrated to TypeScript.

Please note:

  • the support for JSON-Schema draft-04 is removed - if you have schemas using "id" attributes you have to replace them with "$id" (or continue using Ajv v6 that will be supported until 02/28/2021).
  • all formats are separated to ajv-formats package - they have to be explicitly added if you use them.

See release notes for the details.

To install the new version:

npm install ajv

See Getting started for code example.

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.

At Ajv, we are committed to creating more equitable and inclusive spaces for our community and team members to contribute to discussions that affect both this project and our ongoing work in the open source ecosystem.

We strive to create an environment of respect and healthy discourse by setting standards for our interactions and we expect it from all members of our community - from long term project member to first time visitor. For more information, review our code of conduct and values.

How we make decisions

We value conscious curation of our library size, and balancing performance and functionality. To that end, we cannot accept every suggestion. When evaluating pull requests we consider:

  • Will this benefit many users or a niche use case?
  • How will this impact the performance of Ajv?
  • How will this expand our library size?

To help us evaluate and understand, when you submit an issue and pull request:

  • Explain why this feature is important to the user base
  • Include documentation
  • Include test coverage with any new feature implementations

Please include documentation and test coverage with any new feature implementations.

To run tests:

npm install
git submodule update --init
npm test

npm run build - compiles typescript to dist folder.

Please also review Contributing guidelines and Code components.

Contents

Mozilla MOSS grant and OpenJS Foundation

Ajv has been awarded a grant from Mozilla’s Open Source Support (MOSS) program in the “Foundational Technology” track! It will sponsor the development of Ajv support of JSON Schema version 2019-09 and of JSON Type Definition (RFC8927).

Ajv also joined OpenJS Foundation – having this support will help ensure the longevity and stability of Ajv for all its users.

This blog post has more details.

I am looking for the long term maintainers of Ajv – working with ReadySet, also sponsored by Mozilla, to establish clear guidelines for the role of a "maintainer" and the contribution standards, and to encourage a wider, more inclusive, contribution from the community.

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:

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:

Performance of different validators by json-schema-benchmark:

performance

Features

Install

To install version 7:

npm install ajv

Getting started

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

In JavaScript:

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

const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}
const validate = ajv.compile(schema)
const valid = validate(data)
if (!valid) console.log(validate.errors)

See more examples in Guide: getting started

Extending Ajv

User defined keywords

See section in data validation and the detailed guide.

Plugins

Ajv can be extended with plugins that add keywords, formats or functions to process generated code. When such plugin is published as npm package it is recommended that it follows these conventions:

  • it exports a function that accepts ajv instance as the first parameter - it allows using plugins with ajv-cli.
  • this function returns the same instance to allow chaining.
  • this function can accept an optional configuration as the second parameter.

You can import Plugin interface from ajv if you use Typescript.

If you have published a useful plugin please submit a PR to add it to the next section.

Related packages

  • ajv-bsontype - plugin to validate mongodb's bsonType formats
  • ajv-cli - command line interface
  • ajv-formats - formats defined in JSON Schema specification
  • ajv-errors - plugin for defining error messages in the schema
  • ajv-i18n - internationalised error messages
  • ajv-istanbul - plugin to instrument generated validation code to measure test coverage of your schemas
  • ajv-keywords - plugin with additional validation keywords (select, typeof, etc.)
  • ajv-merge-patch - plugin with keywords $merge and $patch
  • ajv-formats-draft2019 - format validators for draft2019 that aren't included in ajv-formats (ie. idn-hostname, idn-email, iri, iri-reference and duration)

Some packages using Ajv

Changes history

See https://github.com/ajv-validator/ajv/releases

Please note: Changes in version 7.0.0

Version 6.0.0.

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 security contact. 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

MIT

About

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09 and JSON Type Definition (RFC8927)

https://ajv.js.org

License:MIT License


Languages

Language:TypeScript 98.1%Language:JavaScript 1.5%Language:Shell 0.4%