UNPKG

@rjsf/validator-ajv6

Version:
69 lines (68 loc) 4.04 kB
import { Ajv } from 'ajv'; import { CustomValidator, ErrorSchema, ErrorTransformer, FormContextType, RJSFSchema, RJSFValidationError, StrictRJSFSchema, UiSchema, ValidationData, ValidatorType } from '@rjsf/utils'; import { CustomValidatorOptionsType } from './types.js'; /** `ValidatorType` implementation that uses the AJV 6 validation mechanism. * * @deprecated in favor of the `@rjsf/validator-ajv8 */ export default class AJV6Validator<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any> implements ValidatorType<T, S, F> { /** The AJV instance to use for all validations */ ajv: Ajv; /** Constructs an `AJV6Validator` instance using the `options` * * @param options - The `CustomValidatorOptionsType` options that are used to create the AJV instance */ constructor(options: CustomValidatorOptionsType); /** Converts an `errorSchema` into a list of `RJSFValidationErrors` * * @param errorSchema - The `ErrorSchema` instance to convert * @param [fieldPath=[]] - The current field path, defaults to [] if not specified * @deprecated - Use the `toErrorList()` function provided by `@rjsf/utils` instead. This function will be removed in * the next major release. */ toErrorList(errorSchema?: ErrorSchema<T>, fieldPath?: string[]): RJSFValidationError[]; /** Transforming the error output from ajv to format used by @rjsf/utils. * At some point, components should be updated to support ajv. * * @param errors - The list of AJV errors to convert to `RJSFValidationErrors` * @private */ private transformRJSFValidationErrors; /** Runs the pure validation of the `schema` and `formData` without any of the RJSF functionality. Provided for use * by the playground. Returns the `errors` from the validation * * @param schema - The schema against which to validate the form data * @param schema * @param formData - The form data to validate */ rawValidation<Result = any>(schema: RJSFSchema, formData?: T): { errors?: Result[]; validationError?: Error; }; /** This function processes the `formData` with an optional user contributed `customValidate` function, which receives * the form data and a `errorHandler` function that will be used to add custom validation errors for each field. Also * supports a `transformErrors` function that will take the raw AJV validation errors, prior to custom validation and * transform them in what ever way it chooses. * * @param formData - The form data to validate * @param schema - The schema against which to validate the form data * @param [customValidate] - An optional function that is used to perform custom validation * @param [transformErrors] - An optional function that is used to transform errors after AJV validation * @param [uiSchema] - An optional uiSchema that is passed to `transformErrors` and `customValidate` */ validateFormData(formData: T | undefined, schema: S, customValidate?: CustomValidator<T, S, F>, transformErrors?: ErrorTransformer<T, S, F>, uiSchema?: UiSchema<T, S, F>): ValidationData<T>; /** * This function checks if a schema needs to be added and if the root schemas don't match it removes the old root schema from the ajv instance and adds the new one. * @param rootSchema - The root schema used to provide $ref resolutions */ handleSchemaUpdate(rootSchema: RJSFSchema): void; /** Validates data against a schema, returning true if the data is valid, or * false otherwise. If the schema is invalid, then this function will return * false. * * @param schema - The schema against which to validate the form data * @param schema * @param formData- - The form data to validate * @param rootSchema - The root schema used to provide $ref resolutions */ isValid(schema: RJSFSchema, formData: T | undefined, rootSchema: RJSFSchema): boolean; }