@rjsf/validator-ajv8
Version:
The ajv-8 based validator for @rjsf/core
30 lines (27 loc) • 1.6 kB
text/typescript
import type { FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '@rjsf/utils';
import AJV8PrecompiledValidator from './precompiledValidator';
import type { Localizer, SuppressDuplicateFilteringType, ValidatorFunctions } from './types';
/** Creates and returns a `ValidatorType` interface that is implemented with a precompiled validator. 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.
*
* @param validateFns - The map of the validation functions that are created by the `compileSchemaValidators()` function
* @param rootSchema - The root schema that was used with the `compileSchemaValidators()` function
* @param [localizer] - If provided, is used to localize a list of Ajv `ErrorObject`s
* @param [suppressDuplicateFiltering] - Controls which duplicate filtering is suppressed; see `filterDuplicateErrors`
* @returns - The precompiled validator implementation resulting from the set of parameters provided
*/
export default function createPrecompiledValidator<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any,
>(
validateFns: ValidatorFunctions,
rootSchema: S,
localizer?: Localizer,
suppressDuplicateFiltering?: SuppressDuplicateFilteringType,
): ValidatorType<T, S, F> {
return new AJV8PrecompiledValidator<T, S, F>(validateFns, rootSchema, localizer, suppressDuplicateFiltering);
}