All files / app/payment/creditCard getCreditCardValidationSchema.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  52x 52x 52x                 52x 16x 16x   16x             17x                 17x       16x 7x         9x   9x         16x    
import { LanguageService } from '@bigcommerce/checkout-sdk';
import { memoize } from '@bigcommerce/memoize';
import { cvv, expirationDate, number } from 'card-validator';
import { object, string, ObjectSchema } from 'yup';
 
import { CreditCardFieldsetValues } from './CreditCardFieldset';
 
export interface CreditCardValidationSchemaOptions {
    isCardCodeRequired: boolean;
    language: LanguageService;
}
 
export default memoize(function getCreditCardValidationSchema({
    isCardCodeRequired,
    language,
}: CreditCardValidationSchemaOptions): ObjectSchema<CreditCardFieldsetValues> {
    const schema = {
        ccCustomerCode: string(),
        ccCvv: string(),
        ccExpiry: string()
            .required(language.translate('payment.credit_card_expiration_required_error'))
            .test({
                message: language.translate('payment.credit_card_expiration_invalid_error'),
                test: value => expirationDate(value).isValid,
            }),
        ccName: string()
            .max(200)
            .required(language.translate('payment.credit_card_name_required_error')),
        ccNumber: string()
            .required(language.translate('payment.credit_card_number_required_error'))
            .test({
                message: language.translate('payment.credit_card_number_invalid_error'),
                test: value => number(value).isValid,
            }),
    };
 
    if (isCardCodeRequired) {
        schema.ccCvv = string()
            .required(language.translate('payment.credit_card_cvv_required_error'))
            .test({
                message: language.translate('payment.credit_card_cvv_invalid_error'),
                test(value) {
                    const { card } = number(this.parent.ccNumber);
 
                    return cvv(value, card && card.code ? card.code.size : undefined).isValid;
                },
            });
    }
 
    return object(schema);
});