UNPKG

@eleva-io/erp-sdk

Version:

SDK oficial para el ERP de Eleva

347 lines 6.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VALID_SWIFT_COUNTRIES = exports.SWIFT_BIC_PATTERNS = void 0; exports.isTestBic = isTestBic; exports.formatSwiftBic = formatSwiftBic; exports.getSwiftCountry = getSwiftCountry; exports.getBankCode = getBankCode; exports.validateCodeSwiftBic = validateCodeSwiftBic; const errors_1 = require("../../../../../errors"); const invalid = (path, message) => errors_1.ElevaError.invalidField({ error: { validation: path, code: 'invalid_string', message, path: [path] }, }); // SWIFT/BIC validation patterns and functions exports.SWIFT_BIC_PATTERNS = { // Standard SWIFT/BIC format (8 or 11 characters) STANDARD: /^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/, // 8-character format (bank + country + location) FORMAT_8: /^[A-Z]{6}[A-Z0-9]{2}$/, // 11-character format (bank + country + location + branch) FORMAT_11: /^[A-Z]{6}[A-Z0-9]{2}[A-Z0-9]{3}$/, // Component patterns BANK_CODE: /^[A-Z]{4}$/, // 4 letters - Bank identifier COUNTRY_CODE: /^[A-Z]{2}$/, // 2 letters - ISO country code LOCATION_CODE: /^[A-Z0-9]{2}$/, // 2 alphanumeric - Location code BRANCH_CODE: /^[A-Z0-9]{3}$/, // 3 alphanumeric - Branch code (optional) }; // ISO country codes for SWIFT validation exports.VALID_SWIFT_COUNTRIES = new Set([ 'AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'XK', 'YE', 'YT', 'ZA', 'ZM', 'ZW', ]); function isTestBic(swiftBic) { const cleanSwift = swiftBic.replace(/\s/g, '').toUpperCase(); if (cleanSwift.length < 8) return false; return cleanSwift.substring(7, 8) === '0'; } function formatSwiftBic(swiftBic, withSpaces = false) { const cleanSwift = swiftBic.replace(/\s/g, '').toUpperCase(); if (!withSpaces) return cleanSwift; if (cleanSwift.length === 8) { return `${cleanSwift.substring(0, 4)} ${cleanSwift.substring(4, 6)} ${cleanSwift.substring(6, 8)}`; } else if (cleanSwift.length === 11) { return `${cleanSwift.substring(0, 4)} ${cleanSwift.substring(4, 6)} ${cleanSwift.substring(6, 8)} ${cleanSwift.substring(8, 11)}`; } return cleanSwift; } function getSwiftCountry(swiftBic) { const cleanSwift = swiftBic.replace(/\s/g, '').toUpperCase(); if (cleanSwift.length < 6) return null; return cleanSwift.substring(4, 6); } function getBankCode(swiftBic) { const cleanSwift = swiftBic.replace(/\s/g, '').toUpperCase(); if (cleanSwift.length < 4) return null; return cleanSwift.substring(0, 4); } // Main validation function function validateCodeSwiftBic(swiftBic, path) { if (!swiftBic || typeof swiftBic !== 'string') { throw invalid(path, 'SWIFT/BIC must be a string'); } // Remove spaces and convert to uppercase const cleanSwift = swiftBic.replace(/\s/g, '').toUpperCase(); // Check basic format if (!exports.SWIFT_BIC_PATTERNS.STANDARD.test(cleanSwift)) { throw invalid(path, 'Invalid SWIFT/BIC format. Must be 8 or 11 alphanumeric characters'); } // Extract components const bankCode = cleanSwift.substring(0, 4); const countryCode = cleanSwift.substring(4, 6); const locationCode = cleanSwift.substring(6, 8); const branchCode = cleanSwift.length === 11 ? cleanSwift.substring(8, 11) : undefined; // Validate individual components if (!exports.SWIFT_BIC_PATTERNS.BANK_CODE.test(bankCode)) { throw invalid(path, 'Invalid bank code. Must be 4 letters'); } if (!exports.SWIFT_BIC_PATTERNS.COUNTRY_CODE.test(countryCode)) { throw invalid(path, 'Invalid country code. Must be 2 letters'); } if (!exports.VALID_SWIFT_COUNTRIES.has(countryCode)) { throw invalid(path, `Invalid country code: ${countryCode}`); } if (!exports.SWIFT_BIC_PATTERNS.LOCATION_CODE.test(locationCode)) { throw invalid(path, 'Invalid location code. Must be 2 alphanumeric characters'); } if (branchCode && !exports.SWIFT_BIC_PATTERNS.BRANCH_CODE.test(branchCode)) { throw invalid(path, 'Invalid branch code. Must be 3 alphanumeric characters'); } // Test BICs (location code ending in 0) are reserved for non-live testing and rejected if (locationCode.endsWith('0')) { throw invalid(path, 'Test BICs (location code ending in 0) are not allowed'); } } //# sourceMappingURL=swift.js.map