Ir al contenido

nif-dni-nie-cif-validation/zod

Esta página aún no está disponible en tu idioma.

zCif(opts?): NifSchema

Defined in: zod/index.ts:200

A Zod schema for the NIF of a legal person or entity (CIF). The control character follows CIF-3 unless cifControl: "lenient".

TypedNifSchemaOptions

The options of validate() except types.

NifSchema

A schema from string to the normalized string.

zCif().parse(" b-1234567-4 "); // "B12345674"
zCif().safeParse("G1234567D").success; // false (CIF-3)
zCif({ cifControl: "lenient" }).safeParse("G1234567D").success; // true

SPEC.md#cif-3

2.0.0


zDni(opts?): NifSchema

Defined in: zod/index.ts:164

A Zod schema for a DNI (8 digits and a letter) or a K/L/M NIF, like isValidDni. Another valid document, such as an NIE, gives UNSUPPORTED_TYPE. The output is the normalized value: a DNI with fewer than 8 digits is padded with zeros (NORM-4).

TypedNifSchemaOptions

The options of validate() except types.

NifSchema

A schema from string to the normalized string.

zDni().parse("1234567-l"); // "01234567L"
zDni().safeParse("X1234567L").success; // false: UNSUPPORTED_TYPE, a NIE is not a DNI
zDni().safeParse("12345678A").success; // false: the letter should be Z (DNI-2)

2.0.0


zNie(opts?): NifSchema

Defined in: zod/index.ts:182

A Zod schema for an NIE. The output is the canonical 9-character form: an old 10-character NIE is shortened (NIE-3).

TypedNifSchemaOptions

The options of validate() except types.

NifSchema

A schema from string to the normalized string.

zNie().parse("x-01234567-l"); // "X1234567L"
zNie().safeParse("12345678Z").success; // false: a DNI is not an NIE

2.0.0


zNif(opts?): NifSchema

Defined in: zod/index.ts:143

A Zod schema for any Spanish NIF: a DNI, a K/L/M NIF, an NIE or a NIF of a legal person or entity (CIF). The output is the normalized value. Takes the options of validate(), including types to accept only some documents (POLICY-2) and locale for the messages.

ValidateOptions

types, normalize, cifControl, rejectPlaceholders, allowVatPrefix, locale. See NifSchemaOptions.

NifSchema

A schema from string to the normalized string.

import { z } from "zod";
import { zNif } from "nif-dni-nie-cif-validation/zod";
import { es } from "nif-dni-nie-cif-validation/locales/es";
const form = z.object({ nif: zNif({ types: ["DNI", "NIE"], locale: es }) });
form.parse({ nif: " 12.345.678-z " }); // { nif: "12345678Z" }
import { z } from "zod";
const result = zNif().safeParse("12345678A");
const issue = result.error?.issues[0] as z.core.$ZodIssueCustom;
issue.message;
// 'The control character is not correct: for this DNI it should be "Z".'
issue.params; // { code: "INVALID_CONTROL_CHARACTER", rule: "DNI-2", expected: "Z" }

SPEC.md#nif-1

2.0.0


zSpanishVat(opts?): NifSchema

Defined in: zod/index.ts:219

A Zod schema for a Spanish VAT number: ES and a NIF (VAT-1). The prefix is required, as in isValidSpanishVat, and the output keeps it: ES and the normalized NIF. It checks the format only: a valid format doesn’t mean the number is registered in VIES.

SpanishVatSchemaOptions

The options of validate() except allowVatPrefix.

NifSchema

A schema from string to the normalized string.

zSpanishVat().parse("es b-1234567-4"); // "ESB12345674"
zSpanishVat().safeParse("B12345674").success; // false: no ES prefix

SPEC.md#vat-1

2.0.0

Defined in: zod/index.ts:91

The params of the issue of a refused value: what validate() says.

import { z } from "zod";
const result = zDni().safeParse("12345678A");
const issue = result.error?.issues[0] as z.core.$ZodIssueCustom;
const params = issue.params as NifIssueParams;
params.code; // "INVALID_CONTROL_CHARACTER"
params.rule; // "DNI-2"
import { z } from "zod";
const issue = zDni().safeParse("12345678A").error?.issues[0] as z.core.$ZodIssueCustom;
(issue.params as NifIssueParams).expected; // "Z": the right control character

SPEC.md#dni-2

2.0.0

code: NifErrorCode

Defined in: zod/index.ts:93

The error code of validate().

optional expected?: string

Defined in: zod/index.ts:97

The right control character, for INVALID_CONTROL_CHARACTER.

rule: string

Defined in: zod/index.ts:95

The SPEC.md rule that failed, for example "DNI-2".

NifSchema = ZodPipe<ZodString, ZodTransform<string, string>>

Defined in: zod/index.ts:71

The schemas of this entry point: a string, transformed to its normalized form. Its input and its output are string.

import { z } from "zod";
const schema: NifSchema = zNif();
const output: z.output<NifSchema> = schema.parse(" 12.345.678-z ");
output; // "12345678Z"
import { z } from "zod";
const input: z.input<NifSchema> = "b-1234567-4";
zCif().parse(input); // "B12345674"

SPEC.md#nif-1

2.0.0


NifSchemaOptions = ValidateOptions

Defined in: adapter.ts:43

Options of the generic schema (zNif, vNif, yNif): the options of validate().

import { es } from "nif-dni-nie-cif-validation/locales/es";
const options: NifSchemaOptions = { types: ["DNI", "NIE"], locale: es };
zNif(options).safeParse("B12345674").success; // false: a CIF is not accepted
const options: NifSchemaOptions = { rejectPlaceholders: true, cifControl: "lenient" };
zNif(options).safeParse("00000000T").success; // false: a placeholder (POLICY-1)

SPEC.md#policy-2

2.0.0


SpanishVatSchemaOptions = Omit<ValidateOptions, "allowVatPrefix">

Defined in: adapter.ts:77

Options of the Spanish VAT schema: the options of validate() except allowVatPrefix, which the schema always sets (the ES prefix is required).

import { es } from "nif-dni-nie-cif-validation/locales/es";
const options: SpanishVatSchemaOptions = { locale: es };
zSpanishVat(options).parse("ES 12345678-z"); // "ES12345678Z"
const options: SpanishVatSchemaOptions = { rejectPlaceholders: true };
zSpanishVat(options).safeParse("ES00000000T").success; // false: a placeholder

SPEC.md#vat-1

2.0.0


TypedNifSchemaOptions = Omit<ValidateOptions, "types">

Defined in: adapter.ts:60

Options of the schemas for one type (DNI, NIE, CIF): the options of validate() except types, which the schema fixes.

import { es } from "nif-dni-nie-cif-validation/locales/es";
const options: TypedNifSchemaOptions = { locale: es };
zNie(options).safeParse("12345678Z").error?.issues[0]?.message;
// "Aquí no se admite un DNI."
const options: TypedNifSchemaOptions = { cifControl: "lenient" };
zCif(options).safeParse("G1234567D").success; // true

SPEC.md#policy-2

2.0.0