@apizr-io/class-utils
Version:
Package containing all class-validator function with all custom apizr class validation functions
116 lines • 4.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IsFutureDate = exports.IsAfterDate = exports.IsBeforeDate = void 0;
const class_validator_1 = require("class-validator");
function getLimit(args, validationOptions) {
var _a;
const [propertyObject] = args.constraints;
let limit;
if (typeof propertyObject === 'function') {
limit = propertyObject(args.object);
}
else {
const rawValue = args.object[propertyObject];
if (((_a = validationOptions === null || validationOptions === void 0 ? void 0 : validationOptions.context) === null || _a === void 0 ? void 0 : _a.otherDateIsOptional) && !limit) {
return true;
}
if (typeof rawValue !== 'string') {
return false;
}
limit = new Date(rawValue);
}
return limit;
}
function IsBeforeDate(property, validationOptions) {
return (object, propertyName) => {
(0, class_validator_1.registerDecorator)({
name: 'isBeforeDate',
target: object.constructor,
constraints: [property],
propertyName,
options: {
message: (args) => {
const limit = getLimit(args, validationOptions);
if (limit instanceof Date) {
return `Date ${args.property} must be before ${limit.toISOString()}`;
}
if (typeof limit === 'string') {
return `Date ${args.property} must be before ${limit}`;
}
return `Invalid date format for ${args.property}`;
},
...validationOptions,
},
validator: {
validate(value, args) {
const limitResult = getLimit(args, validationOptions);
if (typeof limitResult === 'boolean') {
return limitResult;
}
return value instanceof Date
? value < limitResult
: typeof value === 'string' && new Date(value) < limitResult;
},
},
});
};
}
exports.IsBeforeDate = IsBeforeDate;
function IsAfterDate(property, validationOptions) {
return (object, propertyName) => {
(0, class_validator_1.registerDecorator)({
name: 'isAfterDate',
target: object.constructor,
constraints: [property],
propertyName,
options: {
message: (args) => {
const limit = getLimit(args, validationOptions);
if (limit instanceof Date) {
return `Date ${args.property} must be after ${limit.toISOString()}`;
}
if (typeof limit === 'string') {
return `Date ${args.property} must be after ${limit}`;
}
return `Invalid date format for ${args.property}`;
},
...validationOptions,
},
validator: {
validate(value, args) {
const limitResult = getLimit(args, validationOptions);
if (typeof limitResult === 'boolean') {
return limitResult;
}
return value instanceof Date
? value > limitResult
: typeof value === 'string' && new Date(value) > limitResult;
},
},
});
};
}
exports.IsAfterDate = IsAfterDate;
function IsFutureDate() {
return (object, propertyName) => {
(0, class_validator_1.registerDecorator)({
name: 'isFutureDate',
target: object.constructor,
propertyName,
constraints: [],
options: {
message: (value) => `Date ${value.property} must be in the future`,
},
validator: {
validate: (value) => {
if (value instanceof Date) {
return value > new Date();
}
return typeof value === 'string' && new Date(value) > new Date();
},
},
});
};
}
exports.IsFutureDate = IsFutureDate;
//# sourceMappingURL=date.decorator.js.map