Param won't cast to DTO type in NestJS when trying to use class validators

Viewed 16

I'm trying to add validation to the param I get in the request for example to delete something. The param is a string but it has to be a valid UUID. I added the dto to the type of the param in the controller.

@Delete(':personId')
  async deletePersonIdentity(@Param('personId') id:deletePersonIdentityDto) {
    return this.personIdentityService.deletePersonIdentity(id.personId);
  }

The DTO for deletePersonIdentity looks like this.

export class deletePersonIdentityDto {  
  @ApiProperty({
    example: 'fd914b72-a423-4256-99a1-aff78da9281f',
    description: `ID of the Person`,
    required: true,
  })
  @IsUUID()
  readonly personId: string;
}

Even if I pass a valid UUID in the param I still get a bad request that says that the id must be a UUID. This is the error thrown by the class-validator. Any ideas?

1 Answers

The req.params['personId'] (what @Param('personId') maps to) does not satisfy the type deletePersonIdentityDto. What you're looking for is @Param() { personid }: deletePersonIdeentiyDto which is equivalent to const { personId } = req.params

Related