@rjsf/validator-ajv8 APIs
In RJSF version 6, the original, embedded AJV 6 validator implementation from earlier versions was removed in favor of the @rjsf/validator-ajv8 package.
@rjsf/validator-ajv8 uses the AJV 8 package, including adding support for using 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 can be found on GitHub here. 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.
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.
Parameters
- [options={}]: CustomValidatorOptionsType - The optional map of
CustomValidatorOptionsTypeoptions that are used to create theValidatorTypeinstance. Supported options:additionalMetaSchemas: Additional meta schemas the validator can accesscustomFormats: Additional custom format validatorsajvOptionsOverrides: Config overrides passed to the AJV constructorajvFormatOptions: Options forajv-formats; passfalseto disable format support entirelyAjvClass: The AJV class to construct (e.g.Ajv2019,Ajv2020)extenderFn: A function called to extend the AJV instance (e.g.ajvErrors())suppressDuplicateFiltering: Controls filtering of duplicateanyOf/oneOferrors using theSuppressDuplicateFilteringTypetype.'none'(default) filters duplicates for both;'anyOf'suppresses filtering foranyOf(oneOf duplicates still filtered);'oneOf'suppresses filtering foroneOf(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 onesgetValidationSchemas()lists, each$refinto 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
validateFnsparameter is an object obtained by importing from a precompiled validation file created via thecompileSchemaValidators()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: alocalizer, used to localize the engine's errors; asuppressDuplicateFiltering, controlling filtering of duplicateanyOf/oneOferrors (seecustomizeValidator()); and the form'scustomMergeAllOf, 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
ValidatorTypewhoserawValidation()reports Ajv'sErrorObjects