class-validator-extended
Version:
Additional validators for class-validator.
52 lines (51 loc) • 2.46 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MIN_DAYJS = void 0;
exports.MinDayjs = MinDayjs;
const class_validator_1 = require("class-validator");
const dayjs_1 = __importDefault(require("dayjs"));
const min_dayjs_predicate_1 = require("./min-dayjs.predicate");
/** @hidden */
exports.MIN_DAYJS = 'minDayjs';
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) ? 'before or on' : 'before'} $constraint1`;
}
/**
* Checks if the given value is a valid Dayjs object not earlier than `minimum`.
*
* #### Example
* ```typescript
* // Ensure the value is after the infamous Y2K.
* @MinDayjs('2000-01-01Z', { granularity: 'day', inclusive: true })
* y2kUnsafeDate: Dayjs
* ```
*
* @category Dayjs
* @param minimum The minimum 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 `minimum` 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 MinDayjs(minimum, options) {
return (0, class_validator_1.ValidateBy)({
name: exports.MIN_DAYJS,
constraints: [(0, dayjs_1.default)(minimum).toISOString()],
validator: {
validate: (value, _arguments) => (0, min_dayjs_predicate_1.minDayjs)(value, minimum, {
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);
}