All files / app/formFields getFormFieldsValidationSchema.ts

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3732x 32x   32x   32x           32x 396x 396x   396x   3652x 2945x 2945x   2945x 1303x     2945x         2945x            
import { memoize } from '@bigcommerce/memoize';
import { object, string, ObjectSchema, StringSchema } from 'yup';
 
import getCustomFormFieldsValidationSchema, { FormFieldsValidationSchemaOptions } from './getCustomFormFieldsValidationSchema';
 
export const WHITELIST_REGEXP = /^[^<>]*$/;
 
export interface FormFieldValues {
    [key: string]: string | { [id: string]: any };
}
 
export default memoize(function getFormFieldsValidationSchema({
    formFields,
    translate = () => undefined,
}: FormFieldsValidationSchemaOptions): ObjectSchema<FormFieldValues> {
    return object({
        ...formFields
            .filter(({ custom }) => !custom)
            .reduce((schema, { name, required, label }) => {
                schema[name] = string();
 
                if (required) {
                    schema[name] = schema[name].required(translate('required', { label, name }));
                }
 
                schema[name] = schema[name].matches(
                    WHITELIST_REGEXP,
                    translate('invalid', { name, label })
                );
 
                return schema;
            },
            {} as { [key: string]: StringSchema }
        ),
    }).concat(getCustomFormFieldsValidationSchema({ formFields, translate })) as ObjectSchema<FormFieldValues>;
});