UNPKG

class-validator-extended

Version:
52 lines (51 loc) 2.44 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MAX_DAYJS = void 0; exports.MaxDayjs = MaxDayjs; const class_validator_1 = require("class-validator"); const dayjs_1 = __importDefault(require("dayjs")); const max_dayjs_predicate_1 = require("./max-dayjs.predicate"); /** @hidden */ exports.MAX_DAYJS = 'maxDayjs'; function message(options) { return `${(options === null || options === void 0 ? void 0 : options.allow_invalid) ? 'a' : 'a valid'} Dayjs object not ${(options === null || options === void 0 ? void 0 : options.inclusive) ? 'after or on' : 'after'} $constraint1`; } /** * Checks if the given value is a valid Dayjs object not later than `maximum`. * * #### Example * ```typescript * // Ensure the value is before the infamous Y2K. * @MaxDayjs('2000-01-01Z', { granularity: 'day' }) * y2kSafeDate: Dayjs * ``` * * @category Dayjs * @param maximum The maximum allowed value. * @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 `maximum` 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 MaxDayjs(maximum, options) { return (0, class_validator_1.ValidateBy)({ name: exports.MAX_DAYJS, constraints: [(0, dayjs_1.default)(maximum).toISOString()], validator: { validate: (value, _arguments) => (0, max_dayjs_predicate_1.maxDayjs)(value, maximum, { 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); }