import * as AD from './ad'; import * as AF from './af'; import * as AG from './ag'; import * as AI from './ai'; import * as AL from './al'; import * as AM from './am'; import * as AO from './ao'; import * as AR from './ar'; import * as AT from './at'; import * as AU from './au'; import * as AW from './aw'; import * as AZ from './az'; import * as BA from './ba'; import * as BB from './bb'; import * as BD from './bd'; import * as BE from './be'; import * as BG from './bg'; import * as BH from './bh'; import * as BO from './bo'; import * as BR from './br'; import * as BS from './bs'; import * as BY from './by'; import * as BZ from './bz'; import * as CA from './ca'; import * as CH from './ch'; import * as CI from './ci'; import * as CK from './ck'; import * as CL from './cl'; import * as CM from './cm'; import * as CN from './cn'; import * as CO from './co'; import * as CR from './cr'; import * as CU from './cu'; import * as CW from './cw'; import * as CY from './cy'; import * as CZ from './cz'; import * as DE from './de'; import * as DK from './dk'; import * as DM from './dm'; import * as DO from './do'; import * as DZ from './dz'; import * as EC from './ec'; import * as EE from './ee'; import * as EG from './eg'; import * as ES from './es'; import * as ET from './et'; import * as FI from './fi'; import * as FJ from './fj'; import * as FO from './fo'; import * as FR from './fr'; import * as GB from './gb'; import * as GD from './gd'; import * as GE from './ge'; import * as GH from './gh'; import * as GN from './gn'; import * as GR from './gr'; import * as GT from './gt'; import * as GY from './gy'; import * as HK from './hk'; import * as HR from './hr'; import * as HT from './ht'; import * as HU from './hu'; import * as ID from './id'; import * as IE from './ie'; import * as IL from './il'; import * as IN from './in'; import * as IS from './is'; import * as IT from './it'; import * as JM from './jm'; import * as JP from './jp'; import * as KE from './ke'; import * as KH from './kh'; import * as KN from './kn'; import * as KR from './kr'; import * as KZ from './kz'; import * as LA from './la'; import * as LB from './lb'; import * as LC from './lc'; import * as LI from './li'; import * as LK from './lk'; import * as LT from './lt'; import * as LU from './lu'; import * as LV from './lv'; import * as MA from './ma'; import * as MC from './mc'; import * as MD from './md'; import * as ME from './me'; import * as MK from './mk'; import * as MN from './mn'; import * as MT from './mt'; import * as MU from './mu'; import * as MX from './mx'; import * as MY from './my'; import * as MZ from './mz'; import * as NG from './ng'; import * as NL from './nl'; import * as NO from './no'; import * as NP from './np'; import * as NZ from './nz'; import * as OM from './om'; import * as PE from './pe'; import * as PG from './pg'; import * as PH from './ph'; import * as PK from './pk'; import * as PL from './pl'; import * as PT from './pt'; import * as PY from './py'; import * as QA from './qa'; import * as RO from './ro'; import * as RS from './rs'; import * as RU from './ru'; import * as SB from './sb'; import * as SC from './sc'; import * as SE from './se'; import * as SG from './sg'; import * as SI from './si'; import * as SK from './sk'; import * as SM from './sm'; import * as SN from './sn'; import * as SR from './sr'; import * as SV from './sv'; import * as TH from './th'; import * as TJ from './tj'; import * as TM from './tm'; import * as TN from './tn'; import * as TO from './to'; import * as TR from './tr'; import * as TT from './tt'; import * as TW from './tw'; import * as TZ from './tz'; import * as UA from './ua'; import * as UG from './ug'; import * as US from './us'; import * as UY from './uy'; import * as VC from './vc'; import * as VE from './ve'; import * as VN from './vn'; import * as WS from './ws'; import * as YE from './ye'; import * as ZA from './za'; import * as ZM from './zm'; import type { ValidateReturn, Validator } from './types'; export type { Validator } from './types'; // Named per-country exports. Unlike the `stdnum` object below (which eagerly // references every country and therefore always bundles all of them), these are // tree-shakeable: `import { GB } from 'stdnum'` lets a bundler drop the other // countries entirely. export { AD, AF, AG, AI, AL, AM, AO, AR, AT, AU, AW, AZ, BA, BB, BD, BE, BG, BH, BO, BR, BS, BY, BZ, CA, CH, CI, CK, CL, CM, CN, CO, CR, CU, CW, CY, CZ, DE, DK, DM, DO, DZ, EC, EE, EG, ES, ET, FI, FJ, FO, FR, GB, GD, GE, GH, GN, GR, GT, GY, HK, HR, HT, HU, ID, IE, IL, IN, IS, IT, JM, JP, KE, KH, KN, KR, KZ, LA, LB, LC, LI, LK, LT, LU, LV, MA, MC, MD, ME, MK, MN, MT, MU, MX, MY, MZ, NG, NL, NO, NP, NZ, OM, PE, PG, PH, PK, PL, PT, PY, QA, RO, RS, RU, SB, SC, SE, SG, SI, SK, SM, SN, SR, SV, TH, TJ, TM, TN, TO, TR, TT, TW, TZ, UA, UG, US, UY, VC, VE, VN, WS, YE, ZA, ZM, }; // Live an uppercase world, to prevent keyword collisions export const stdnum: Record> = { AD, AF, AG, AI, AL, AM, AO, AR, AT, AU, AW, AZ, BA, BB, BD, BE, BG, BH, BO, BR, BS, BY, BZ, CA, CH, CI, CK, CL, CM, CN, CO, CR, CU, CW, CY, CZ, DE, DK, DM, DO, DZ, EC, EE, EG, ES, ET, FI, FJ, FO, FR, GB, GD, GE, GH, GN, GR, GT, GY, HK, HR, HT, HU, ID, IE, IL, IN, IS, IT, JM, JP, KE, KH, KN, KR, KZ, LA, LB, LC, LI, LK, LT, LU, LV, MA, MC, MD, ME, MK, MN, MT, MU, MX, MY, MZ, NG, NL, NO, NP, NZ, OM, PE, PG, PH, PK, PL, PT, PY, QA, RO, RS, RU, SB, SC, SE, SG, SI, SK, SM, SN, SR, SV, TH, TJ, TM, TN, TO, TR, TT, TW, TZ, UA, UG, US, UY, VC, VE, VN, WS, YE, ZA, ZM, }; export const personValidators: Record = { AD: [AD.nrt], AF: [AF.tin], AI: [AI.tin], AL: [AL.nipt], AM: [AM.tin], AO: [AO.nif, AO.bi], AR: [AR.cuit, AR.dni], AT: [AT.vnr], AU: [AU.tfn], AW: [AW.tin], AZ: [AZ.pin, AZ.tin], BA: [BA.jmbg], BB: [BB.tin], BD: [BD.nid], BE: [BE.bis, BE.insz, BE.nn], BG: [BG.egn, BG.pnf, BG.vat], BR: [BR.cpf], BY: [BY.unp], BZ: [BZ.tin], CA: [CA.sin], CH: [CH.ssn], CK: [CK.tin], CL: [CL.run], // which is the same as RUT CN: [CN.ric], CO: [CO.nit], CR: [CR.cpf, CR.cr], CU: [CU.ni], CW: [CW.tin], CZ: [CZ.rc], DE: [DE.idnr, DE.svnr], DK: [DK.cpr], DO: [DO.cedula], EC: [EC.ci], EE: [EE.ik], ES: [ES.dni, ES.nie], ET: [ET.nid], FI: [FI.hetu], FJ: [FJ.tin], FR: [FR.nif, FR.nir], GB: [GB.nino, GB.utr], GD: [GD.tin], GE: [GE.nid, GE.tin], GH: [GH.tin], GR: [GR.amka], GT: [GT.cui], HK: [HK.hkid], HR: [HR.oib], HU: [HU.anum], ID: [ID.nik, ID.npwp], IE: [IE.pps], IL: [IL.idnr], IN: [IN.aadhaar], IS: [IS.kennitala], IT: [IT.codicefiscale], JM: [JM.tin], KR: [KR.rrn], LI: [LI.peid], LT: [LT.asmens], LV: [LV.pvn], ME: [ME.jmbg], MK: [MK.jmbg], MU: [MU.nid], MX: [MX.curp, MX.rfc], MY: [MY.nric], MZ: [MZ.bi, MZ.nuit], NG: [NG.nid], NL: [NL.onderwijsnummer, NL.bsn], NO: [NO.fodselsnummer], NP: [NP.nid], NZ: [NZ.ird], PE: [PE.cui, PE.ce], PK: [PK.cnic], PL: [PL.pesel], PT: [PT.nif], PY: [PY.ruc, PY.cedula], RO: [RO.cnp], RS: [RS.jmbg], RU: [RU.inn], SC: [SC.tin], SE: [SE.personnummer], SI: [SI.jmbg], SK: [SK.rc], SV: [SV.nit], TH: [TH.idnr], TR: [TR.tckimlik], TT: [TT.nid], TW: [TW.ubn], TZ: [TZ.nid], UA: [UA.rntrc], US: [US.ssn], UY: [UY.nie, UY.cedula], VN: [VN.mst], ZA: [ZA.tin, ZA.idnr], }; export const entityValidators: Record = { AD: [AD.nrt], AF: [AF.tin], AI: [AI.tin], AL: [AL.nipt], AM: [AM.tin], AO: [AO.nif], AR: [AR.cuit], AT: [AT.businessid, AT.tin, AT.uid], AU: [AU.abn, AU.acn, AU.tfn], AW: [AW.tin], AZ: [AZ.tin], BA: [BA.jib], BB: [BB.tin], BE: [BE.vat], BG: [BG.vat], BH: [BH.vat], BR: [BR.cnpj], BY: [BY.unp], BZ: [BZ.tin], CA: [CA.bn], CH: [CH.uid, CH.vat], CK: [CK.tin], CL: [CL.rut], CN: [CN.uscc], CO: [CO.nit], CR: [CR.cpj], CW: [CW.tin], CY: [CY.vat], CZ: [CZ.dic], DE: [DE.stnr, DE.vat], DK: [DK.cvr], DO: [DO.ncf, DO.rnc], EC: [EC.ruc], EE: [EE.kmkr, EE.registrikood], ES: [ES.cif], FI: [FI.alv, FI.ytunnus], FJ: [FJ.tin], FR: [FR.siren, FR.siret, FR.tva], GB: [GB.vat], GD: [GD.tin], GE: [GE.tin], GR: [GR.vat], GT: [GT.nit], HU: [HU.anum], ID: [ID.npwp], IE: [IE.vat], IL: [IL.hp], IN: [IN.pan], IS: [IS.kennitala, IS.vsk], IT: [IT.iva], JM: [JM.tin], JP: [JP.cn], KR: [KR.brn], LI: [LI.peid], LT: [LT.pvm], LU: [LU.tva], LV: [LV.pvn], MA: [MA.ice, MA.ice9], MC: [MC.tva], MD: [MD.idno], MT: [MT.vat], MX: [MX.rfc], MZ: [MZ.nuit], NG: [NG.tin], NL: [NL.btw], NO: [NO.mva, NO.orgnr], NZ: [NZ.ird], PE: [PE.ruc], PH: [PH.tin], PK: [PK.ntn], PL: [PL.nip, PL.regon], PT: [PT.nipc], PY: [PY.ruc], RO: [RO.onrc, RO.cui], RS: [RS.pib], RU: [RU.inn], SC: [SC.tin], SE: [SE.orgnr, SE.vat], SG: [SG.uen], SI: [SI.ddv], SK: [SK.dph], SM: [SM.coe], SV: [SV.nit], TR: [TR.vkn], TT: [TT.nid], TW: [TW.ubn], UA: [UA.edrpou], US: [US.ein], UY: [UY.rut], VN: [VN.mst], ZA: [ZA.tin, ZA.vat], }; /** * https://en.wikipedia.org/wiki/VAT_identification_number */ export const euVat: Record = { AD: [AD.nrt], AT: [AT.uid], BE: [BE.vat], BG: [BG.vat], CH: [CH.vat], HR: [HR.oib], CY: [CY.vat], DE: [DE.vat], CZ: [CZ.dic], DK: [DK.cvr], ES: [ES.nif], EE: [EE.kmkr], FI: [FI.alv], FR: [FR.tva], GR: [GR.vat], HU: [HU.anum], IE: [IE.vat], IT: [IT.iva], LT: [LT.pvm], LU: [LU.tva], LV: [LV.pvn], MT: [MT.vat], NL: [NL.btw], PL: [PL.nip], PT: [PT.nif], // same as PR.nipc RO: [RO.cif], SE: [SE.vat], SI: [SI.ddv], SK: [SK.dph], }; /** * Apply the necessary validators for a given country to validate an ID number */ export function validatePerson( country: string, value: string, ): { checked: boolean; isValid?: boolean; matchedValidators?: Validator[] } { const vset = personValidators[country.toLocaleUpperCase()]; if (!vset || vset.length === 0) { return { checked: false }; } const match = vset.filter(grp => grp.validate(value).isValid); return { checked: true, isValid: match.length > 0, matchedValidators: match }; } /** * Apply the necessary validators for a given country to validate an Entity (Business) ID number */ export function validateEntity( country: string, value: string, ): { checked: boolean; isValid?: boolean; matchedValidators?: Validator[] } { const vset = entityValidators[country.toLocaleUpperCase()]; if (!vset || vset.length === 0) { return { checked: false }; } const match = vset.filter(grp => grp.validate(value).isValid); return { checked: true, isValid: match.length > 0, matchedValidators: match }; } /** * Apply the VAT validator for a given country to validate a VAT number. * * Covers the countries in `euVat`, which besides the EU member states * includes some non-EU countries (such as AD and CH). A country that is not * in `euVat` returns `{ checked: false }`, the same result `validateEntity` * gives for a country it does not know. * * This only reads `euVat`, so a bundler can leave out the much larger * `stdnum`, `personValidators` and `entityValidators` tables. */ export function validateVat( country: string, value: string, ): { checked: boolean; isValid?: boolean; matchedValidators?: Validator[] } { const vset = euVat[country.toUpperCase()]; if (!vset || vset.length === 0) { return { checked: false }; } const match = vset.filter(grp => grp.validate(value).isValid); return { checked: true, isValid: match.length > 0, matchedValidators: match }; } /** * Validate a passport number for a given country. * Like validatePerson, returns `{ checked: false }` when the country has no * passport validator, so the caller decides what to do with the value. * * @param countryCode - ISO 3166-1 alpha-2 country code (e.g., 'BR', 'US', 'DE') * @param value - Passport number to validate */ export function validatePassport( countryCode: string, value: string, ): { checked: false } | ({ checked: true } & ValidateReturn) { const validator = stdnum[countryCode.toUpperCase()]?.passport; if (!validator) { return { checked: false }; } return { checked: true, ...validator.validate(value) }; } /** * Format a passport number for a given country. * Returns the value unchanged if the country is unsupported. */ export function formatPassport(countryCode: string, value: string): string { const country = stdnum[countryCode.toUpperCase()]; if (!country || !country.passport) { return value; } return country.passport.format(value); } /** * Compact a passport number for a given country. * Returns the value unchanged if the country is unsupported. */ export function compactPassport(countryCode: string, value: string): string { const country = stdnum[countryCode.toUpperCase()]; if (!country || !country.passport) { return value; } return country.passport.compact(value); }