fix(server): use luxon for maxdate validator (#11651)

This commit is contained in:
Michel Heusschen 2024-08-08 16:02:39 +02:00 committed by GitHub
parent 66f2ac8ce3
commit 4a42a72bd3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 5 deletions

View file

@ -21,7 +21,6 @@ import {
ValidationOptions,
buildMessage,
isDateString,
maxDate,
} from 'class-validator';
import { CronJob } from 'cron';
import { DateTime } from 'luxon';
@ -203,14 +202,21 @@ export function IsDateStringFormat(format: string, validationOptions?: Validatio
);
}
export function MaxDateString(date: Date | (() => Date), validationOptions?: ValidationOptions): PropertyDecorator {
function maxDate(date: DateTime, maxDate: DateTime | (() => DateTime)) {
return date <= (maxDate instanceof DateTime ? maxDate : maxDate());
}
export function MaxDateString(
date: DateTime | (() => DateTime),
validationOptions?: ValidationOptions,
): PropertyDecorator {
return ValidateBy(
{
name: 'maxDateString',
constraints: [date],
validator: {
validate: (value, args) => {
const date = DateTime.fromISO(value, { zone: 'utc' }).toJSDate();
const date = DateTime.fromISO(value, { zone: 'utc' });
return maxDate(date, args?.constraints[0]);
},
defaultMessage: buildMessage(