#How to use Transform for DTOs in Nestjs
12 messages · Page 1 of 1 (latest)
@junior beacon does this not work?
export class CreateUserDto {
@IsString()
@Transform(({ value }) => value.toLowerCase())
username: string;
@IsString()
@Transform(({ value }) => value.toLowerCase())
email: string;
}
This works! Shocking how i battled with this all weekend and it didnt work
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?
What is CourierSignupDTO?
I guess that's the problem, how your app should know which DTO to use to validate fields?
its also a dto that extends the CommonFields class
There can only be 1 DTO per method. So create a dto that combines those 2 together
You can create a custom decorator:
https://docs.nestjs.com/custom-decorators
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).