UNPKG

@dwp/govuk-casa

Version:

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

77 lines 2.8 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Test if a value is present in an array. * * Config options: Array source = Array of values to test against * * If the value itself is an array, all values within that array must be present * in the `source` array in order to pass validation. */ 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} ArrayConfigOptions * @property {ErrorMessageConfig} errorMsg Error message config * @property {string[]} source Array of values to test against */ /** * Test if a value is present in an array. * * If the value itself is an array, all values within that array must be present * in the `source` array in order to pass validation. * * See {@link ArrayConfigOptions} for `make()` options. * * @memberof Validators * @augments ValidatorFactory */ class InArray extends ValidatorFactory_js_1.default { /** @property {string} name Validator name ("inArray") */ name = "inArray"; validate(value, dataContext = {}) { let valid = false; const source = this.config.source || []; const errorMsg = this.config.errorMsg || { inline: "validation:rule.inArray.inline", summary: "validation:rule.inArray.summary", }; if (value !== null && typeof value !== "undefined") { const search = Array.isArray(value) ? value : [value]; for (let i = 0, l = search.length; i < l; i += 1) { if (source.indexOf(search[parseInt(i, 10)]) > -1) { valid = true; } else { valid = false; break; } } } return valid ? [] : [ValidationError_js_1.default.make({ errorMsg, dataContext })]; } sanitise(value) { const coerce = (val) => (0, utils_js_1.stringifyInput)(val, undefined); // Basic stringable if ((0, utils_js_1.isStringable)(value)) { return (0, utils_js_1.stringifyInput)(value); } // Coerce all elements to Strings. // This only supports one dimensional array, with stringable element. if (Array.isArray(value)) { return value.map(coerce); } // Unsupported value return undefined; } } exports.default = InArray; //# sourceMappingURL=inArray.js.map