nestjs-swagger-dto - startTime is not ISO8601 format error

33 views Asked by At

I'm using the nestjs-swagger-dto package and I'm getting the error "startTime is not ISO8601 format." I don't understand why, because the startTime property in the request body is a string in ISO8601 format.

Here is my MyDto class:

export class MyDto {
  @IsDate({
    nullable: true,
    format: 'date-time',
  })
  startTime: Date | null;
}

Here is my Post request body: JSON

{
  "startTime": "2023-10-24T16:13:58.325035"
}

Here is my controller code:

@Post()
async create(@Body() dto: MyDto) {
  // ...
}

I have a global validation pipe in my main.ts file:

app.useGlobalPipes(
  new ValidationPipe({
    forbidUnknownValues: true,
    transform: true,
  }),
);

I tried copying the isDateString function from the nestjs-swagger-dto package to my MyDto.ts file and adding some console logs before and inside the if statement:

if (!isDateString(value, { strict: true })) {
  // ...
}

Here is the output of the console logs:

// this is a string
2023-10-24T16:13:58.325035

// this is a date object
2023-10-24T14:13:58.325Z

It seems like the validation is correctly phrasing the string and after that it's trying to validate a date object, which was previously converted and this is of course not a string. This is why isDateString is failing.

I'm not sure if this is a problem with my code, NestJS, or the nestjs-swagger-dto package. Can anyone help me here?

Thank you in advance

0

There are 0 answers