Migrating from better-dni
better-dni validates DNIs and NIEs, and generates them. This library covers the same ground, and adds CIFs and K/L/M NIFs, normalization, the reason for each error and localized messages. The comparison on this page is with better-dni 4.4.2.
Function mapping
Section titled “Function mapping”| better-dni | nif-dni-nie-cif-validation | Notes |
|---|---|---|
isValid(x) |
isValidNaturalPersonNif(x) |
Also accepts K/L/M NIFs and the old NIE form. isValidNif(x) also accepts a CIF |
isNIF(x) |
isValidDni(x) |
Also accepts K/L/M NIFs. validate(x, { types: ["DNI"] }) for a DNI only |
isNIE(x) |
isValidNie(x) |
|
ctrlChar(x) |
computeControlCharacter(x) |
Pass the number without its control character |
randomNIF(), randomNIE() |
generateDni(), generateNie() |
From nif-dni-nie-cif-validation/generate; { seed } for fixed values |
randomNIFWith(letter), randomNIEWith(prefix, letter) |
No equivalent | Use generateNie({ prefix }); the check letter follows from the digits |
normalize(x) |
normalize(x) |
The validators already normalize: no need to call it first |
Before and after
Section titled “Before and after”Before, with better-dni:
import { ctrlChar, isNIE, isNIF, isValid, normalize } from "better-dni";
isValid("12345678Z"); // trueisNIF("12345678Z"); // trueisNIE("X1234567L"); // truectrlChar("12345678"); // "Z"isValid(normalize("12345678-z")); // true: normalize firstAfter:
import { computeControlCharacter, isValidDni, isValidNaturalPersonNif, isValidNie,} from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif("12345678Z"); // trueisValidDni("12345678Z"); // trueisValidNie("X1234567L"); // truecomputeControlCharacter("12345678"); // "Z"isValidNaturalPersonNif("12345678-z"); // true: no separate normalize stepTo generate numbers, before:
import { randomNIE, randomNIF } from "better-dni";
randomNIF(); // a random DNIrandomNIE(); // a random NIEAfter:
import { createGenerator, generateDni, generateNie } from "nif-dni-nie-cif-validation/generate";
generateDni(); // a random DNIgenerateNie(); // a random NIEgenerateDni({ seed: 1 }); // "62707394X": the same on every runcreateGenerator(42).nie(); // "Y4482905N": a seeded stream of valuesDifferences in behaviour
Section titled “Differences in behaviour”K/L/M NIFs. better-dni rejects them. They are NIFs of natural persons (Spaniards without a DNI, foreign nationals without an NIE), and this library accepts them in isValidDni and isValidNaturalPersonNif (KLM-1).
The old NIE form. better-dni rejects 10-character NIEs such as X01234567L; they are still valid (NIE-3).
Separators. better-dni accepts lower case, but rejects spaces, dots and hyphens unless you call its normalize() first. Here every validator normalizes, and normalize() also removes dots and adds the leading zeros of a short DNI.
import { isValid, normalize } from "better-dni";
isValid("K1234567L"); // falseisValid("X01234567L"); // falseisValid(" 12345678Z"); // falsenormalize(" 12.345.678-z "); // "12.345.678Z": dots are keptimport { isValidNaturalPersonNif, normalize } from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif("K1234567L"); // trueisValidNaturalPersonNif("X01234567L"); // trueisValidNaturalPersonNif(" 12345678Z"); // truenormalize(" 12.345.678-z "); // "12345678Z"CIFs. better-dni has no CIF validator. Use isValidCif(), or isValidNif() to accept every type.
The reason. better-dni answers yes or no. validate() also gives the type, the canonical form and, for an invalid value, the error code, the SPEC rule and a message in five languages: see Error codes and localized messages.