Skip to main content
Version: Current (v7.0.0-beta.3)

validator-ajv8 fails every validation of a schema naming a draft it doesn't support

Detect​

  • In your schema: a root $schema naming a JSON Schema draft that your @rjsf/validator-ajv8 validator doesn't check. Examples are "$schema": "https://json-schema.org/draft/2020-12/schema" with the default validator, which checks draft-07, and a draft-07 $schema with a validator built with AjvClass: Ajv2020.

  • In your schema: a root $schema that misspells its draft's, with http for https or the reverse, such as "$schema": "https://json-schema.org/draft-07/schema#". The validator doesn't hold a meta schema under that URL.

  • On the rendered page or in your tests: such a form that holds a oneOf, an anyOf or an if used to submit. It now shows this error on every submit and, under live validation, on every 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.
  • In the browser console: the same text as a warning while such a form renders.

  • In your code or tests: a match on no schema with key or ref, the text of the error such a schema reported when it held no oneOf, anyOf or if.

Change​

Without a oneOf, an anyOf or an if, such a form reported no schema with key or ref "https://json-schema.org/draft/2020-12/schema" on every validation, so it could not be submitted. With one, the form asked the validator about its options or conditions first. The validator then checked the data as its own draft, without the keywords that draft doesn't know, such as prefixItems or dependentRequired, so the form submitted.

Every such schema now fails every validation, on a submit and under live validation, with one error. Its first sentence quotes the $schema, and the rest says what to change.

Where it ends with Import the validator from "@rjsf/validator-ajv8/2020" instead., import the validator from the entry point it names: @rjsf/validator-ajv8/2020 for 2020-12, @rjsf/validator-ajv8/2019 for 2019-09, or the package root for draft-07. Each also exports a customizeValidator() that takes the same options:

-import validator from '@rjsf/validator-ajv8';
+import validator from '@rjsf/validator-ajv8/2020';

To keep the data checked as draft-07, as it was, name draft-07 in $schema or remove the keyword:

{
- "$schema": "https://json-schema.org/draft/2020-12/schema",
+ "$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"oneOf": [{ "required": ["email"] }, { "required": ["phone"] }]
}

Where it ends with Import the validator from "@rjsf/validator-ajv8" and drop the "AjvClass" option., the validator was built with an AjvClass, which wins at every entry point. Remove the option, and import customizeValidator() from the entry point the error names where you don't already:

import { customizeValidator } from '@rjsf/validator-ajv8';
-import { Ajv2020 } from 'ajv/dist/2020';

-const validator = customizeValidator({ AjvClass: Ajv2020, customFormats });
+const validator = customizeValidator({ customFormats });

Where it says It misspells "http://json-schema.org/draft-07/schema"., write $schema as the error spells it. Draft-07 starts with http://, and 2019-09 and 2020-12 start with https://. When one of the two sentences above follows, do that as well:

{
- "$schema": "https://json-schema.org/draft-07/schema#",
+ "$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"oneOf": [{ "required": ["email"] }, { "required": ["phone"] }]
}

Where it ends with Pass its meta-schema in the "additionalMetaSchemas" option., no entry point checks the draft, such as draft-06. See additionalMetaSchemas. JSON Schema versions lists which draft each validator checks.

Update code and tests that match no schema with key or ref to match the new text.

Verify​

  • The form submits, and neither the error list nor the console shows the message.
  • With the entry point for the schema's draft, the keywords of that draft now report errors, so data that used to submit can fail on a prefixItems or a dependentRequired rule.