UNPKG

@malga-checkout-full/core

Version:
101 lines (96 loc) 4.88 kB
'use strict'; const settings = require('./settings-69e76429.js'); const index = require('./index-3ef97c09.js'); const i18n_es = require('./i18n.es-0d00be30.js'); const schema = (locale) => { return index.create().shape({ cardNumber: index.create$1() .required(i18n_es.Y('paymentMethods.card.newCard.fields.cardNumber.errorMessageRequired', locale)) .min(14, i18n_es.Y('paymentMethods.card.newCard.fields.cardNumber.errorMessageInvalidFormat', locale)) .max(22, i18n_es.Y('paymentMethods.card.newCard.fields.cardNumber.errorMessageInvalidFormat', locale)) .test('isNumber', i18n_es.Y('paymentMethods.card.newCard.fields.cardNumber.errorMessageInvalidFormat', locale), (value) => { if (!value.length) { return true; } return index.src_default.number(value).isValid; }), expirationDate: index.create$1() .required(i18n_es.Y('paymentMethods.card.newCard.fields.expirationDate.errorMessageRequired', locale)) .test('isValidDate', i18n_es.Y('paymentMethods.card.newCard.fields.expirationDate.errorMessageInvalidFormat', locale), (value) => { const normalizedValue = value.replace(/\D/g, '').trim(); if (!normalizedValue) return true; const [month, year] = value.split('/'); const parsedMonth = parseInt(month) - 1; const parsedYear = parseInt(`20${year}`); const today = new Date(); const months = Array.from({ length: 12 }).map((_, index) => index); const isValidMonthOfCurrentYear = parsedMonth >= today.getMonth() && months.includes(parsedMonth); const isValidMonth = parsedYear === today.getFullYear() ? isValidMonthOfCurrentYear : months.includes(parsedMonth); const isValidYear = parsedYear >= today.getFullYear(); return isValidMonth && isValidYear; }), cvv: index.create$1() .required(i18n_es.Y('paymentMethods.card.newCard.fields.cvv.errorMessageRequired', locale)) .min(3, i18n_es.Y('paymentMethods.card.newCard.fields.cvv.errorMessageInvalidFormat', locale)) .max(4, i18n_es.Y('paymentMethods.card.newCard.fields.cvv.errorMessageInvalidFormat', locale)), name: index.create$1() .required(i18n_es.Y('paymentMethods.card.newCard.fields.name.errorMessageRequired', locale)) .test('isValidName', i18n_es.Y('paymentMethods.card.newCard.fields.name.errorMessageInvalidFormat', locale), (value) => { const normalizedValue = value.replace(/[^A-Za-z]+/g, ''); const comparedValue = value.replace(/\s/g, ''); return normalizedValue.length === comparedValue.length; }), installments: index.create$1().test('isValidInstallments', i18n_es.Y('paymentMethods.card.newCard.fields.installments.errorMessageRequired', locale), (value, context) => { if (!context.options.context.hasInstallments) { return true; } return !!value && value !== 'none'; }), }); }; const handleSubmitValidation = async () => { var _a, _b, _c, _d; const hasInstallments = (_d = (_c = (_b = (_a = settings.state.paymentMethods) === null || _a === void 0 ? void 0 : _a.credit) === null || _b === void 0 ? void 0 : _b.installments) === null || _c === void 0 ? void 0 : _c.show) !== null && _d !== void 0 ? _d : false; const validation = await validateCreditForm(index.state$1.form, { hasInstallments, }); if (!validation.isValid) { index.state$1.validations.fields = Object.assign(Object.assign({}, index.state$1.validations.fields), validation.errors); const checkedIfAllFieldsIsBlank = checkIfAllFieldsIsBlank(index.state$1.form); if (checkedIfAllFieldsIsBlank) { index.state$1.validations.allFieldsIsBlank = true; } } return validation.isValid; }; const normalizeValidationErrors = (errors) => { const normalizedErrors = errors.reduce((accumulatorErrors, currentError) => (Object.assign(Object.assign({}, accumulatorErrors), { [currentError.path]: currentError.message })), {}); return normalizedErrors; }; const checkIfAllFieldsIsBlank = (data) => { const fields = Object.entries(data); const filteredBlankFieldValues = fields .map(([field, value]) => { const isMaskedField = ['expirationDate', 'cvv'].includes(field); const normalizedValue = field.replace(/\D/g, '').trim(); return isMaskedField ? normalizedValue : value; }) .filter((field) => !field || field === 'none'); return fields.length === filteredBlankFieldValues.length; }; const validateCreditForm = async (data, context, locale) => { try { const cardSchema = schema(locale); await cardSchema.validate(data, { abortEarly: false, context }); return { isValid: true }; } catch (error) { const errors = normalizeValidationErrors(error.inner); return { isValid: false, errors }; } }; exports.handleSubmitValidation = handleSubmitValidation; exports.validateCreditForm = validateCreditForm;