UNPKG

class-validator-extended

Version:
49 lines (48 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FUTURE_DAYJS = void 0; exports.FutureDayjs = FutureDayjs; const class_validator_1 = require("class-validator"); const future_dayjs_predicate_1 = require("./future-dayjs.predicate"); function message(options) { return `${(options === null || options === void 0 ? void 0 : options.allow_invalid) ? 'a' : 'a valid'} Dayjs object ${(options === null || options === void 0 ? void 0 : options.inclusive) ? 'in the future or today' : 'in the future'}`; } /** @hidden */ exports.FUTURE_DAYJS = 'futureDayjs'; /** * Checks if the given value is a valid Dayjs object in the future. * * Beware that the behaviour of this check depends on the current time and can thus be difficult to test. In particular, * as time goes by the property can become invalid without ever changing its value. * * #### Example * ```typescript * // Ensure the value is in the future. * @FutureDayjs() * scheduledFor: Dayjs * ``` * * @category Dayjs * @param options * Accepts the following options (in addition to generic class-validator options): * - `allow_invalid: boolean = false` * If true, allow the Dayjs object to be invalid (see [isValid()](https://day.js.org/docs/en/parse/is-valid)). * - `inclusive: boolean = false` * If true, allow the current date as well. * - `granularity: string = 'milliseconds'` * Defines the [granularity](https://day.js.org/docs/en/manipulate/start-of#list-of-all-available-units), e.g. "day" * to ignore hours, minutes, seconds and milliseconds. */ function FutureDayjs(options) { return (0, class_validator_1.ValidateBy)({ name: exports.FUTURE_DAYJS, validator: { validate: (value, _arguments) => (0, future_dayjs_predicate_1.futureDayjs)(value, { allow_invalid: options === null || options === void 0 ? void 0 : options.allow_invalid, inclusive: options === null || options === void 0 ? void 0 : options.inclusive, granularity: options === null || options === void 0 ? void 0 : options.granularity, }), defaultMessage: (0, class_validator_1.buildMessage)(eachPrefix => `${eachPrefix}$property must be ${message(options)}`, options), }, }, options); }