Skip to content

High performance self-hosted photo and video management solution.

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

Michel Heusschen4a42a72bd368
diff --git a/server/src/dtos/person.dto.ts b/server/src/dtos/person.dto.ts
index 573651c3f3..3833e4f3e7 100644
--- a/server/src/dtos/person.dto.ts
+++ b/server/src/dtos/person.dto.ts
@@ -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;
diff --git a/server/src/validation.spec.ts b/server/src/validation.spec.ts
index d470918107..7cd7826223 100644
--- a/server/src/validation.spec.ts
+++ b/server/src/validation.spec.ts
@@ -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)
diff --git a/server/src/validation.ts b/server/src/validation.ts
index 10f2e7b77b..81b309d663 100644
--- a/server/src/validation.ts
+++ b/server/src/validation.ts
@@ -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(