tsbase
Version:
Base class libraries for TypeScript
27 lines • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RegExpValidation = void 0;
var Command_1 = require("../../Patterns/CommandQuery/Command");
var RegExpValidation = /** @class */ (function () {
function RegExpValidation(member, regex, customErrorMessage) {
this.member = member;
this.regex = regex;
this.customErrorMessage = customErrorMessage;
}
RegExpValidation.prototype.Validate = function (object) {
var _this = this;
return new Command_1.Command(function () {
var value = object[_this.member];
var regex = new RegExp(_this.regex);
var regexTestPasses = regex.test(value);
if (!!value && // don't enforce if falsy - leave that to required validation
!regexTestPasses) {
var label = object.LabelFor(_this.member);
throw new Error(_this.customErrorMessage || "".concat(label, " value: ").concat(value, " does not conform to the expected regular expression: ").concat(regex));
}
}).Execute();
};
return RegExpValidation;
}());
exports.RegExpValidation = RegExpValidation;
//# sourceMappingURL=RegExpValidation.js.map