diff --git a/src/trip/dto/create-trip.dto.ts b/src/trip/dto/create-trip.dto.ts index c257c96..49d1fd4 100644 --- a/src/trip/dto/create-trip.dto.ts +++ b/src/trip/dto/create-trip.dto.ts @@ -12,11 +12,13 @@ import { Matches, MaxLength, Min, + Validate, } from 'class-validator'; import { NestedObjectValidator } from 'src/common/decorators/nested-object-validator.decorator'; import { ApiProperty } from '@nestjs/swagger'; import { Type } from 'class-transformer'; import { RegexEnum } from '../types'; +import { NotMoreThanMonth } from './validators/estimated-departure-date.validator'; export class VehiclePlateDto { @ApiProperty({ @@ -192,7 +194,13 @@ export class CreateTripDto { }) @IsNotEmpty() @IsDateString() - // TODO: allow up to 30 days later + @Validate(NotMoreThanMonth, { + message: `Estimated depart time should be between ${new Date( + Date.now() + )} and ${new Date( + new Date(Date.now()).setMonth(new Date(Date.now()).getMonth() + 1) + )}`, + }) estimatedDepartTime: string; @ApiProperty({ diff --git a/src/trip/dto/validators/estimated-departure-date.validator.ts b/src/trip/dto/validators/estimated-departure-date.validator.ts new file mode 100644 index 0000000..60c277e --- /dev/null +++ b/src/trip/dto/validators/estimated-departure-date.validator.ts @@ -0,0 +1,18 @@ +import { + ValidatorConstraint, + ValidatorConstraintInterface, +} from 'class-validator'; +import { Injectable } from '@nestjs/common'; + +@ValidatorConstraint() +@Injectable() +export class NotMoreThanMonth implements ValidatorConstraintInterface { + public async validate(estimatedDepartTime: Date) { + const dateNow = new Date(Date.now()); + return ( + new Date(estimatedDepartTime) <= + new Date(dateNow.setMonth(dateNow.getMonth() + 1)) && + new Date(estimatedDepartTime) >= new Date(Date.now()) + ); + } +}