UNPKG

@dwp/govuk-casa

Version:

A framework for building GOVUK Collect-And-Submit-Applications

57 lines 2.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ValidationError_js_1 = __importDefault(require("../ValidationError.js")); const ValidatorFactory_js_1 = __importDefault(require("../ValidatorFactory.js")); const utils_js_1 = require("../utils.js"); /** * @typedef {import("../../casa").ErrorMessageConfig} ErrorMessageConfig * @access private */ /** * @typedef {object} NinoConfigOptions * @property {ErrorMessageConfig} errorMsg Error message config * @property {boolean} allowWhitespace Will permit input values that contain * spaces. */ /** * UK National Insurance number. * * Ref: https://en.wikipedia.org/wiki/National_Insurance_number#Format * https://design-system.service.gov.uk/patterns/national-insurance-numbers/ * * See {@link NinoConfigOptions} for `make()` options. * * @memberof Validators * @augments ValidatorFactory */ class Nino extends ValidatorFactory_js_1.default { name = "nino"; validate(value, dataContext = {}) { const { allowWhitespace, errorMsg = { inline: "validation:rule.nino.inline", summary: "validation:rule.nino.summary", }, } = this.config; if (typeof allowWhitespace !== "undefined" && typeof allowWhitespace !== "boolean") { throw new TypeError(`NINO validation rule option "allowWhitespace" must been a boolean. received ${typeof allowWhitespace}`); } const valid = typeof value === "string" && value .replace(typeof allowWhitespace !== "undefined" && allowWhitespace ? /\u0020/g : "", "") .match(/^(?!BG|GB|NK|KN|TN|NT|ZZ)[ABCEGHJ-PRSTW-Z][ABCEGHJ-NPRSTW-Z]\d{6}[A-D]$/i); return valid ? [] : [ValidationError_js_1.default.make({ errorMsg, dataContext })]; } sanitise(value) { if (value !== undefined) { return (0, utils_js_1.stringifyInput)(value); } return undefined; } } exports.default = Nino; //# sourceMappingURL=nino.js.map