@dwp/govuk-casa
Version:
A framework for building GOVUK Collect-And-Submit-Applications
47 lines • 1.68 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ValidatorFactory_js_1 = __importDefault(require("../ValidatorFactory.js"));
const ValidationError_js_1 = __importDefault(require("../ValidationError.js"));
const utils_js_1 = require("../utils.js");
/**
* @typedef {import("../../casa").ErrorMessageConfig} ErrorMessageConfig
* @access private
*/
/**
* @typedef {object} RegexConfigOptions
* @property {ErrorMessageConfig} errorMsg Error message config
* @property {RegExp} pattern Regular expression to test against
* @property {boolean} invert Return error on positive regex match
*/
/**
* Match a string pattern.
*
* See {@link RegexConfigOptions} for `make()` options.
*
* @memberof Validators
* @augments ValidatorFactory
*/
class Regex extends ValidatorFactory_js_1.default {
name = "regex";
validate(value = "", dataContext = {}) {
const invert = this.config.invert || false;
const match = value.match(this.config.pattern || /.*/);
const valid = invert ? !match : match;
const errorMsg = this.config.errorMsg || {
inline: "validation:rule.regex.inline",
summary: "validation:rule.regex.summary",
};
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 = Regex;
//# sourceMappingURL=regex.js.map