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 v0.6 it ships 34 countries with ~120 document codes, all with proper algorithms documented from official sources.
npm i nationid
# or
pnpm add nationid
# or
yarn add nationid
Requires 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 / normalize for all 22 countriesextract (DOB, sex, region) where the document encodes itpii masking + SHA-256 hashing for safe display and storagei18n error messages in es, en, ptcatalog โ queryable document metadata for UI dropdownsSource 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 new tree-shakable modules for common app needs:
// 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", ... }, ...]
Each 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 |
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.
| nationid | validator.js | cpf-cnpj-validator | rut.js | |
|---|---|---|---|---|
| LATAM countries | 13 | 6 | 1 | 1 |
| 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 | โ | โ | โ | โ |
BR_CNH, BR_TITULO_ELEITOR, BR_PIS, AR_CDI, ES_NUSS, MX_CLAVE_ELECTOR, CO_PEP, CO_PPT โ
extract (DOB, sex, region) + pii (mask, hash, lastN) + i18n (es/en/pt error messages) + catalog (queryable document metadata) โ
@nationid/react companion with <DocumentInput> + additional i18n localesSee CONTRIBUTING.md. Country submissions are welcomed โ every country added must include cited official sources and a comprehensive test fixture set.
MIT โ see LICENSE.