#How to use Transform for DTOs in Nestjs

12 messages · Page 1 of 1 (latest)

junior beacon
#

I want to transform the request body to lowercase, this should be something simple, but for some strange reasons it doesnt work, how exactly can transformations be used with Dtos

tropic axle
#

@junior beacon does this not work?

export class CreateUserDto {
    @IsString()
    @Transform(({ value }) => value.toLowerCase())
    username: string;

    @IsString()
    @Transform(({ value }) => value.toLowerCase())
    email: string;
}
junior beacon
#

This works! Shocking how i battled with this all weekend and it didnt work

junior beacon
#

So here is my code where it wasnt working

export class SignupDto extends CommonFields {
  @IsEmail()
  @Transform(({ value }) => value.toLowerCase())
  email: string;

  @IsString()
  @Transform(({ value }) => value.toLowerCase())
  name: string;
}

Whereas, it works here

export class CreateCategoryDTO {
  @IsString()
  @Transform(({ value }) => value.toLowerCase())
  name: string;

  @IsString()
  @IsOptional()
  description: string;

  @IsNumber()
  amount: number;
}

What could be the issue?

potent harbor
#

Hi

#

Show us a controller where you use this DTO and a CommonFields class

junior beacon
potent harbor
#

What is CourierSignupDTO?

#

I guess that's the problem, how your app should know which DTO to use to validate fields?

junior beacon
#

its also a dto that extends the CommonFields class

potent harbor
#

There can only be 1 DTO per method. So create a dto that combines those 2 together

left moth