UNPKG

@dwp/govuk-casa

Version:

Framework for creating basic GOVUK Collect-And-Submit-Applications

40 lines (34 loc) 1.12 kB
/* eslint-disable class-methods-use-this */ /** * Match a string pattern. * * Config options: * string|object errorMsg = Error message to use on validation failure * RegExp pattern = Regular expression to test against * boolean invert = return reject on positive regex match */ const ValidatorFactory = require('../ValidatorFactory.js'); const ValidationError = require('../ValidationError.js'); const { stringifyInput } = require('../../Util.js'); class Regex extends ValidatorFactory { 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 ? Promise.resolve() : Promise.reject(ValidationError.make({ errorMsg, dataContext, })); } sanitise(value) { if (value !== undefined) { return stringifyInput(value); } return undefined; } } module.exports = Regex;