#not understanding DTO
1 messages · Page 1 of 1 (latest)
yeah i meant x-www-url-formencoded
sorry mb
Can you show the request you're making?
Oh, you're not using the validation pipe?
If that's the case, the user is coming in as a JSON object, not a class, and therefore has no decorators, so class-validator doesn't throw an error cause it doesn't know what should be there
Hmm?
What can I clarify?
Do I need a class transform pipe?
If you don't plan to use nest's ValidationPipe, then yes
First things first: do you plan to use the ValidationPipe from @nestjs/common?
yeah
Then why are you calling validate yourself?
i am not sure, i thought validate will validate the INFO based on the DTO
It would have, if the body was actually an instance of the DTO and not just a JSON object
i see.
@Post('register')
@UsePipes(new ValidationPipe())
async register(@Body() user: User) {
this.authservice.registerUser(user)
}
}
is that better?
You tell me: does it work as you expect?
not really, when i send wrong info it just acts like it doesnt receive the response, i want it to at least return something
What is User here?
a DTO
Can you show it?
mhm
import { IsString, IsNotEmpty, MinLength} from 'class-validator';
export class User {
@IsString()
@IsNotEmpty()
readonly username: string;
@IsString()
@IsNotEmpty()
@MinLength(6) // Example: Minimum length of 6 characters for password
readonly password: string;
}
Hmm, so when you send wrong info you don't send a password or something?
Can you provide a minimum reproduction that shows this behavior?
minimum reproduction?
i see
i seemed to fix it
added ```ts
app.useGlobalPipes(
new ValidationPipe({
transform: true,
transformOptions: {
enableImplicitConversion: true,
},
whitelist: true,
})
);
to my main.\ts
Okay, so the validation pipe is now globally bound
Can you explain? There should only be one response
That's how class validator works
Isn't empty technically too short?
If you really want, you can look to remove the "too short" message inside a filter, but you won't really find an easier solution
technically yes
but i want to be more specific
Then donn't use the @IsNotEmpty()?