#ValidationPipe mishandled input data

51 messages · Page 1 of 1 (latest)

rancid mulch
#

When using ValidationPipe to turn on the transformer option, the Query annotation will produce wrong behavior

app.useGlobalPipes(
    new ValidationPipe({
      transform: false,
      whitelist: true,
      forbidNonWhitelisted: true,
    }),
  );
// controller
   @Get("role")
  getUserRole(@Query("uid") uid: number) {
    return this.userService.userIsExitsts(uid).then((exists) => {
      if (!exists) {
        throw new NotFoundException(`can not found this user ${uid}`);
      }
      return this.userService.getUserInforByUID(uid).then((userinfor) => {
        return userinfor.role;
      });
    });
  }

if transformis true, then uid will be NaN.

#
"dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/mapped-types": "^1.2.0",
    "@nestjs/passport": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/platform-fastify": "^9.2.1",
    "@nestjs/swagger": "^6.1.4",
    "@nestjs/throttler": "^3.1.0",
    "@nestjs/typeorm": "^9.0.1",
    "class-transformer": "^0.5.1"
}
copper dagger
#

Don't use @Query('parameter ') of you want to do validation. You should make a DTO for the query parameter(s)

rancid mulch
#

maybe should use @Post?

copper dagger
#

Why?

rancid mulch
#

you say dto

copper dagger
#

Yes, as in a class that defines what you expect. You expect certain values, certain data, to be sent via the query parameters, correct? That data is transferred and this class is the object that represents it. Just because it's a DTO doesn't mean it has to be used with only posts

rancid mulch
#

ok, i will be try

#

like this?

#

still has wrong

#
[Nest] 17612  - 2023/01/06 11:49:07   ERROR [ExceptionsHandler] ER_BAD_FIELD_ERROR: Unknown column 'NaN' in 'where clause'
copper dagger
#

You still need to decorate it with the proper decisions

#

Also, wouldn't a uid be a string?

rancid mulch
#

this user entity,I expect their types to be consistent

copper dagger
#

Ah, okay. Normally uids are not numbers in my experience

rancid mulch
#

👀

copper dagger
#

Okay. So add the @IsNumber() decorator to the dto

rancid mulch
#

I did it before you said it

#

but useless

copper dagger
#

Then validation isn't happening, and that's a problem. As this is from a query, you may need @Type(() => Number)

rancid mulch
#

Which pack is the type decorator in?

copper dagger
#

Class-transformer

rancid mulch
#

same result

#

500 error

copper dagger
#

Can you provide a reproduction then? This should be working

rancid mulch
copper dagger
#

A git repo please

rancid mulch
#

is private

copper dagger
#

Make a reproduction that can be public then?

rancid mulch
stark crest
# rancid mulch

have you tried it with both @IsNumber() and @Type(() => Number)?

rancid mulch
stark crest
#

one is for validation, the other is for parsing (kinda)

stark crest
# rancid mulch like this?

can you add console.log(user) in the first line of the getUserRole and show us what did you get?
I want to see if that has nothing to do with the validation pipe

#

oh nvm

#

the issue is that you're not invoking that route

#

@Get(":uid") declaration should appear after @Get("role"), otherwise you'll always hit the GET /:uid whereas uid will be the string "role"

rancid mulch
stark crest
rancid mulch
#

I added an infor path and it works now

#

I never thought the problem could be solved so easily