@blocklet/payment-react
Version:
Reusable react components for payment kit v2
34 lines (33 loc) • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getFieldValidation = getFieldValidation;
exports.validatePostalCode = validatePostalCode;
var _isPostalCode = _interopRequireDefault(require("validator/lib/isPostalCode"));
var _locales = require("../locales");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const POSTAL_CODE_SUPPORTED_COUNTRIES = ["AD", "AT", "AU", "BE", "BG", "BR", "CA", "CH", "CN", "CZ", "DE", "DK", "DZ", "EE", "ES", "FI", "FR", "GB", "GR", "HR", "HU", "ID", "IE", "IL", "IN", "IR", "IS", "IT", "JP", "KE", "KR", "LI", "LT", "LU", "LV", "MX", "MT", "NL", "NO", "NZ", "PL", "PR", "PT", "RO", "RU", "SA", "SE", "SI", "SK", "TN", "TW", "UA", "US", "ZA", "ZM"];
function validatePostalCode(postalCode, country) {
if (!postalCode) return true;
const countryUpper = country?.toUpperCase();
const isSupported = country && POSTAL_CODE_SUPPORTED_COUNTRIES.includes(countryUpper);
try {
return (0, _isPostalCode.default)(postalCode, isSupported ? countryUpper : "any");
} catch (error) {
console.error(error);
return false;
}
}
function getFieldValidation(fieldName, validations, locale = "en") {
if (!validations || !validations[fieldName]) return {};
const fieldValidation = validations[fieldName];
const rules = {};
if (fieldValidation.pattern) {
rules.pattern = {
value: new RegExp(fieldValidation.pattern),
message: fieldValidation.pattern_message?.[locale] || (0, _locales.t)("payment.checkout.invalid", locale)
};
}
return rules;
}