1
0
mirror of https://github.com/immich-app/immich.git synced 2024-11-24 08:52:28 +02:00

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

@ -1,6 +1,7 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsArray, IsInt, IsNotEmpty, IsString, Max, Min, ValidateNested } from 'class-validator';
import { DateTime } from 'luxon';
import { PropertyLifecycle } from 'src/decorators';
import { AuthDto } from 'src/dtos/auth.dto';
import { AssetFaceEntity } from 'src/entities/asset-face.entity';
@ -20,7 +21,7 @@ export class PersonCreateDto {
* Note: the mobile app cannot currently set the birth date to null.
*/
@ApiProperty({ format: 'date' })
@MaxDateString(() => new Date(), { message: 'Birth date cannot be in the future' })
@MaxDateString(() => DateTime.now(), { message: 'Birth date cannot be in the future' })
@IsDateStringFormat('yyyy-MM-dd')
@Optional({ nullable: true })
birthDate?: string | null;

View File

@ -1,10 +1,11 @@
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { DateTime } from 'luxon';
import { IsDateStringFormat, MaxDateString } from 'src/validation';
describe('Validation', () => {
describe('MaxDateString', () => {
const maxDate = new Date(2000, 0, 1);
const maxDate = DateTime.fromISO('2000-01-01', { zone: 'utc' });
class MyDto {
@MaxDateString(maxDate)

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(