Skip to content

nif-dni-nie-cif-validation/valibot

vCif(opts?): NifSchema

Defined in: valibot/index.ts:255

A Valibot 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.

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

SPEC.md#cif-3

2.0.0


vDni(opts?): NifSchema

Defined in: valibot/index.ts:211

A Valibot 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.

import { parse } from "valibot";
parse(vDni(), "1234567-l"); // "01234567L"
import { safeParse } from "valibot";
safeParse(vDni(), "X1234567L").success; // false: UNSUPPORTED_TYPE

2.0.0


vNie(opts?): NifSchema

Defined in: valibot/index.ts:233

A Valibot 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.

import { parse } from "valibot";
parse(vNie(), "x-01234567-l"); // "X1234567L"
import { safeParse } from "valibot";
safeParse(vNie(), "12345678Z").success; // false: a DNI is not an NIE

2.0.0


vNif(opts?): NifSchema

Defined in: valibot/index.ts:187

A Valibot 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 { object, parse } from "valibot";
import { vNif } from "nif-dni-nie-cif-validation/valibot";
import { es } from "nif-dni-nie-cif-validation/locales/es";
const form = object({ nif: vNif({ types: ["DNI", "NIE"], locale: es }) });
parse(form, { nif: " 12.345.678-z " }); // { nif: "12345678Z" }
import { safeParse } from "valibot";
const result = safeParse(vNif(), "12345678A");
result.issues?.[0]?.message;
// 'The control character is not correct: for this DNI it should be "Z".'
(result.issues?.[0] as NifIssue).rule; // "DNI-2"

SPEC.md#nif-1

2.0.0


vSpanishVat(opts?): NifSchema

Defined in: valibot/index.ts:278

A Valibot 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.

import { parse } from "valibot";
parse(vSpanishVat(), "es b-1234567-4"); // "ESB12345674"
import { safeParse } from "valibot";
safeParse(vSpanishVat(), "B12345674").success; // false: no ES prefix

SPEC.md#vat-1

2.0.0

Defined in: valibot/index.ts:89

The transformation of the schemas: a string to its normalized form.

2.0.0

  • BaseTransformation<string, string, NifIssue>

readonly async: false

Whether it’s async.

BaseTransformation.async

readonly kind: "transformation"

The object kind.

BaseTransformation.kind

readonly reference: (kind, opts?) => NifAction

Defined in: valibot/index.ts:92

The transformation reference.

The transformation itself: Valibot runs ~run on the dataset of the pipe. It checks the string and replaces it with the normalized value, or fails with a NifIssue.

NifSchemaKind

ValidateOptions

NifAction

BaseTransformation.reference

readonly type: "nif"

Defined in: valibot/index.ts:91

The transformation type.

BaseTransformation.type


Defined in: valibot/index.ts:71

The issue of a refused value: a Valibot issue with what validate() says.

import { safeParse } from "valibot";
const result = safeParse(vDni(), "12345678A");
const issue = result.issues?.[0] as NifIssue;
issue.code; // "INVALID_CONTROL_CHARACTER"
issue.rule; // "DNI-2"
issue.expected; // "Z"
import { safeParse } from "valibot";
const issue = safeParse(vNif(), "").issues?.[0] as NifIssue;
issue.code; // "EMPTY"
issue.expected; // null: only a wrong control character has an expected value

SPEC.md#dni-2

2.0.0

  • BaseIssue<string>

readonly optional abortEarly?: boolean

Whether it should be aborted early.

BaseIssue.abortEarly

readonly optional abortPipeEarly?: boolean

Whether a pipe should be aborted early.

BaseIssue.abortPipeEarly

readonly code: NifErrorCode

Defined in: valibot/index.ts:75

The error code of validate().

readonly expected: string | null

Defined in: valibot/index.ts:82

The right control character, for INVALID_CONTROL_CHARACTER; null otherwise (Valibot’s own property, which says what was expected).

BaseIssue.expected

readonly input: string

The raw input data.

BaseIssue.input

readonly optional issues?: [BaseIssue<string>, ...BaseIssue<string>[]]

The sub issues.

BaseIssue.issues

readonly kind: "transformation"

Defined in: valibot/index.ts:72

The issue kind.

BaseIssue.kind

readonly optional lang?: string

The selected language.

BaseIssue.lang

readonly message: string

The error message.

BaseIssue.message

readonly optional path?: [IssuePathItem, ...IssuePathItem[]]

The issue path.

BaseIssue.path

readonly received: string

The received property.

BaseIssue.received

readonly optional requirement?: unknown

The input requirement.

BaseIssue.requirement

readonly rule: string

Defined in: valibot/index.ts:77

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

readonly type: "nif"

Defined in: valibot/index.ts:73

The issue type.

BaseIssue.type

NifSchema = SchemaWithPipe<readonly [StringSchema<string>, NifAction]>

Defined in: valibot/index.ts:114

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

import { type InferOutput, parse } from "valibot";
import { type NifSchema, vNif } from "nif-dni-nie-cif-validation/valibot";
const schema: NifSchema = vNif();
const output: InferOutput<NifSchema> = parse(schema, " 12.345.678-z ");
output; // "12345678Z"
import { type InferInput, parse } from "valibot";
import { type NifSchema, vCif } from "nif-dni-nie-cif-validation/valibot";
const input: InferInput<NifSchema> = "b-1234567-4";
parse(vCif(), input); // "B12345674"

SPEC.md#nif-1

2.0.0

Re-exports NifSchemaOptions


Re-exports SpanishVatSchemaOptions


Re-exports TypedNifSchemaOptions