UNPKG

tsbase

Version:

Base class libraries for TypeScript

25 lines 1.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RegExpValidation = void 0; const Command_1 = require("../../Patterns/CommandQuery/Command"); class RegExpValidation { constructor(member, regex, customErrorMessage) { this.member = member; this.regex = regex; this.customErrorMessage = customErrorMessage; } Validate(object) { return new Command_1.Command(() => { const value = object[this.member]; const regex = new RegExp(this.regex); const regexTestPasses = regex.test(value); if (!!value && // don't enforce if falsy - leave that to required validation !regexTestPasses) { const label = object.LabelFor(this.member); throw new Error(this.customErrorMessage || `${label} value: ${value} does not conform to the expected regular expression: ${regex}`); } }).Execute(); } } exports.RegExpValidation = RegExpValidation; //# sourceMappingURL=RegExpValidation.js.map