Ir ao contido

nif-dni-nie-cif-validation/generate

Este contido aínda non está dispoñible no seu idioma.

createGenerator(seed): Generator

Defined in: generate/index.ts:402

Creates a generator whose values come one after the other from a single seed: a stream of different values that is the same on every run and every platform. Use it for test fixtures that need many distinct documents. (generateDni({ seed }) would return the same DNI at every call.)

The values are the ones of the matching generate* functions, with the same options except seed, and with the same limits: synthetic, for tests only, and they may match a real person or company by chance.

number

An integer, reduced modulo 2^32.

Generator

An object with dni, nie, cif, nif and invalid.

If seed isn’t an integer.

const gen = createGenerator(2024);
const users = Array.from({ length: 3 }, () => ({ dni: gen.dni() }));
users.map((user) => user.dni); // ["81176236J", "71082149M", "65052588D"]
const gen = createGenerator(1);
gen.nie({ prefix: "Z", format: true }); // "Z-6270739-R"
validate(gen.invalid("CIF", { reason: "INVALID_FORMAT" })).valid; // false

SPEC.md#nif-1

2.0.0


generateCif(opts?): string

Defined in: generate/index.ts:270

Generates a valid NIF of a legal person or entity (CIF): an organisation key, 7 digits and the control character (CIF-1 to CIF-4). The control is a digit or a letter as CIF-3 says for the key, never as the legacy cifControl: "lenient" mode would allow. The 7 digits are random, with no province code (CIF-5).

orgKey picks the key. control only makes sense together with the key’s own type, since CIF-3 gives each key one: without orgKey it restricts the keys to the ones that take a letter (N P Q R S W) or a digit (the other eleven); with an orgKey that takes the other type it throws.

Synthetic data, for tests only: the number is valid but made up, and it may match a real company’s by chance. Never use it as a real identifier.

GenerateCifOptions = {}

seed, orgKey, control, format.

string

A CIF in canonical form, or formatted with format.

For an unknown orgKey or control, a control that the orgKey can’t take (CIF-3), or a seed that isn’t an integer.

generateCif({ seed: 1, orgKey: "B" }); // always "B62707393"
generateCif({ seed: 1, orgKey: "P" }); // "P6270739C": P takes a letter
validate(generateCif({ control: "letter" })).valid; // true: N, P, Q, R, S or W
generateCif({ orgKey: "B", control: "letter" }); // throws RangeError

2.0.0


generateDni(opts?): string

Defined in: generate/index.ts:211

Generates a valid DNI (8 digits and a check letter, DNI-1, DNI-2), or with kind a K, L or M NIF (KLM-1, KLM-2). The number is zero-padded, so about one in ten starts with 0. It is never a placeholder such as 00000000T (POLICY-1).

Synthetic data, for tests only: the number is valid but made up, and it may match a real person’s by chance. Never use it as a real identifier. validate() and isValidDni() accept it.

GenerateDniOptions = {}

seed, kind, format.

string

A DNI in canonical form, or formatted with format.

For an unknown kind or a seed that isn’t an integer.

generateDni({ seed: 1 }); // always "62707394X"
isValidDni(generateDni()); // true
generateDni({ seed: 1, kind: "K", format: true }); // "K-6270739-L"

2.0.0


generateInvalid(type, opts?): string

Defined in: generate/index.ts:344

Generates a value that validate() rejects, for negative tests: it starts from a valid document of type and breaks one thing, so it looks like the type it stands for. reason is the error.code that validate() gives it, with the default options:

  • "INVALID_CONTROL_CHARACTER" (the default): only the control character changes (DNI-2, KLM-2, NIE-2, CIF-3, CIF-4). For a CIF it is wrong with cifControl: "lenient" too.
  • "INVALID_LENGTH": too long, or too short (never for a DNI, which NORM-4 pads). It is never an old 10-character NIE (NIE-3), which is valid.
  • "INVALID_FORMAT": a letter in the number, or a control character of the wrong class.
  • "EMPTY": an empty string or only separators (INPUT-2), for any type.
  • "UNSUPPORTED_TYPE": a valid value, rejected only when types of validate() leaves type out (POLICY-2).
  • "PLACEHOLDER": a placeholder number, rejected only with rejectPlaceholders (POLICY-1). It exists for "DNI" and "NIE"; any other type throws.
  • "NOT_A_STRING" throws: the result is always a string.

Synthetic data, for tests only. The value is made up.

NifType

The type of document the value stands for.

GenerateInvalidOptions = {}

seed, reason.

string

The value, not formatted.

For an unknown type or reason, for NOT_A_STRING, for PLACEHOLDER with a type that has no placeholder, or a seed that isn’t an integer.

const value = generateInvalid("DNI", { seed: 1 }); // "62707394A"
validate(value).error?.code; // "INVALID_CONTROL_CHARACTER"
const short = generateInvalid("CIF", { reason: "INVALID_LENGTH" });
validate(short).error?.code; // "INVALID_LENGTH"
const fake = generateInvalid("DNI", { reason: "PLACEHOLDER" });
validate(fake, { rejectPlaceholders: true }).error?.code; // "PLACEHOLDER"

SPEC.md#dni-2

2.0.0


generateNie(opts?): string

Defined in: generate/index.ts:236

Generates a valid NIE in its canonical 9-character form: X, Y or Z, 7 digits and the check letter (NIE-1, NIE-2). It is never the old 10-character form (NIE-3) and never the placeholder X0000000T (POLICY-1).

Synthetic data, for tests only: the number is valid but made up, and it may match a real person’s by chance. Never use it as a real identifier.

GenerateNieOptions = {}

seed, prefix, format.

string

An NIE in canonical form, or formatted with format.

For an unknown prefix or a seed that isn’t an integer.

generateNie({ seed: 1 }); // always "Y0027357R"
isValidNie(generateNie()); // true
generateNie({ seed: 1, prefix: "Y", format: { separator: " " } }); // "Y 6270739 X"

2.0.0


generateNif(opts?): string

Defined in: generate/index.ts:299

Generates a valid NIF of any of the types (DNI, K/L/M NIF, NIE and CIF by default), one of them chosen at random, with the rules of generateDni, generateNie and generateCif.

Synthetic data, for tests only: the number is valid but made up, and it may match a real person or company by chance. Never use it as a real identifier.

GenerateNifOptions = {}

seed, types, format.

string

A NIF in canonical form, or formatted with format.

For an empty or unknown types, or a seed that isn’t an integer.

generateNif({ seed: 1 }); // always "X5274470H"
isValidNif(generateNif()); // true
generateNif({ seed: 1, types: ["CIF"] }); // "A52744703"
validate(generateNif({ types: ["CIF"] })).type; // "CIF"

SPEC.md#nif-1

2.0.0

Defined in: generate/index.ts:124

Options of generateCif.

generateCif({ seed: 1, orgKey: "B" }); // "B62707393": a digit control
generateCif({ seed: 1, control: "letter" }); // "R0027357C": N P Q R S or W

SPEC.md#cif-3

2.0.0

optional control?: CifControl

Defined in: generate/index.ts:137

The type of the control character. CIF-3 gives every key exactly one type, so it can never change what a key takes: without orgKey it picks among the keys with that type (letter: N P Q R S W; digit: the other eleven), and with an orgKey that takes the other type it throws a RangeError.

SPEC.md#cif-3

optional format?: boolean | FormatOptions

Defined in: generate/index.ts:77

false (the default): the canonical form, upper case and without separators. true or { separator }: the display form of format(), with - or the separator given ("" gives the canonical form).

GenerateFormatOptions.format

optional orgKey?: CifOrganisationKey

Defined in: generate/index.ts:128

The organisation key. Default: one of the 17 keys at random.

optional seed?: number

Defined in: generate/index.ts:58

An integer that fixes the result: the same seed gives the same value on every platform. It is reduced modulo 2^32. Without it, Math.random is used. A call with a seed always starts from that seed, so it returns the same value each time: use createGenerator for a stream of different values from one seed.

GenerateOptions.seed


Defined in: generate/index.ts:89

Options of generateDni.

generateDni({ seed: 1, kind: "K" }); // "K6270739L": K + 7 digits + letter
generateDni({ seed: 1, kind: "K", format: true }); // "K-6270739-L"

SPEC.md#klm-1

2.0.0

optional format?: boolean | FormatOptions

Defined in: generate/index.ts:77

false (the default): the canonical form, upper case and without separators. true or { separator }: the display form of format(), with - or the separator given ("" gives the canonical form).

GenerateFormatOptions.format

optional kind?: DniKind

Defined in: generate/index.ts:96

"DNI" (the default): 8 digits and a letter. "K", "L" or "M": that NIF, a letter and 7 digits and a check letter (KLM-1).

optional seed?: number

Defined in: generate/index.ts:58

An integer that fixes the result: the same seed gives the same value on every platform. It is reduced modulo 2^32. Without it, Math.random is used. A call with a seed always starts from that seed, so it returns the same value each time: use createGenerator for a stream of different values from one seed.

GenerateOptions.seed


Defined in: generate/index.ts:71

Options of the generators that return a valid document: the output form.

generateDni({ seed: 1 }); // "62707394X"
generateDni({ seed: 1, format: true }); // "62707394-X"
generateCif({ seed: 1, orgKey: "B", format: { separator: " " } }); // "B 6270739 3"

SPEC.md#cif-1

2.0.0

optional format?: boolean | FormatOptions

Defined in: generate/index.ts:77

false (the default): the canonical form, upper case and without separators. true or { separator }: the display form of format(), with - or the separator given ("" gives the canonical form).


Defined in: generate/index.ts:168

Options of generateInvalid.

generateInvalid("CIF", { seed: 1, reason: "INVALID_LENGTH" }); // "P0027C"
generateInvalid("DNI", { seed: 1 }); // "62707394A": the default, a wrong control letter

SPEC.md#dni-2

2.0.0

optional reason?: NifErrorCode

Defined in: generate/index.ts:174

The error code validate() gives the value. Default: "INVALID_CONTROL_CHARACTER". See generateInvalid for the codes and for what each needs.

optional seed?: number

Defined in: generate/index.ts:58

An integer that fixes the result: the same seed gives the same value on every platform. It is reduced modulo 2^32. Without it, Math.random is used. A call with a seed always starts from that seed, so it returns the same value each time: use createGenerator for a stream of different values from one seed.

GenerateOptions.seed


Defined in: generate/index.ts:108

Options of generateNie.

generateNie({ seed: 1, prefix: "Z" }); // "Z6270739R"
generateNie({ seed: 1, prefix: "Y", format: { separator: " " } }); // "Y 6270739 X"

SPEC.md#nie-1

2.0.0

optional format?: boolean | FormatOptions

Defined in: generate/index.ts:77

false (the default): the canonical form, upper case and without separators. true or { separator }: the display form of format(), with - or the separator given ("" gives the canonical form).

GenerateFormatOptions.format

optional prefix?: NiePrefix

Defined in: generate/index.ts:112

The first letter. Default: one of X, Y and Z at random.

optional seed?: number

Defined in: generate/index.ts:58

An integer that fixes the result: the same seed gives the same value on every platform. It is reduced modulo 2^32. Without it, Math.random is used. A call with a seed always starts from that seed, so it returns the same value each time: use createGenerator for a stream of different values from one seed.

GenerateOptions.seed


Defined in: generate/index.ts:149

Options of generateNif.

generateNif({ seed: 1, types: ["DNI", "NIE"] }); // "X5274470H": a DNI or an NIE
generateNif({ seed: 1, types: ["CIF"] }); // "A52744703"

SPEC.md#nif-1

2.0.0

optional format?: boolean | FormatOptions

Defined in: generate/index.ts:77

false (the default): the canonical form, upper case and without separators. true or { separator }: the display form of format(), with - or the separator given ("" gives the canonical form).

GenerateFormatOptions.format

optional seed?: number

Defined in: generate/index.ts:58

An integer that fixes the result: the same seed gives the same value on every platform. It is reduced modulo 2^32. Without it, Math.random is used. A call with a seed always starts from that seed, so it returns the same value each time: use createGenerator for a stream of different values from one seed.

GenerateOptions.seed

optional types?: NifType[]

Defined in: generate/index.ts:156

The types to pick from, one of them at random. Default: all four. An empty array throws a RangeError.


Defined in: generate/index.ts:50

Options that every generator takes.

generateDni({ seed: 42 }); // "60110375J"
generateDni({ seed: 42 }); // "60110375J": the same seed gives the same value
isValidNie(generateNie()); // true: without a seed, a different value every time

SPEC.md#dni-1

2.0.0

optional seed?: number

Defined in: generate/index.ts:58

An integer that fixes the result: the same seed gives the same value on every platform. It is reduced modulo 2^32. Without it, Math.random is used. A call with a seed always starts from that seed, so it returns the same value each time: use createGenerator for a stream of different values from one seed.


Defined in: generate/index.ts:365

A stream of generated values: every call gives the next value, so the values differ, and the same seed gives the same stream.

const gen = createGenerator(1);
gen.dni(); // "62707394X", the first DNI of the stream
gen.dni(); // "00273574N", a different one
const a = createGenerator(5).cif({ orgKey: "B" });
const b = createGenerator(5).cif({ orgKey: "B" });
a === b; // true

SPEC.md#cif-3

2.0.0

cif(opts?): string

Defined in: generate/index.ts:371

The next CIF. See generateCif.

Omit<GenerateCifOptions, "seed">

string

dni(opts?): string

Defined in: generate/index.ts:367

The next DNI, or K/L/M NIF. See generateDni.

Omit<GenerateDniOptions, "seed">

string

invalid(type, opts?): string

Defined in: generate/index.ts:375

The next invalid value. See generateInvalid.

NifType

Omit<GenerateInvalidOptions, "seed">

string

nie(opts?): string

Defined in: generate/index.ts:369

The next NIE. See generateNie.

Omit<GenerateNieOptions, "seed">

string

nif(opts?): string

Defined in: generate/index.ts:373

The next NIF of any of the types. See generateNif.

Omit<GenerateNifOptions, "seed">

string

CifControl = "letter" | "digit"

Defined in: generate/core.ts:65

The control character of a CIF: a digit or a letter (CIF-3).

generateCif({ seed: 1, control: "letter" }); // "R0027357C"
const control: CifControl = "digit";
validate(generateCif({ control })).valid; // true

SPEC.md#cif-3

2.0.0


DniKind = "DNI" | "K" | "L" | "M"

Defined in: generate/core.ts:41

What generateDni makes: a DNI, or a K, L or M NIF (KLM-1).

generateDni({ seed: 1, kind: "K" }); // "K6270739L"
const kinds: DniKind[] = ["DNI", "K", "L", "M"];
kinds.every((kind) => isValidDni(generateDni({ kind }))); // true

SPEC.md#klm-1

2.0.0


NiePrefix = "X" | "Y" | "Z"

Defined in: generate/core.ts:53

The first letter of an NIE (NIE-1): X, Y or Z.

generateNie({ seed: 1, prefix: "Z" }); // "Z6270739R"
const prefix: NiePrefix = "Y";
generateNie({ prefix }).startsWith("Y"); // true

SPEC.md#nie-1

2.0.0