Skip to content

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.

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, with validator.js:

import validator from "validator";
validator.isIdentityCard("12345678Z", "ES"); // true
validator.isIdentityCard("X1234567L", "ES"); // true
validator.isTaxID("K1234567L", "es-ES"); // true
validator.isTaxID("B12345674", "es-ES"); // false: no CIF support

After:

import { isValidCif, isValidNaturalPersonNif, isValidNif } from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif("12345678Z"); // true
isValidNaturalPersonNif("X1234567L"); // true
isValidNaturalPersonNif("K1234567L"); // true
isValidCif("B12345674"); // true
isValidNif("B12345674"); // true: any type

With a form library, the schemas for Zod, Valibot and Yup replace a custom refine that calls validator.js, and give the message too.

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 TypeError
import { isValidNaturalPersonNif, validate } from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif(null); // false
validate(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"); // false
validator.isTaxID("X01234567L", "es-ES"); // false
validator.isIdentityCard("K1234567L", "ES"); // false
import { isValidNaturalPersonNif, validate } from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif("12.345.678-Z"); // true
validate("12.345.678-Z").normalized; // "12345678Z"
isValidNaturalPersonNif("X01234567L"); // true
isValidNaturalPersonNif("K1234567L"); // true

Bundle 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.