All files / src/yup index.ts

100% Statements 18/18
100% Branches 12/12
100% Functions 8/8
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243                                                                                                                                                                                            5245x 5245x                     68237x 68203x 68203x               68192x 68188x 68188x 62080x 62080x                                                                                         4124x                                       32x                                   26x                                   28x                                     1035x    
/**
 * Entry point of `nif-dni-nie-cif-validation/yup`: Yup schemas for Spanish
 * NIF, DNI, K/L/M, NIE and CIF numbers, and for Spanish VAT numbers.
 * Opt-in: the main entry point never imports this module, and it never
 * bundles Yup. Yup is an optional peer dependency (`yup@^1`): install it
 * yourself if you use this entry point.
 *
 * Every schema is `string().transform(...).test(...)`, so it works wherever
 * a Yup schema does: `object`, `array`, `.optional()`, `InferType`, Formik,
 * React Hook Form's `yupResolver`.
 *
 * - It accepts what `validate()` accepts with the same options, and its
 *   output is the **normalized** value (`" 12.345.678-z "` gives
 *   `"12345678Z"`). See src/adapter.ts.
 * - A value that is refused gives one `ValidationError` whose `message` is
 *   `validate().error.message`, in the locale you pass (`{ locale: es }`).
 *   Its `type` is `"nif"` and its `params` have the error `code` (for
 *   example `"INVALID_CONTROL_CHARACTER"`), the SPEC.md `rule` that failed
 *   (for example `"DNI-2"`) and, for a wrong control character, `expected`.
 * - Unlike Yup's own `string()`, a number is not turned into a string: a
 *   value that is not a string is refused with the `NOT_A_STRING` message
 *   (INPUT-1), and so are `undefined` and `null`. Yup's schemas are optional
 *   by default; these are not, so use `.optional()` for an optional field.
 *   `""` gives the `EMPTY` message.
 *
 * Yup has no `exports` map: in Node, `import { string } from "yup"` works
 * from the ES module build and `require("yup")` from the CommonJS one.
 *
 * Rule IDs refer to SPEC.md.
 */
import { type AnyObject, type StringSchema, string } from "yup";
import {
  checkNif,
  type NifSchemaKind,
  type NifSchemaOptions,
  notAStringMessage,
  type SpanishVatSchemaOptions,
  type TypedNifSchemaOptions,
} from "../adapter";
import type { NifErrorCode } from "../types";
 
export type {
  NifSchemaOptions,
  SpanishVatSchemaOptions,
  TypedNifSchemaOptions,
} from "../adapter";
 
/**
 * The schemas of this entry point: a required string, transformed to its
 * normalized form. Its input and its output are `string`.
 * @example
 * import { type InferType } from "yup";
 * import { type NifSchema, yNif } from "nif-dni-nie-cif-validation/yup";
 *
 * const schema: NifSchema = yNif();
 * const output: InferType<NifSchema> = schema.validateSync(" 12.345.678-z ");
 * output; // "12345678Z"
 * @example
 * import { object } from "yup";
 * import { yDni } from "nif-dni-nie-cif-validation/yup";
 *
 * object({ nif: yDni() }).isValidSync({ nif: "12345678A" }); // false
 * @see {@link https://github.com/josegoval/nif-dni-nie-cif-validation/blob/master/SPEC.md#nif-1 SPEC.md#nif-1}
 * @since 2.0.0
 */
export type NifSchema = StringSchema<string, AnyObject, undefined, "">;
 
/**
 * The `params` of the error of a refused value: what `validate()` says.
 * @example
 * import { ValidationError } from "yup";
 *
 * let params: NifErrorParams | undefined;
 * try {
 *   yDni().validateSync("12345678A");
 * } catch (error) {
 *   params = (error as ValidationError).params as NifErrorParams | undefined;
 * }
 * params?.code; // "INVALID_CONTROL_CHARACTER"
 * params?.rule; // "DNI-2"
 * @see {@link https://github.com/josegoval/nif-dni-nie-cif-validation/blob/master/SPEC.md#dni-2 SPEC.md#dni-2}
 * @since 2.0.0
 */
export interface NifErrorParams {
  /** The error code of `validate()`. */
  code: NifErrorCode;
  /** The SPEC.md rule that failed, for example `"DNI-2"`. */
  rule: string;
  /** The right control character, for `INVALID_CONTROL_CHARACTER`. */
  expected?: string;
}
 
function schema(kind: NifSchemaKind, opts?: NifSchemaOptions): NifSchema {
  // INPUT-1: undefined, null and other types are not strings.
  const notAString = notAStringMessage(opts);
  return (
    string()
      .typeError(notAString)
      .defined(notAString)
      .nonNullable(notAString)
      // Yup runs the transforms first, and the tests on their result: a
      // valid value becomes its normalized form, and any other stays as it
      // is, so the test below gives the error for what was typed.
      .transform((value, original) => {
        // Yup turns numbers and other values into strings before this: the
        // original tells what was really given (INPUT-1).
        if (typeof original !== "string") return original;
        const result = checkNif(value, kind, opts);
        return result.ok ? result.value : value;
      })
      .test({
        name: "nif",
        test(value, context) {
          // Yup runs a test on undefined and null too. `defined` and
          // `nonNullable` (above) already refuse them unless the field was
          // made optional or nullable, so here they are accepted.
          if (value === undefined || value === null) return true;
          const result = checkNif(value, kind, opts);
          if (result.ok) return true;
          const { code, rule, expected, message } = result.error;
          return context.createError({
            message,
            params:
              expected === undefined
                ? { code, rule }
                : { code, rule, expected },
          });
        },
      })
  );
}
 
/**
 * A Yup 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.
 *
 * @param opts `types`, `normalize`, `cifControl`, `rejectPlaceholders`,
 * `allowVatPrefix`, `locale`. See {@link NifSchemaOptions}.
 * @returns A schema from `string` to the normalized `string`.
 * @example
 * import { object } from "yup";
 * import { yNif } from "nif-dni-nie-cif-validation/yup";
 * import { es } from "nif-dni-nie-cif-validation/locales/es";
 *
 * const form = object({ nif: yNif({ types: ["DNI", "NIE"], locale: es }) });
 * form.validateSync({ nif: " 12.345.678-z " }); // { nif: "12345678Z" }
 * @example
 * import { ValidationError } from "yup";
 *
 * let error: ValidationError | undefined;
 * try {
 *   yNif().validateSync("12345678A");
 * } catch (caught) {
 *   error = caught as ValidationError;
 * }
 * error?.message;
 * // 'The control character is not correct: for this DNI it should be "Z".'
 * error?.params?.rule;     // "DNI-2"
 * error?.params?.expected; // "Z"
 * @see {@link https://github.com/josegoval/nif-dni-nie-cif-validation/blob/master/SPEC.md#nif-1 SPEC.md#nif-1}
 * @since 2.0.0
 */
export function yNif(opts?: NifSchemaOptions): NifSchema {
  return schema("nif", opts);
}
 
/**
 * A Yup 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).
 *
 * @param opts The options of `validate()` except `types`.
 * @returns A schema from `string` to the normalized `string`.
 * @example
 * yDni().validateSync("1234567-l"); // "01234567L"
 * @example
 * yDni().isValidSync("X1234567L"); // false: UNSUPPORTED_TYPE
 * @see {@link https://github.com/josegoval/nif-dni-nie-cif-validation/blob/master/SPEC.md#dni-1 SPEC.md#dni-1}
 * @see {@link https://github.com/josegoval/nif-dni-nie-cif-validation/blob/master/SPEC.md#klm-1 SPEC.md#klm-1}
 * @since 2.0.0
 */
export function yDni(opts?: TypedNifSchemaOptions): NifSchema {
  return schema("dni", opts);
}
 
/**
 * A Yup schema for an NIE. The output is the canonical 9-character form: an
 * old 10-character NIE is shortened (NIE-3).
 *
 * @param opts The options of `validate()` except `types`.
 * @returns A schema from `string` to the normalized `string`.
 * @example
 * yNie().validateSync("x-01234567-l"); // "X1234567L"
 * @example
 * yNie().isValidSync("12345678Z"); // false: a DNI is not an NIE
 * @see {@link https://github.com/josegoval/nif-dni-nie-cif-validation/blob/master/SPEC.md#nie-1 SPEC.md#nie-1}
 * @see {@link https://github.com/josegoval/nif-dni-nie-cif-validation/blob/master/SPEC.md#nie-3 SPEC.md#nie-3}
 * @since 2.0.0
 */
export function yNie(opts?: TypedNifSchemaOptions): NifSchema {
  return schema("nie", opts);
}
 
/**
 * A Yup schema for the NIF of a legal person or entity (CIF). The control
 * character follows CIF-3 unless `cifControl: "lenient"`.
 *
 * @param opts The options of `validate()` except `types`.
 * @returns A schema from `string` to the normalized `string`.
 * @example
 * yCif().validateSync(" b-1234567-4 "); // "B12345674"
 * @example
 * yCif().isValidSync("G1234567D");                         // false (CIF-3)
 * yCif({ cifControl: "lenient" }).isValidSync("G1234567D"); // true
 * @see {@link https://github.com/josegoval/nif-dni-nie-cif-validation/blob/master/SPEC.md#cif-3 SPEC.md#cif-3}
 * @since 2.0.0
 */
export function yCif(opts?: TypedNifSchemaOptions): NifSchema {
  return schema("cif", opts);
}
 
/**
 * A Yup 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.
 *
 * @param opts The options of `validate()` except `allowVatPrefix`.
 * @returns A schema from `string` to the normalized `string`.
 * @example
 * ySpanishVat().validateSync("es b-1234567-4"); // "ESB12345674"
 * @example
 * ySpanishVat().isValidSync("B12345674"); // false: no ES prefix
 * @see {@link https://github.com/josegoval/nif-dni-nie-cif-validation/blob/master/SPEC.md#vat-1 SPEC.md#vat-1}
 * @since 2.0.0
 */
export function ySpanishVat(opts?: SpanishVatSchemaOptions): NifSchema {
  return schema("vat", opts);
}