UNPKG

class-validator-extended

Version:
51 lines (50 loc) 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MIN_DURATION = void 0; exports.MinDuration = MinDuration; const class_validator_1 = require("class-validator"); const create_duration_1 = require("../create-duration"); const is_valid_duration_1 = require("../is-valid-duration"); const min_duration_predicate_1 = require("./min-duration.predicate"); /** @hidden */ exports.MIN_DURATION = 'minDuration'; function message(options) { return `a valid Dayjs duration ${(options === null || options === void 0 ? void 0 : options.inclusive) ? 'equal to or not shorter than' : 'not shorter than'} $constraint1`; } /** * Checks if the given value is a valid Dayjs duration not longer than `minimum`. * * The minimum can be given as either: * - a Dayjs duration * - an ISO 8601 string, e.g. `"PT1H"` * - a units object, e.g. `{ hour: 1, minute: 30 }` * - a time/unit tuple, e.g. `[1, 'hour]` * * #### Example * ```typescript * // Ensure the value is a duration at least 1 hour long * @MinDuration([1, 'hour'], { inclusive: true }) * value: Duration * ``` * * @category Dayjs * @param minimum The minimum allowed value. * @param options * Accepts the following options (in addition to generic class-validator options): * - `inclusive: boolean = false` * If true, allow the `minimum` duration as well. */ function MinDuration(minimum, options) { const min = (0, create_duration_1.createDuration)(minimum); if (!(0, is_valid_duration_1.isValidDuration)(min)) { throw new TypeError('Parameter "minimum" must be a valid Dayjs duration'); } return (0, class_validator_1.ValidateBy)({ name: exports.MIN_DURATION, constraints: [min.toISOString()], validator: { validate: (value, _arguments) => (0, min_duration_predicate_1.minDuration)(value, min, { inclusive: options === null || options === void 0 ? void 0 : options.inclusive }), defaultMessage: (0, class_validator_1.buildMessage)(eachPrefix => `${eachPrefix}$property must be ${message(options)}`, options), }, }, options); }