Home > OS >  class-transformer @Type() decorator doesn't work
class-transformer @Type() decorator doesn't work

Time:06-28

I have a dto file:

export class UpdateUserDto {
  @IsUUID()
  public readonly uuid: string;

  @IsObject()
  @Type(() => UserModelDto)
  public readonly dataToUpdate: UserModelDto;
}

The problem is, it seems @Type() decorator doesn't work. My UserModelDto looks like this:

export class UserModelDto {
  @IsUUID()
  @IsOptional()
  public uuid?: string;

  @IsEmail()
  @IsOptional()
  public email?: string;

  @IsString()
  @IsOptional()
  public password?: string;

  @IsJWT()
  @IsOptional()
  public refreshToken?: string;
}

When I send a request to a controller validation doesn't work for dataToUpdate field however for uuid it does. I've tried many ways but result remains the same.

CodePudding user response:

You have forgot to add @validateNested decorator.

CodePudding user response:

You need to enable { transform: true } inside the ValidationPipe options:

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

reference: https://docs.nestjs.com/techniques/validation#transform-payload-objects

  • Related