Migrating from validator.js
validator.js validates many kinds of strings, and has two functions for Spanish IDs: isIdentityCard(x, "ES") and isTaxID(x, "es-ES"). If Spanish IDs are what you need it for, this library checks them more fully, and adds CIFs, normalization and the reason for each error. You can also keep validator.js for everything else and use this library for NIFs only. The comparison on this page is with validator.js 13.15.35.
Function mapping
Section titled “Function mapping”| validator.js | nif-dni-nie-cif-validation | Notes |
|---|---|---|
isIdentityCard(x, "ES") |
isValidNaturalPersonNif(x) |
validator.js accepts a DNI or an NIE; this also accepts K/L/M NIFs and the old NIE form |
isTaxID(x, "es-ES") |
isValidNaturalPersonNif(x) |
validator.js accepts a DNI, K/L/M NIF or NIE; this also accepts the old NIE form |
| No CIF check | isValidCif(x), or isValidNif(x) for every type |
Before and after
Section titled “Before and after”Before, with validator.js:
import validator from "validator";
validator.isIdentityCard("12345678Z", "ES"); // truevalidator.isIdentityCard("X1234567L", "ES"); // truevalidator.isTaxID("K1234567L", "es-ES"); // truevalidator.isTaxID("B12345674", "es-ES"); // false: no CIF supportAfter:
import { isValidCif, isValidNaturalPersonNif, isValidNif } from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif("12345678Z"); // trueisValidNaturalPersonNif("X1234567L"); // trueisValidNaturalPersonNif("K1234567L"); // trueisValidCif("B12345674"); // trueisValidNif("B12345674"); // true: any typeWith a form library, the schemas for Zod, Valibot and Yup replace a custom refine that calls validator.js, and give the message too.
Differences in behaviour
Section titled “Differences in behaviour”Values that are not strings. validator.js throws a TypeError on anything that is not a string, such as null or a number. This library returns false, and validate() returns the code NOT_A_STRING (INPUT-1), so you can pass a form value as it is.
import validator from "validator";
validator.isIdentityCard(null, "ES"); // throws TypeErrorimport { isValidNaturalPersonNif, validate } from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif(null); // falsevalidate(null).error?.code; // "NOT_A_STRING"Separators. Both libraries accept lower case. validator.js rejects spaces, dots and hyphens; this library removes them (NORM-2, NORM-3) and gives the canonical form to store.
The old NIE form. validator.js rejects 10-character NIEs such as X01234567L; they are still valid (NIE-3).
K/L/M NIFs. isIdentityCard(x, "ES") rejects them, and isTaxID(x, "es-ES") accepts them. This library accepts them as natural persons’ NIFs (KLM-1).
import validator from "validator";
validator.isIdentityCard("12.345.678-Z", "ES"); // falsevalidator.isTaxID("X01234567L", "es-ES"); // falsevalidator.isIdentityCard("K1234567L", "ES"); // falseimport { isValidNaturalPersonNif, validate } from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif("12.345.678-Z"); // truevalidate("12.345.678-Z").normalized; // "12345678Z"isValidNaturalPersonNif("X01234567L"); // trueisValidNaturalPersonNif("K1234567L"); // trueBundle size. One validator.js function bundles the rules of every country it supports, so isIdentityCard and isTaxID are several times the size of isValidNif. The benchmarks have the measured figures.