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

@rjsf/validator-ajv8 APIs

@rjsf/validator-ajv8 is the recommended validator package for RJSF; @rjsf/validator-cfworker and @rjsf/validator-ata are the alternatives. It uses the AJV 8 package, and supports precompiled validators. Below are the exported API functions that are provided by this package. See the Validation documentation for examples of using these APIs.

Types​

There are a few Typescript types that are exported by @rjsf/validator-ajv8 in support of the APIs.

These types are defined in packages/validator-ajv8/src/types.ts on GitHub. The package also re-exports Ajv's ErrorObject, the type of the errors rawValidation() returns, so an app doesn't need ajv as its own dependency to name it.

Entry points for JSON Schema 2019-09 and 2020-12​

The package root validates JSON Schema draft-07. @rjsf/validator-ajv8/2019 and @rjsf/validator-ajv8/2020 validate drafts 2019-09 and 2020-12, with Ajv's Ajv2019 and Ajv2020 classes. Each exports a ready validator as its default export, the customizeValidator() below with its AjvClass option defaulting to the class of that draft, and the package's types. AJV8Validator, createPrecompiledValidator() and compileSchemaValidators() do not depend on the draft and stay on the package root.

import validator, { customizeValidator } from '@rjsf/validator-ajv8/2020';

A validator handed a schema whose $schema names a draft it has no meta schema for reports that as its one error on every validation, and validates nothing. The error says what to change. For draft-07, 2019-09 and 2020-12 it names the entry point to import the validator from, and for a validator built with an AjvClass option it also says to drop the option. For any other $schema, such as draft-06, draft-04 or a meta schema of your own, it asks for the meta schema in the additionalMetaSchemas option. For a $schema that differs from the one of draft-07, 2019-09 or 2020-12 only in http for https or the reverse, it names the $schema that one misspells. compileSchemaValidators() throws that error, naming the Ajv class to pass as AjvClass in place of the entry point. See JSON Schema versions for which draft each validator checks.

APIs​

customizeValidator<S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = FormContextType>()​

Creates and returns an AJV8Validator, an implementation of ValidatorType customized with the given options if provided. If a localizer is provided, it is used to translate the messages generated by the underlying AJV validation. The validator keeps what it compiled for the last two root schemas it validated against, and drops the older one's when a third arrives. Ajv takes each $id once, so a root with the same $id as the other, or one holding an $id other than a fragment below its own, is kept alone. This reads only $id: on an ajv-draft-04 validator, two roots with the same draft-04 id are both kept, and a $ref by that id resolves into the one registered first. A schema isValid() is handed with the same $id as the root it is checked against is checked as that root, not as itself, since Ajv holds the root under that $id. Every form that uses the package's default export shares one validator, and so its cache: on a page with three or more such forms, moving between them compiles again. A form given its own validator from customizeValidator() keeps its own cache.

Parameters​

  • [options={}]: CustomValidatorOptionsType - The optional map of CustomValidatorOptionsType options that are used to create the ValidatorType instance. Supported options:
    • additionalMetaSchemas: Additional meta schemas the validator can access
    • customFormats: Additional custom format validators
    • ajvOptionsOverrides: Config overrides passed to the AJV constructor
    • ajvFormatOptions: Options for ajv-formats; pass false to disable format support entirely
    • AjvClass: The AJV class to construct. It defaults to Ajv's draft-07 class, and in the customizeValidator() of an entry point for another draft to the class of that draft
    • extenderFn: A function called to extend the AJV instance (e.g. ajvErrors())
    • suppressDuplicateFiltering: Controls filtering of duplicate anyOf/oneOf errors using the SuppressDuplicateFilteringType type. 'none' (default) filters duplicates for both; 'anyOf' suppresses filtering for anyOf (oneOf duplicates still filtered); 'oneOf' suppresses filtering for oneOf (anyOf duplicates still filtered); 'all' disables all duplicate filtering.
  • [localizer]: Localizer | undefined - If provided, is used to localize a list of Ajv ErrorObjects after running the form validation using AJV

Returns​

  • AJV8Validator<S, F>: The custom validator implementation resulting from the set of parameters provided

compileSchemaValidators<S extends StrictRJSFSchema = RJSFSchema>()​

The function used to compile a schema into an output file in the form that allows it to be used as a precompiled validator. The main reasons for using a precompiled validator is reducing code size, improving validation speed and, most importantly, avoiding dynamic code compilation when prohibited by a browser's Content Security Policy. For more information about AJV code compilation see: https://ajv.js.org/standalone.html

Parameters​

  • schema: S - The schema to be compiled into a set of precompiled validators functions
  • output: string - The name of the file into which the precompiled validator functions will be generated
  • [options={}]: CustomValidatorOptionsType - The options to compile with. They are the same options that are passed to the customizeValidator() function in order to modify the behavior of the regular AJV-based validator. The schemas compiled are the ones getValidationSchemas() lists, each $ref into the root exported as the function compiled for that part of the root

createPrecompiledValidator<S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = FormContextType>()​

Creates and returns an AJV8PrecompiledValidator, an implementation of ValidatorType that uses the precompiled validator functions. If a localizer is provided, it is used to translate the messages generated by the underlying AJV validation.

note

The validateFns parameter is an object obtained by importing from a precompiled validation file created via the compileSchemaValidators() function.

Parameters​

  • validateFns: ValidatorFunctions - The map of the validation functions that are created by the compileSchemaValidators() function
  • rootSchema: S - The root schema that was used with the compileSchemaValidators() function
  • [options]: PrecompiledValidatorOptionsType<S> | undefined - The options the validator is constructed with: a localizer, used to localize the engine's errors; a suppressDuplicateFiltering, controlling filtering of duplicate anyOf/oneOf errors (see customizeValidator()); and the form's customMergeAllOf, used when the validator resolves its own root schema to check the schema it is handed against it

Returns​

  • AJV8PrecompiledValidator<S, F>: The precompiled validator implementation resulting from the set of parameters provided, a ValidatorType whose rawValidation() reports Ajv's ErrorObjects