docs/guide/schema-language.md
[[toc]]
Ajv supports the new specification focussed on defining cross-platform types of JSON messages/payloads - JSON Type Definition (JTD). See the informal reference of JTD schema forms and formal specification RFC8927.
Ajv supports most widely used drafts of JSON Schema specification. Please see the informal reference of available JSON Schema validation keywords and specification drafts.
Draft-04 is not included in Ajv v7, because of some differences it has with the following drafts:
id in draft-04 instead of $id)You can still use draft-04 schemas with Ajv v6 - while this is no longer actively developed, any security related issues would still be supported at least until 30/06/2021.
To install v6:
npm install ajv@6
You can migrate schemas from draft-04 to draft-07 using ajv-cli.
These are the most widely used versions of JSON Schema specification, and they are supported with the main ajv export.
import Ajv from "ajv"
const ajv = new Ajv()
If you need to support draft-06 schemas you need to add additional meta-schema, but you may just change (or remove) $schema attribute in your schemas - no other changes are needed:
const draft6MetaSchema = require("ajv/dist/refs/json-schema-draft-06.json")
ajv.addMetaSchema(draft6MetaSchema)
The main advantage of this JSON Schema version over draft-07 is the ability to spread the definition of records that do not allow additional properties across multiple schemas. If you do not need it, you might be better off with draft-07.
To use Ajv with the support of all JSON Schema draft-2019-09/2020-12 features you need to use a different export:
import Ajv2019 from "ajv/dist/2019"
const ajv = new Ajv2019()
Optionally, you can add draft-07 meta-schema, to use both draft-07 and draft-2019-09 schemas in one Ajv instance:
const draft7MetaSchema = require("ajv/dist/refs/json-schema-draft-07.json")
ajv.addMetaSchema(draft7MetaSchema)
Draft-2019-09 support is provided via a separate export in order to avoid increasing the bundle and generated code size for draft-07 users.
With this import Ajv supports the following features:
unevaluatedProperties and unevaluatedItemsdependentRequired, dependentSchemas, maxContains/minContainsrecursiveAnchor/recursiveReference] - see Extending recursive schemas::: warning Draft-2019-09 features performance cost (even when not used)
Supporting dynamic recursive references and unevaluatedProperties/unevaluatedItems keywords adds additional generated code even to the validation functions where these features are not used (when possible, Ajv determines which properties/items are "unevaluated" at compilation time, but support for dynamic references always adds additional generated code). If you are not using these features in your schemas it is recommended to use default Ajv export with JSON-Schema draft-07 support.
:::
Both JSON Schema and JSON Type Definition are cross-platform specifications with implementations in multiple programming languages that define the shape of your JSON data.
You can see the difference between the two specifications in Getting started section examples.
This section compares their pros/cons to help decide which specification fits your application better.
Pros:
Cons:
See JSON Schema for more information and the list of defined keywords.
Pros:
Cons:
<sup>1</sup> Ajv defines non-standard keyword "union" that can be used inside "metadata" object.
<sup>2</sup> You can still combine schemas from multiple files in the application code.
<sup>3</sup> Ajv defines meta-schema for JTD schemas.
See JSON Type Definition for more information and the list of defined schema forms.