UNPKG

tsbase

Version:

Base class libraries for TypeScript

27 lines 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringLengthValidation = void 0; const Command_1 = require("../../Patterns/CommandQuery/Command"); class StringLengthValidation { 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 valueIsString = typeof value === 'string'; const valueWithinRange = valueIsString && value.length >= this.minimum && value.length <= this.maximum; if (!valueWithinRange) { const label = object.LabelFor(this.member); throw new Error(this.customErrorMessage || `\"${label}\" length must be within ${this.minimum} and ${this.maximum} characters.`); } }).Execute(); } } exports.StringLengthValidation = StringLengthValidation; //# sourceMappingURL=StringLengthValidation.js.map