Debian Codegraph @

packages/validator-ajv6/test/utilsTests/getTestValidator.ts

raw symbols SCIP
1 import {
2 CustomValidator,
3 ErrorSchema,
4 ErrorTransformer,
5 RJSFSchema,
6 RJSFValidationError,
7 ValidationData,
8 } from '@rjsf/utils';
9 // With Lerna active, the test world has access to the test suite via the symlink
10 import { TestValidatorType } from '@rjsf/utils/test/schema';
11 import { customizeValidator, CustomValidatorOptionsType } from '../../src';
12
13 /** In this environment, a test validator merely creates an internal `AJV6` validator with the custom `options` and
14 * then forwards all calls into it. It is provided to mirror the test validator in the `utils` directory, except that
15 * the `setReturnValues()` function does nothing.
16 *
17 * @param options
18 */
19 export default function getTestValidator<T = any>(options: CustomValidatorOptionsType): TestValidatorType {
20 const validator = customizeValidator<T>(options);
21 return {
22 validateFormData(
23 formData: T,
24 schema: RJSFSchema,
25 customValidate?: CustomValidator<T>,
26 transformErrors?: ErrorTransformer
27 ): ValidationData<T> {
28 return validator.validateFormData(formData, schema, customValidate, transformErrors);
29 },
30 toErrorList(errorSchema?: ErrorSchema<T>, fieldPath?: string[]): RJSFValidationError[] {
31 return validator.toErrorList(errorSchema, fieldPath);
32 },
33 isValid(schema: RJSFSchema, formData: T | undefined, rootSchema: RJSFSchema): boolean {
34 return validator.isValid(schema, formData, rootSchema);
35 },
36 rawValidation<Result = any>(schema: RJSFSchema, formData?: T): { errors?: Result[]; validationError?: Error } {
37 return validator.rawValidation(schema, formData);
38 },
39 // This is intentionally a no-op as we are using the real validator here
40 setReturnValues() {},
41 };
42 }