tsbase
Version:
Base class libraries for TypeScript
27 lines • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RangeValidation = void 0;
const Command_1 = require("../../Patterns/CommandQuery/Command");
class RangeValidation {
constructor(member, minimum, maximum, customErrorMessage) {
this.member = member;
this.minimum = minimum;
this.maximum = maximum;
this.customErrorMessage = customErrorMessage;
}
Validate(object) {
return new Command_1.Command(() => {
const value = object[this.member];
const valueIsNumeric = !isNaN(parseFloat(value.toString()));
const valueWithinRange = valueIsNumeric &&
value >= this.minimum && value <= this.maximum;
if (!valueWithinRange) {
const label = object.LabelFor(this.member);
throw new Error(this.customErrorMessage ||
`\"${label}\" must be within the range of ${this.minimum} and ${this.maximum}.`);
}
}).Execute();
}
}
exports.RangeValidation = RangeValidation;
//# sourceMappingURL=RangeValidation.js.map