#not understanding DTO

1 messages · Page 1 of 1 (latest)

rough juniper
#

How are you telling Nest to handle the formdata type? By default, Nest doesn't include a formdata parser, just applciation/json and x-www-url-formencoded

solar rose
#

sorry mb

rough juniper
#

Can you show the request you're making?

solar rose
#

Yep

#

in theory it should display error

rough juniper
#

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

solar rose
rough juniper
#

What can I clarify?

solar rose
#

Do I need a class transform pipe?

rough juniper
#

If you don't plan to use nest's ValidationPipe, then yes

solar rose
#

There's a nest validation pipe to transform into class?

#

I wasn't able to find one

rough juniper
#

First things first: do you plan to use the ValidationPipe from @nestjs/common?

rough juniper
#

Then why are you calling validate yourself?

solar rose
rough juniper
#

It would have, if the body was actually an instance of the DTO and not just a JSON object

solar rose
#

i see.

#
 @Post('register')
    @UsePipes(new ValidationPipe())
    async register(@Body() user: User) {        
        this.authservice.registerUser(user)

    }
}
#

is that better?

rough juniper
#

You tell me: does it work as you expect?

solar rose
#

not really, when i send wrong info it just acts like it doesnt receive the response, i want it to at least return something

rough juniper
#

What is User here?

solar rose
#

a DTO

rough juniper
#

Can you show it?

solar rose
#

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;


}
rough juniper
#

Hmm, so when you send wrong info you don't send a password or something?

solar rose
#

yeah

#

or a password under 6 characters

rough juniper
#

Can you provide a minimum reproduction that shows this behavior?

solar rose
#

minimum reproduction?

rough juniper
solar rose
#

i see

solar rose
#

i seemed to fix it

#

added ```ts
app.useGlobalPipes(
new ValidationPipe({
transform: true,
transformOptions: {
enableImplicitConversion: true,
},
whitelist: true,
})
);

to my main.\ts
rough juniper
#

Okay, so the validation pipe is now globally bound

solar rose
#

mhm

#

but im facing a new issue, it sends 2 messages

#

instead of 1

rough juniper
#

Can you explain? There should only be one response

rough juniper
solar rose
#

yeah but i wanted it to return either too short or empty

#

depends on the conditions

rough juniper
#

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

solar rose
#

but i want to be more specific

rough juniper
#

Then donn't use the @IsNotEmpty()?