nationid - v0.6.0
    Preparing search index...

    nationid - v0.6.0

    nationid

    TypeScript-first, zero-dependency validator for national identity and tax documents from every country.

    npm version bundle size types license CI

    ๐ŸŽฎ 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 countries
    • ๐ŸŽฏ extract (DOB, sex, region) where the document encodes it
    • ๐Ÿ”’ pii masking + SHA-256 hashing for safe display and storage
    • ๐ŸŒ i18n error messages in es, 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 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 โœ… โœ… โœ… โœ…
    • 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: BR_CNH, BR_TITULO_ELEITOR, BR_PIS, AR_CDI, ES_NUSS, MX_CLAVE_ELECTOR, CO_PEP, CO_PPT โœ…
    • v0.3 โ€” extract (DOB, sex, region) + pii (mask, hash, lastN) + i18n (es/en/pt error messages) + catalog (queryable document metadata) โœ…
    • v0.4 โ€” 9 new countries: UY, VE, PA, EC, BO, PY, NI, CA, PT โœ…
    • v0.5 โ€” Passport family (22 countries) + ICAO 9303 algorithm + BR_CNPJ alphanum rollout (IN RFB 2.229/2024) + MX_NSS + audit fixes โœ…
    • v0.6 โ€” Europe principal: GB, FR, DE, IT, NL, BE, CH, PL, SE, NO, DK, FI โœ…
    • v0.7 โ€” Asia: IN, CN, JP, KR, SG, HK, TW + AU, NZ, ZA, IL
    • v0.8 โ€” @nationid/react companion with <DocumentInput> + additional i18n locales
    • v1.0 โ€” API stability, all current countries at High confidence

    See CONTRIBUTING.md. Country submissions are welcomed โ€” every country added must include cited official sources and a comprehensive test fixture set.

    MIT โ€” see LICENSE.