English ยท Espaรฑol ยท Portuguรชs
TypeScript-first, zero-dependency validator for national identity and tax documents from every country.
๐ฎ Live playground: https://lu1tr0n.github.io/nationid_example/ โ try every country, every helper, in 3 locales. ๐ API Reference: https://lu1tr0n.github.io/nationid/ ๐ Benchmarks: see BENCHMARKS.md
nationid is a focused, comprehensive library for validating national identity documents and tax IDs. It ships with checksum verification (not just regex shape), proper formatting and normalization, and works in Node, browsers, Bun, Deno and edge runtimes.
Existing tools cover a fraction of the world. validator.js only validates 6 LATAM tax IDs. cpf-cnpj-validator covers Brazil. rut.js covers Chile. None ship El Salvador, Guatemala, Honduras, Dominican Republic, or Costa Rica with checksum verification.
nationid fills that gap. As of v2.2 it ships 54 countries with ~145 document codes, all with proper algorithms documented from official sources โ and ships an API-stability promise plus a CI-enforced governance test that every high-confidence spec cites a first-party issuer source.
- ๐ธ๐ฌ Singapore โ second country of Asia phase 2. Three new specs under
nationid/sg:SG_NRIC(9-char identity number for citizens/PRs, weighted mod-11 check letter, ICA) andSG_FIN(9-char foreigner identity number incl. the 2022 M-series, ICA/MOM), bothpersonal; andSG_UEN(Unique Entity Number, three category formats each with its own check letter, ACRA),tax. All three atconfidence: "high". - UEN check digits โ
SG_UENupgrades from format-only to full check-letter validation across all three categories (Business, Local Company, Other Entity), with the 38-code entity-type whitelist. Constants are taken verbatim frompython-stdnum/stdnum/sg/uen.py; the four doctest fixtures (00192200M,197401143C,S16FC0121D,T01FC6132D) anchor the suite, alongside the real-world Cat B UENs196800306E(DBS) and199201624D(Singtel). - Oracle-agreement tests โ every SG spec runs a property test against an independent re-implementation of the published algorithm (NRIC/FIN weighted mod-11, plus the three UEN categories). Singapore's authoritative URLs are all on
*.gov.sghosts, so the governance citation test passes without a host-allowlist patch. - 7000+ โ 7800+ tests, 54 countries ร 145 codes.
- ๐ฏ๐ต Japan โ first country of Asia phase 2. Two new specs under
nationid/jp:JP_MY_NUMBER(12-digit Individual Number, weighted mod-11, MIC Ordinance 85/2014) andJP_CORPORATE_NUMBER(13-digit Corporate Number, weighted mod-9, NTA). Both atconfidence: "high". The NTA's own corporate number7000012050002is verifiable in the public registry and is used as a canonical anchor in the test suite. - Oracle-agreement tests โ every JP spec runs a 10k-sample property test against an inline port of
python-stdnum/stdnum/jp/in_.py(My Number) andpython-stdnum/stdnum/jp/cn.py(Corporate Number). Independent cross-validation built into CI. - 6970+ โ 7000+ tests, 53 countries ร ~147 codes.
- ๐ช๐บ EU-VAT complete โ 16 EU members + 1 EEA participant ship VAT validators in one batched release:
IE_VAT,AT_UID,LU_VAT,GR_VAT,CZ_DIC,HU_VAT,RO_VAT,BG_VAT,HR_OIB,SK_VAT,SI_VAT,LT_VAT,LV_VAT,EE_VAT,MT_VAT,CY_VAT,IS_VSK. Unlocks EU VIES feature parity as a single tagline. - ISO/IEC 7064 MOD 11,10 primitive โ
mod11_10CheckDigit+mod11_10Validexported fromnationid/algorithms. Length-generic; used by HR_OIB, DE_USTID, DE_STEUER_ID. - Greek
EL/GRprefix handling built in โ accept both on input, normalise toEL(canonical VIES form). Closes the #1 historical EU-VAT bug. - Every URL in every v2.0+ JSDoc verified live via
browser_fetch(firefox133 TLS impersonation) before publish โ fixes 5 broken URLs caught in shipped India v1.2 and 3 in v2.0.web.archive.orgsnapshots accepted as supplementary citation where issuer cert blocks programmatic checks. Pre-v2.0 specs are being back-filled to the same standard on a country-by-country basis as part of the post-v2.1 architecture audit.
- ๐ฎ๐ณ India support โ first Asia country. Five new specs under
nationid/in:IN_AADHAAR(Verhoeff + palindrome reject, UIDAI),IN_VID(16-digit Aadhaar alias),IN_PAN(entity-type whitelist, Income Tax Department),IN_GSTIN(Luhn mod-36 + embedded PAN + state code, CBIC),IN_EPIC(format-only, ECI). - Verhoeff primitive โ
verhoeffValidandverhoeffCheckDigitexported fromnationid/algorithms. Canonical Dโ multiplication and permutation tables verbatim from Verhoeff (1969). - 6488 โ 6606 tests, 35 countries ร ~125 codes. Tarball 2.9 MB unpacked (+200 KB).
- Country catalog under
nationid/catalogโgetCountryInfo,listCountries,countryName,flagEmoji. Backed byIntl.DisplayNames(CLDR), so any locale the runtime supports works out of the box โ not justes / en / pt.
- TypeScript inference upgraded.
parse("MX_CURP", x).codenow infers the literal"MX_CURP", not the 124-member union.extractDOB / extractSex / extractRegionconstrain their first argument to the codes that actually encode each field. Country bundles expose literalcountry/defaultPersonal/defaultTaxtypes. - 76% smaller npm tarball. From 1.7 MB โ 414 KB by dropping shipped sourcemaps and breaking the
extract/pii/catalogsubpaths' dependency on the root REGISTRY.nationid/extractalone drops -90% raw. - Three small breaking changes, all documented in
MIGRATION.mdยง0:pii.masknow throws on unknown codes (symmetric withhash/lastN);package.jsonexports denies undocumented subpaths;CA_PASAPORTEandES_PASAPORTEconfidence demotehigh โ moderate(no first-party issuer spec to cite). - Governance test. New
tests/governance/confidence-citations.test.tsfails CI if anyconfidence: "high"spec lacks an issuer-TLD URL or recognized legal statute in its JSDoc header.
See MIGRATION.md ยง0 before upgrading from v0.x.
npm i nationid
# or
pnpm add nationid
# or
yarn add nationidRequires Node 20+.
import { validate, format, normalize, parse } from "nationid";
validate("SV_DUI", "04567890-3"); // true
validate("BR_CPF", "529.982.247-25"); // true
validate("CL_RUT", "12.345.678-5"); // true
validate("ES_DNI", "12345678Z"); // true
format("SV_DUI", "045678903"); // "04567890-3"
normalize("SV_DUI", "04567890-3"); // "045678903"
const result = parse("SV_NIT", "0614-150585-101-5");
if (result.ok) {
console.log(result.normalized); // "06141505851015"
console.log(result.formatted); // "0614-150585-101-5"
console.log(result.confidence); // "moderate"
}parse() returns a discriminated union โ no exceptions are thrown from the
public API. On failure it carries a typed reason.kind:
const r = parse("SV_DUI", "");
if (!r.ok) r.reason.kind; // "empty" | "too_short" | "too_long" | "invalid_format" | "invalid_checksum"Try every country and every helper without installing anything: https://lu1tr0n.github.io/nationid_example/
The playground covers:
- โ
validate / parse / format / normalizefor every supported country - ๐ฏ
extract(DOB, sex, region) where the document encodes it - ๐
piimasking + SHA-256 hashing for safe display and storage - ๐
i18nerror messages ines,en,pt - ๐
catalogโ queryable document metadata for UI dropdowns - 6 best-practice code examples (React forms, server validation, KYC display, etc.)
Source code for the showcase site: https://github.com/lu1tr0n/nationid_example
Single country, ~3-5KB gzipped:
import { validate } from "nationid/sv";
validate("DUI", "045678903");Algorithm primitives:
import { luhnValid, mod11WeightedSum } from "nationid/algorithms";Four tree-shakable modules for common app needs (available since v0.3):
// Extract structured data from valid documents
import { extractDOB, extractSex, extractRegion } from "nationid/extract";
extractDOB("MX_CURP", "GOMC850315HDFRRR07"); // { year: 1985, month: 3, day: 15 }
extractSex("AR_CUIT", "20-12345678-3"); // "M"
// Mask + hash for safe display and storage
import { mask, hash, lastN } from "nationid/pii";
mask("BR_CPF", "12345678901"); // "***.***.**9-01"
await hash("BR_CPF", "12345678901", { salt: "tenant" }); // hex SHA-256
// Localized error messages (es, en, pt)
import { getErrorMessage } from "nationid/i18n";
getErrorMessage({ kind: "too_short" }, "es", "DUI"); // "El DUI es demasiado corto."
// Document catalog with localized names โ for UI dropdowns
import { listDocuments } from "nationid/catalog";
listDocuments("MX", "es");
// [{ code: "MX_CURP", displayName: "CURP",
// longName: "Clave รnica de Registro de Poblaciรณn",
// purpose: "identity", confidence: "high", ... }, ...]
// Country catalog (v1.1) โ names + flags for every supported country.
// Uses Intl.DisplayNames (CLDR) so any locale the runtime supports works.
import { getCountryInfo, listCountries, flagEmoji } from "nationid/catalog";
getCountryInfo("MX", "es");
// { code: "MX", alpha3: "MEX", name: "Mรฉxico", flag: "๐ฒ๐ฝ" }
flagEmoji("BR"); // "๐ง๐ท"
listCountries("pt").length; // 54Each subpath is independently tree-shakable. Single locales (nationid/i18n/es, /en, /pt) ship as <200B bundles.
| Country | Personal | Tax |
|---|---|---|
| ๐ธ๐ป El Salvador | DUI | NIT |
| ๐ฒ๐ฝ Mรฉxico | CURP, Clave de Elector | RFC (PF + PM) |
| ๐จ๐ด Colombia | CC, CE, TI, Pasaporte, PEP, PPT | NIT |
| ๐ง๐ท Brasil | CPF, CNH, Tรญtulo de Eleitor | CNPJ, PIS |
| ๐ต๐ช Perรบ | DNI, CE | RUC |
| ๐ฆ๐ท Argentina | DNI, CUIL | CUIT, CDI |
| ๐จ๐ฑ Chile | RUT/RUN | RUT/RUN |
| ๐ฉ๐ด Rep. Dominicana | Cรฉdula | RNC |
| ๐ฌ๐น Guatemala | DPI | NIT |
| ๐ญ๐ณ Honduras | DNI | RTN |
| ๐จ๐ท Costa Rica | Cรฉdula fรญsica, DIMEX | Cรฉdula jurรญdica |
| ๐ช๐ธ Espaรฑa | DNI, NIE | NIF (CIF), NUSS |
| ๐บ๐ธ United States | SSN, ITIN | EIN |
| ๐ง๐ด Bolivia (v0.4) | CI | NIT |
| ๐ช๐จ Ecuador (v0.4) | Cรฉdula | RUC |
| ๐ต๐พ Paraguay (v0.4) | CI | RUC |
| ๐ณ๐ฎ Nicaragua (v0.4) | Cรฉdula | RUC |
| ๐ต๐ฆ Panamรก (v0.4) | Cรฉdula | RUC |
| ๐บ๐พ Uruguay (v0.4) | CI | RUT |
| ๐จ๐ฆ Canadรก (v0.4) | SIN | BN |
| ๐ต๐น Portugal (v0.4) | CC | NIF |
| ๐ป๐ช Venezuela (v0.4) | Cรฉdula | RIF |
| ๐ฌ๐ง United Kingdom (v0.6) | NINO, NHS Number | UTR, VAT |
| ๐ซ๐ท France (v0.6) | NIR | SIREN, SIRET, TVA |
| ๐ฉ๐ช Germany (v0.6) | Steuer-ID | Steuernummer, USt-IdNr |
| ๐ฎ๐น Italy (v0.6) | Codice Fiscale | Partita IVA |
| ๐ณ๐ฑ Netherlands (v0.6) | BSN | BTW |
| ๐ง๐ช Belgium (v0.6) | NRN | BTW |
| ๐จ๐ญ Switzerland (v0.6) | AHV | UID, MWST |
| ๐ต๐ฑ Poland (v0.6) | PESEL | NIP, REGON |
| ๐ธ๐ช Sweden (v0.6) | Personnummer | Organisationsnummer, Moms |
| ๐ณ๐ด Norway (v0.6) | Fรธdselsnummer, D-nummer | Organisasjonsnummer, MVA |
| ๐ฉ๐ฐ Denmark (v0.6) | CPR | CVR, Moms |
| ๐ซ๐ฎ Finland (v0.6) | HETU | Y-tunnus, ALV |
| ๐ฎ๐ณ India (v1.2) | Aadhaar, VID, Voter ID (EPIC) | PAN, GSTIN |
| ๐ฏ๐ต Japan (v2.1) | My Number | Corporate Number |
| ๐ธ๐ฌ Singapore (v2.2) | NRIC, FIN | UEN |
| ๐ฎ๐ช Ireland (v2.0) | โ | VAT |
| ๐ฆ๐น Austria (v2.0) | โ | UID (USt-IdNr) |
| ๐ฑ๐บ Luxembourg (v2.0) | โ | TVA |
| ๐ฌ๐ท Greece (v2.0) | โ | VAT (AFM, VIES prefix EL) |
| ๐จ๐ฟ Czechia (v2.0) | โ | DIฤ (legal entity) |
| ๐ญ๐บ Hungary (v2.0) | โ | VAT (kรถzรถssรฉgi adรณszรกm) |
| ๐ท๐ด Romania (v2.0) | โ | VAT (CUI / CIF) |
| ๐ง๐ฌ Bulgaria (v2.0) | โ | VAT (legal entity) |
| ๐ญ๐ท Croatia (v2.0) | OIB | OIB |
| ๐ธ๐ฐ Slovakia (v2.0) | โ | VAT (Iฤ DPH) |
| ๐ธ๐ฎ Slovenia (v2.0) | โ | VAT (DDV) |
| ๐ฑ๐น Lithuania (v2.0) | โ | VAT (PVM) |
| ๐ฑ๐ป Latvia (v2.0) | โ | VAT (PVN, legal entity high / personal moderate) |
| ๐ช๐ช Estonia (v2.0) | โ | VAT (KMKR) |
| ๐ฒ๐น Malta (v2.0) | โ | VAT |
| ๐จ๐พ Cyprus (v2.0) | โ | VAT |
| ๐ฎ๐ธ Iceland (v2.0) | โ | VSK (format-only, EEA not VIES) |
Full per-country docs with algorithms and sources cited live in docs/countries/.
Each spec carries a confidence value reflecting how well-verified its algorithm is:
highโ official source AND mature library agree.moderateโ one official source OR mature library agrees; the other missing.lowโ only community / reverse-engineered. Format-only validation.unconfirmedโ no algorithm verified. Format-only validation.
UIs can choose to surface a warning when a low-confidence document validates only by format.
- Emiso โ multi-tenant electronic-invoicing platform for Central America. Uses
nationidfor receiver DUI / NIT / RUC validation and KYC masking.
If your product uses nationid and you'd like to be listed here, open a PR adding a one-line entry above.
| nationid | validator.js | cpf-cnpj-validator | rut.js | |
|---|---|---|---|---|
| LATAM countries | 22 | 6 | 1 | 1 |
| European countries | 31 (EU-27 VIES + UK/CH/NO/IS) | 8 | 0 | 0 |
| EU-VIES VAT coverage | EU-27 complete | partial | 0 | 0 |
| Asia countries | 3 (IN, JP, SG; KR/TW next) | 0 | 0 | 0 |
| El Salvador | โ | โ | โ | โ |
| Guatemala | โ | โ | โ | โ |
| Honduras | โ | โ | โ | โ |
| Costa Rica | โ | โ | โ | โ |
| Bundle size (1 country) | ~3-5KB | ~40KB full | ~5KB | ~5KB |
| TypeScript types | First-class | Yes | Limited | Limited |
| Tree-shakable subpaths | โ | โ | N/A | N/A |
| Zero deps | โ | โ | โ | โ |
- v0.1 โ 13 countries: SV, MX, CO, BR, PE, AR, CL, DO, GT, HN, CR, ES, US โ
- v0.2 โ 8 additional codes in covered countries โ
- v0.3 โ
extract+pii+i18n+catalogsubpaths โ - v0.4 โ 9 new countries: UY, VE, PA, EC, BO, PY, NI, CA, PT โ
- v0.5 โ Passport family + ICAO 9303 algorithm + BR_CNPJ alphanum + MX_NSS + audit fixes โ
- v0.6 โ Europe principal: GB, FR, DE, IT, NL, BE, CH, PL, SE, NO, DK, FI โ
- v1.0 โ API stability declared. Every
high-confidence spec backed by a first-party citation (CI-enforced). 76% smaller tarball. Type inference narrowing forparse/getSpec/extract*. โ - v1.1 โ Country catalog under
nationid/catalog: names + flags + ISO alpha-3, locale param viaIntl.DisplayNames(any BCP 47 tag). โ - v1.2 โ Asia phase 1: India (Aadhaar, VID, PAN, GSTIN, EPIC) + Verhoeff primitive. โ
- v2.0 โ EU-VAT complete: 16 EU members + Iceland (EEA). ISO/IEC 7064 MOD 11,10 primitive. URL liveness audit pattern. โ
- v2.1 โ Asia phase 2 โ Japan (My Number + Corporate Number). MIC + NTA algorithms cross-validated against
python-stdnum. โ - v2.2 โ Asia phase 2 โ Singapore (NRIC, FIN, UEN). ICA/ACRA algorithms, UEN cross-validated against
python-stdnum/stdnum/sg/uen.py. โ - v2.3-v2.4 โ Asia phase 2 (research + verification complete): KR (RRN/BRN), TW (ID/Tax). Implementing next.
- v2.5 โ Balkans via JMBG core primitive (RS/BA/MK/ME) + GB Northern Ireland
XIprefix +BG_EGN+CZ_RC(unlocks 10-digit BG and full CZ DIC branches). - v2.x โ
@nationid/reactcompanion with<DocumentInput>, additional i18n locales, mutation testing (Stryker), lazy REGISTRY for full root-import tree-shaking.
See CONTRIBUTING.md. Country submissions are welcomed โ every country added must include cited official sources and a comprehensive test fixture set.
MIT โ see LICENSE.