Migrating from spain-id
spain-id validates DNIs, NIEs and CIFs, and tells their type. This library covers the same ground, and adds K/L/M NIFs, the reason for each error, localized messages, generators and schemas. spain-id is smaller for a single function; the benchmarks have the measured sizes. The comparison on this page is with spain-id 1.1.14.
Function mapping
Section titled “Function mapping”| spain-id | nif-dni-nie-cif-validation | Notes |
|---|---|---|
validateSpanishId(x) |
isValidNif(x) |
Also accepts K/L/M NIFs. The CIF control follows CIF-3 (see below) |
validDNI(x) |
isValidDni(x) |
Also accepts K/L/M NIFs |
validNIE(x) |
isValidNie(x) |
Also accepts the old 10-character form |
validCIF(x) |
isValidCif(x) |
The control follows CIF-3 |
spainIdType(x) |
getNifType(x) |
Upper case, "NIF_KLM" for K/L/M, and null instead of undefined |
Before and after
Section titled “Before and after”Before, with spain-id:
import { spainIdType, validateSpanishId, validCIF, validDNI, validNIE } from "spain-id";
validateSpanishId("12345678Z"); // truevalidDNI("12345678Z"); // truevalidNIE("X1234567L"); // truevalidCIF("B12345674"); // truespainIdType("B12345674"); // "cif"spainIdType("hello"); // undefinedAfter:
import { getNifType, isValidCif, isValidDni, isValidNie, isValidNif } from "nif-dni-nie-cif-validation";
isValidNif("12345678Z"); // trueisValidDni("12345678Z"); // trueisValidNie("X1234567L"); // trueisValidCif("B12345674"); // truegetNifType("B12345674"); // "CIF"getNifType("hello"); // nullLike spainIdType, getNifType reads the format only, and doesn’t check the control character. To know whether a number is valid and why not, use validate(), which returns the type too.
Differences in behaviour
Section titled “Differences in behaviour”The CIF control (CIF-3). spain-id accepts either a letter or a digit as the control character of every CIF. The AEAT’s technical note gives each organisation key one kind: a digit for A B C D E F G H J U V and a letter for N P Q R S W (CIF-3). So some numbers that spain-id accepts are invalid here, such as G1234567D. If your stored data has them, pass { cifControl: "lenient" } while you clean it up: it accepts a letter for C, D, F, G, J, U and V.
import { validCIF } from "spain-id";
validCIF("G1234567D"); // trueimport { isValidCif } from "nif-dni-nie-cif-validation";
isValidCif("G1234567D"); // false: G takes a digitisValidCif("G1234567D", { cifControl: "lenient" }); // trueisValidCif("G12345674"); // trueK/L/M NIFs. spain-id rejects them and has no type for them. They are NIFs of natural persons, valid here (KLM-1).
The old NIE form. spain-id rejects 10-character NIEs such as X01234567L; they are still valid (NIE-3).
Separators. spain-id removes spaces and hyphens, but not dots. This library also removes dots and slashes, and returns the canonical form to store.
Values that are not strings. spain-id throws on null; this library returns false.
import { spainIdType, validateSpanishId } from "spain-id";
validateSpanishId("K1234567L"); // falsevalidateSpanishId("X01234567L"); // falsevalidateSpanishId("12.345.678-Z"); // falsespainIdType("K1234567L"); // undefinedvalidateSpanishId(null); // throws TypeErrorimport { getNifType, isValidNif, validate } from "nif-dni-nie-cif-validation";
isValidNif("K1234567L"); // trueisValidNif("X01234567L"); // trueisValidNif("12.345.678-Z"); // truegetNifType("K1234567L"); // "NIF_KLM"isValidNif(null); // falsevalidate("12.345.678-Z").normalized; // "12345678Z"