JSON Schema versions
JSON Schema has several versions, called drafts: draft-04, draft-06, draft-07, 2019-09 and 2020-12.
A schema says which one it is written for in its $schema keyword:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object"
}
Two parts of react-jsonschema-form read your schema, and they treat the draft differently:
- The form reads a keyword wherever it is written, whichever validator it is given. A
prefixItemsrenders as a tuple with every validator. Only$anchordepends on the draft the schema declares, as the table below says. - The validator checks the data against one draft, and that draft decides what is enforced. A validator for draft-07 does not know
prefixItems, so it ignores it without a word.
A schema that uses keywords from 2019-09 or 2020-12 should therefore declare its draft in $schema and be given a validator for that draft.
Otherwise the form shows fields for keywords that validation never checks.
Which draft each validator checks
| Validator | Draft it checks | How it treats a $schema naming another draft |
|---|---|---|
@rjsf/validator-ajv8 | draft-07 | Reports an error that says what to change, and validates nothing |
@rjsf/validator-ajv8/2019 | 2019-09 | Reports an error that says what to change, and validates nothing |
@rjsf/validator-ajv8/2020 | 2020-12 | Reports an error that says what to change, and validates nothing |
@rjsf/validator-cfworker | 2020-12, or the one its draft option names | Ignores $schema and checks the draft it was built for |
@rjsf/validator-ata | The one $schema names, 2020-12 without one | Follows it for draft-04, draft-06, draft-07, 2019-09 and 2020-12 |
@rjsf/validator-ajv8
The package has one entry point per draft that Ajv has a class for.
Each exports a ready validator as its default export, and a customizeValidator() that takes the same options as the one of the package root:
import Form from '@rjsf/core';
import { RJSFSchema } from '@rjsf/utils';
import validator from '@rjsf/validator-ajv8/2020';
const schema: RJSFSchema = {
$schema: 'https://json-schema.org/draft/2020-12/schema',
type: 'array',
prefixItems: [{ type: 'string' }, { type: 'number' }],
};
export default function App() {
return <Form schema={schema} validator={validator} />;
}
import { customizeValidator } from '@rjsf/validator-ajv8/2019';
const validator = customizeValidator({ customFormats: { 'phone-us': /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/ } });
A validator given a schema whose $schema names another draft does not validate the data.
Every validation reports one error instead, which says what to change:
This validator does not support the schema's "$schema", "https://json-schema.org/draft/2020-12/schema". Import the validator from "@rjsf/validator-ajv8/2020" instead.
What follows the first sentence depends on the $schema and on how the validator was built:
The schema's $schema | The error ends with |
|---|---|
| draft-07, 2019-09 or 2020-12 | Import the validator from "@rjsf/validator-ajv8/2020" instead., with the entry point for that draft |
The same, under a validator built with an AjvClass option | Import the validator from "@rjsf/validator-ajv8/2020" and drop the "AjvClass" option. |
One of those three with http for https or the reverse, such as https://json-schema.org/draft-07/schema# | It misspells "http://json-schema.org/draft-07/schema"., then one of the two above unless the validator checks that draft |
| Any other, such as draft-06, draft-04 or a meta schema of your own | Pass its meta-schema in the "additionalMetaSchemas" option. |
An AjvClass you pass wins at every entry point, so with one the validator checks the draft of that class wherever it is imported from.
A misspelled $schema is not read as the draft it resembles: correct it in the schema.
See additionalMetaSchemas for draft-06 and draft-04.
A schema without a $schema is checked as the validator's own draft, whatever keywords it uses.
The error is reported whatever else the schema holds, on a submit and under live validation alike.
A form whose schema has options or conditions, such as a oneOf, an anyOf or an if, asks the validator which of them the data matches while it renders, before it validates anything.
The validator answers as its own draft reads them, so the form still renders, and logs the message above once as a console warning.
Ajv's validateSchema: false, passed in ajvOptionsOverrides, turns off every check of a schema against its meta schema, this one included: such a validator checks the data as its own draft whatever $schema says.
compileSchemaValidators() compiles with Ajv's draft-07 class unless it is given another, and throws the same error for a schema declaring another draft, with Pass the Ajv class of "ajv/dist/2020.js" as the "AjvClass" option. in place of the validator to import.
Pass it the Ajv class of the draft in its AjvClass option, as AjvClass shows. The precompiled validator is then created with createPrecompiledValidator() of the package root, whichever draft the schema was compiled for.
@rjsf/validator-cfworker
The validator checks draft 2020-12 unless its draft option names draft-04, draft-07 or 2019-09.
It does not read $schema, so a schema declaring another draft is checked as the draft the validator was built for, without an error.
@rjsf/validator-ata
The validator reads each schema as the draft its $schema names, and as 2020-12 when it has none.
A draft-07 schema without a $schema is therefore read as 2020-12, which ignores a tuple written as an items array and the dependencies keyword: declare "$schema": "http://json-schema.org/draft-07/schema#" to have them checked.
What the form does with the newer keywords
The table lists the keywords that 2019-09 and 2020-12 added or changed, and what the form does with each. "Validation only" means the keyword changes nothing in what the form renders, and the validators for its draft check it on submit.
| Keyword | Since | In the form |
|---|---|---|
$defs | 2019-09 | A $ref into it resolves, as one into definitions does |
Keywords beside a $ref | 2019-09 | Read together with the schema the $ref names, under every draft |
$anchor | 2019-09 | A $ref to it resolves where the root schema declares no $schema, or declares 2019-09 or 2020-12, see References to an $anchor. Under a root declaring another draft, such as draft-07, the form throws Could not find a definition |
$recursiveRef, $recursiveAnchor | 2019-09 | Not yet: the field renders as an unsupported field, see #5988 |
$dynamicRef, $dynamicAnchor | 2020-12 | Not yet: the field renders as an unsupported field, see #5988 |
prefixItems, with items for the rest | 2020-12 | Renders a tuple, see Tuples with prefixItems |
unevaluatedItems | 2019-09 | Describes the items past a tuple that declares no items or additionalItems for them: they render, with an Add button, see The unevaluatedItems keyword. Validation only anywhere else |
unevaluatedProperties | 2019-09 | Read as additionalProperties is where the object names none, see Additional and pattern properties |
dependentRequired | 2019-09 | Read as the dependencies entries that list keys, under every draft: the dependent field is marked required, see Dependencies |
dependentSchemas | 2019-09 | Read as the dependencies entries that hold schemas, under every draft: the fields of the dependent schema render, see Dependencies |
contains, minContains, maxContains | 2019-09 | Validation only (contains itself is older; the two counts are new) |
deprecated | 2019-09 | Adds "(deprecated)" to the field's label, see Deprecated fields |
The duration and uuid formats | 2019-09 | Render as a text input. Validation only, and the draft-07 @rjsf/validator-ajv8 checks them too |
The dependencies keyword that dependentRequired and dependentSchemas replace is read by the form under every draft, see Dependencies, and @rjsf/validator-ajv8/2019 and @rjsf/validator-ajv8/2020 still check it.
The draft-07 @rjsf/validator-ajv8 does not check dependentRequired or dependentSchemas, though the form reads them.
A tuple written the draft-07 way, as an items array with additionalItems, renders under every draft and is checked by @rjsf/validator-ajv8 and @rjsf/validator-ajv8/2019, while @rjsf/validator-ajv8/2020 reports such a schema as invalid: write it with prefixItems there.