class-validator-extended
Version:
Additional validators for class-validator.
48 lines (47 loc) • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PAST_DAYJS = void 0;
exports.PastDayjs = PastDayjs;
const class_validator_1 = require("class-validator");
const past_dayjs_predicate_1 = require("./past-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 past or today' : 'in the past'}`;
}
/** @hidden */
exports.PAST_DAYJS = 'pastDayjs';
/**
* Checks if the given value is a valid Dayjs object in the past.
*
* Beware that the behaviour of this check depends on the current time and can thus be difficult to test.
*
* #### Example
* ```typescript
* // Ensure the value is in the past.
* @PastDayjs()
* created: 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 PastDayjs(options) {
return (0, class_validator_1.ValidateBy)({
name: exports.PAST_DAYJS,
validator: {
validate: (value, _arguments) => (0, past_dayjs_predicate_1.pastDayjs)(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);
}