All files / app/customer getCreateCustomerValidationSchema.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72  3x 3x   3x   3x                                     6x                                                 3x 6x 6x 6x   6x                                
import { FormField, LanguageService } from '@bigcommerce/checkout-sdk';
import { memoize } from '@bigcommerce/memoize';
import { object, string, ObjectSchema } from 'yup';
 
import { getCustomFormFieldsValidationSchema, CustomFormFieldValues, TranslateValidationErrorFunction } from '../formFields';
 
import getEmailValidationSchema from './getEmailValidationSchema';
import { PasswordRequirements } from './getPasswordRequirements';
 
export type CreateAccountFormValues = {
    firstName: string;
    lastName: string;
    email: string;
    password: string;
    acceptsMarketingEmails?: string[];
    token?: string;
} & CustomFormFieldValues;
 
export interface CreateCustomerValidationSchema {
    formFields: FormField[];
    language: LanguageService;
    passwordRequirements: PasswordRequirements;
}
 
function getTranslateCreateCustomerError(language?: LanguageService): TranslateValidationErrorFunction {
    return (type, { label, min, max }) => {
        if (!language) {
            return;
        }
 
        if (type === 'required') {
            return language.translate('customer.required_error', { label });
        }
 
        if (type === 'max' && max) {
            return language.translate('customer.max_error', { label, max });
        }
 
        if (type === 'min' && min) {
            return language.translate('customer.min_error', { label, min });
        }
 
        if (type === 'invalid') {
            return language.translate('customer.invalid_characters_error', { label });
        }
 
        return;
    };
}
 
export default memoize(function getCreateCustomerValidationSchema({
    formFields,
    language,
    passwordRequirements: { description, numeric, alpha, minLength },
}: CreateCustomerValidationSchema): ObjectSchema<CreateAccountFormValues> {
    return object({
            firstName: string().required(language.translate('address.first_name_required_error')),
            lastName: string().required(language.translate('address.last_name_required_error')),
            password: string()
                .required(description || language.translate('customer.password_required_error'))
                .matches(numeric, description || language.translate('customer.password_number_required_error'))
                .matches(alpha, description || language.translate('customer.password_letter_required_error'))
                .min(minLength, description || language.translate('customer.password_under_minimum_length_error'))
                .max(100, language.translate('customer.password_over_maximum_length_error')),
        })
        .concat(getEmailValidationSchema({ language }))
        .concat(getCustomFormFieldsValidationSchema({
            formFields,
            translate: getTranslateCreateCustomerError(language),
        }));
});