@apizr-io/class-utils
Version:
Package containing all class-validator function with all custom apizr class validation functions
91 lines • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IsFutureDate = exports.IsAfterDate = exports.IsBeforeDate = void 0;
const class_validator_1 = require("class-validator");
function IsBeforeDate(property, validationOptions) {
return (object, propertyName) => {
(0, class_validator_1.registerDecorator)({
name: 'isBeforeDate',
target: object.constructor,
propertyName,
constraints: [property],
options: {
message: (value) => {
const [relatedPropertyName] = value.constraints;
return `Date ${value.property} must be before ${relatedPropertyName}`;
},
...validationOptions,
},
validator: {
validate(value, args) {
var _a;
const [relatedPropertyName] = args.constraints;
const relatedValue = args.object[relatedPropertyName];
if (((_a = validationOptions === null || validationOptions === void 0 ? void 0 : validationOptions.context) === null || _a === void 0 ? void 0 : _a.otherDateIsOptional) &&
!relatedValue) {
return true;
}
return (typeof value === 'string' &&
typeof relatedValue === 'string' &&
new Date(value) < new Date(relatedValue));
},
},
});
};
}
exports.IsBeforeDate = IsBeforeDate;
function IsAfterDate(property, validationOptions) {
return (object, propertyName) => {
(0, class_validator_1.registerDecorator)({
name: 'isAfterDate',
target: object.constructor,
propertyName,
constraints: [property],
options: {
message: (value) => {
const [relatedPropertyName] = value.constraints;
return `Date ${value.property} must be after ${relatedPropertyName}`;
},
...validationOptions,
},
validator: {
validate(value, args) {
var _a;
const [relatedPropertyName] = args.constraints;
const relatedValue = args.object[relatedPropertyName];
if (((_a = validationOptions === null || validationOptions === void 0 ? void 0 : validationOptions.context) === null || _a === void 0 ? void 0 : _a.otherDateIsOptional) &&
!relatedValue) {
return true;
}
return (typeof value === 'string' &&
typeof relatedValue === 'string' &&
new Date(value) > new Date(relatedValue));
},
},
});
};
}
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