Skip to content

API reference

The reference is generated from the JSDoc of the library’s source each time the site is built, so it always matches the code. It has one page per entry point of the package, with every export: its signature, what each parameter and the return value mean, two examples that the library’s tests run, the SPEC rule it follows, and the version that added it.

The entry points of the package, and what each one exports
Entry pointWhat it exports
nif-dni-nie-cif-validationThe validators, validate(), normalize(), format(), getNifType(), computeControlCharacter(), describeCifOrganisation(), the v1 constants and the types. English messages are built in.
nif-dni-nie-cif-validation/locales/enThe English locale object, to pass as locale to validate(), describeCifOrganisation() and the schemas.
nif-dni-nie-cif-validation/locales/esThe Spanish locale object, to pass as locale to validate(), describeCifOrganisation() and the schemas.
nif-dni-nie-cif-validation/locales/caThe Catalan (also for Valencian) locale object, to pass as locale to validate(), describeCifOrganisation() and the schemas.
nif-dni-nie-cif-validation/locales/euThe Basque locale object, to pass as locale to validate(), describeCifOrganisation() and the schemas.
nif-dni-nie-cif-validation/locales/glThe Galician locale object, to pass as locale to validate(), describeCifOrganisation() and the schemas.
nif-dni-nie-cif-validation/generateSeeded generators of valid and invalid numbers, for tests. Never imported by the main entry point.
nif-dni-nie-cif-validation/zodZod 4 schemas for every type and for VAT numbers. Zod is an optional peer dependency.
nif-dni-nie-cif-validation/valibotValibot 1 schemas for every type and for VAT numbers. Valibot is an optional peer dependency.
nif-dni-nie-cif-validation/yupYup 1 schemas for every type and for VAT numbers. Yup is an optional peer dependency.

Your editor shows the same documentation as you type, from the type declarations that the package ships.

  • Options. The validators take IsValidOptions, validate() takes ValidateOptions, and the schemas take the options of validate(). Each option’s default and effect are listed on the main entry point’s page.
  • Rules. The links to rule IDs, such as DNI-2, open the official sources page.
  • Aliases. isValidCif and isValidLegalEntityNif are the same function, as are isValidCifControlCode and isValidLegalEntityNifControlCode; CIF_REGEX and LEGAL_ENTITY_NIF_REGEX, and CIF_CONTROL_LETTERS and LEGAL_ENTITY_CONTROL_LETTERS, are the same constants.
  • Deprecated. replaceNieLetter is kept for version 1 code. It throws on bad input, unlike the validators and the other helpers (the generators also throw a RangeError, when their options are impossible).

For a task-oriented introduction, start with Getting started. The decisions behind the API are in docs/api-design.md.