UNPKG

vies-checker

Version:

Modern European VIES VAT number validator with full TypeScript support

79 lines 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateInput = validateInput; const errors_js_1 = require("./errors.js"); /** * Valid EU member state codes */ const VALID_COUNTRIES = [ 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES', 'EU', 'FI', 'FR', 'HR', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK', 'XI', ]; /** * Validates country code format (2 uppercase letters) */ function isValidCountryCode(country) { return /^[A-Z]{2}$/.test(country); } /** * Validates VAT number format (alphanumeric, 2-12 characters) */ function isValidVatNumber(vatNumber) { return /^[A-Z0-9]{2,12}$/i.test(vatNumber); } /** * Validates and normalizes input parameters * * @param country - Country code (will be normalized to uppercase) * @param vatNumber - VAT number (will be normalized by removing spaces/separators) * @returns Normalized and validated inputs * @throws {ViesError} If inputs are invalid */ function validateInput(country, vatNumber) { // Normalize inputs const normalizedCountry = country.trim().toUpperCase(); const normalizedVatNumber = vatNumber.trim().replace(/[\s.-]/g, ''); // Validate country code format if (!isValidCountryCode(normalizedCountry)) { throw new errors_js_1.ViesError('INVALID_INPUT', `Invalid country code format: ${country}`); } // Check if valid EU member state if (!VALID_COUNTRIES.includes(normalizedCountry)) { throw new errors_js_1.ViesError('INVALID_INPUT', `Unknown country code: ${normalizedCountry}`); } // Validate VAT number format if (!isValidVatNumber(normalizedVatNumber)) { throw new errors_js_1.ViesError('INVALID_INPUT', `Invalid VAT number format: ${vatNumber}. Must be alphanumeric, 2-12 characters.`); } return { country: normalizedCountry, vatNumber: normalizedVatNumber, }; } //# sourceMappingURL=validators.js.map