Search code examples
node.jstypescriptnestjsclass-validatorclass-transformer

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


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.


Solution

  • To ensure errors on validation when extra properties are sent in, you need to make use of the forbidNonWhitelisted option in the ValidaitonPipe. If you just want to strip the values you can use transform: true and whitelist: true