All files / app/address getAddressFormFieldsValidationSchema.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  26x     26x             26x 388x                           4222x 4222x 1741x     2481x 765x 743x   22x       1716x       1716x       1716x 1716x             26x 388x 388x   388x          
import { FormField, LanguageService } from '@bigcommerce/checkout-sdk';
import { memoize } from 'lodash';
import { ObjectSchema } from 'yup';
 
import { getFormFieldsValidationSchema, FormFieldValues, TranslateValidationErrorFunction } from '../formFields';
 
export interface AddressFormFieldsValidationSchemaOptions {
    formFields: FormField[];
    language?: LanguageService;
}
 
export function getTranslateAddressError(language?: LanguageService): TranslateValidationErrorFunction {
    const requiredFieldErrorTranslationIds: { [fieldName: string]: string } = {
        countryCode: 'address.country',
        firstName: 'address.first_name',
        lastName: 'address.last_name',
        company: 'address.company_name',
        address1: 'address.address_line_1',
        address2: 'address.address_line_2',
        city: 'address.city',
        stateOrProvince: 'address.state',
        stateOrProvinceCode: 'address.state',
        postalCode: 'address.postal_code',
        phone: 'address.phone_number',
    };
 
    return (type, { label, name, min, max }) => {
        if (!language) {
            return;
        }
 
        if (type === 'required') {
            if (requiredFieldErrorTranslationIds[name]) {
                return language.translate(`${requiredFieldErrorTranslationIds[name]}_required_error`);
            } else {
                return language.translate(`address.custom_required_error`, { label });
            }
        }
 
        Iif (type === 'max' && max) {
            return language.translate(`address.custom_max_error`, { label, max });
        }
 
        Iif (type === 'min' && min) {
            return language.translate(`address.custom_max_error`, { label, min });
        }
 
        Eif (type === 'invalid') {
            return language.translate(`address.invalid_characters_error`, { label });
        }
 
        return;
    };
}
 
export default memoize(function getAddressFormFieldsValidationSchema({
    formFields,
    language,
}: AddressFormFieldsValidationSchemaOptions): ObjectSchema<FormFieldValues> {
    return getFormFieldsValidationSchema({
        formFields,
        translate: getTranslateAddressError(language),
    });
});