UNPKG

@dwp/govuk-casa

Version:

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

95 lines 3.02 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = __importDefault(require("lodash")); const { isPlainObject } = lodash_1.default; // CommonJS /** * @typedef {import("../casa").ErrorMessageConfig} ErrorMessageConfig * @access private */ /** * @typedef {import("./index").JourneyContext} JourneyContext * @access private */ /** * @typedef {import("./index").ValidationError} ValidationError * @access private */ /** * @typedef {import("../casa").ValidateContext} ValidateContext * @access private */ /** * @typedef {import("../casa").Validator} Validator * @access private */ /** * @typedef {Object<string, unknown>} ValidatorFactoryOptions * @property {ErrorMessageConfig} errorMsg Error message */ /** * @memberof module:@dwp/govuk-casa * @class */ class ValidatorFactory { /** * This is a convenience method that will return a consistently object * structure containing validation and sanitisation methods. * * @param {ValidatorFactoryOptions} config Validator config (custom to each * validator) * @returns {Validator} Validator object * @throws {TypeError} When configuration is invalid. */ static make(config = {}) { if (!isPlainObject(config)) { throw new TypeError("Configuration must be an object"); } const validator = Reflect.construct(this, [config]); const instance = {}; instance.name = validator.name || "unknown"; instance.config = config; instance.validate = validator.validate.bind(instance); instance.sanitise = validator.sanitise.bind(instance); Object.freeze(instance); return instance; } /** * NEVER CALL THIS DIRECTLY. USE `make()`. * * @param {ValidatorFactoryOptions} config Validator config (custom to each * validator) */ constructor(config = {}) { if (new.target === ValidatorFactory) { throw new TypeError("Cannot instantiate the abstract class, ValidatorFactory"); } this.config = config; } /* eslint-disable no-unused-vars */ /* eslint-disable-next-line jsdoc/require-returns-check */ /** * Validate the given value. * * @param {any} fieldValue Value to validate * @param {ValidateContext} context Contextual information * @returns {ValidationError[]} A list of errors (empty if no errors found) * @throws {Error} */ validate(fieldValue, context) { throw new Error("validate() method has not been implemented"); } /** * Sanitise the given value. * * @param {any} fieldValue Value to validate * @returns {any} The sanitised value */ sanitise(fieldValue) { return fieldValue; } } exports.default = ValidatorFactory; //# sourceMappingURL=ValidatorFactory.js.map