#ValidationPipe transform does not work when Query() uses dto?

6 messages · Page 1 of 1 (latest)

coarse osprey
#
async index(@Query() dto: AuthLogin) {
   return "somethins"
}

export class AuthLogin {
  @IsString()
  @ApiProperty({
    description: '支持的登陆类型',
    required: true,
    default: 'google-oauth2',
    type: 'string',
  })
  type = 'google-oauth2'

  @IsString()
  @ApiProperty({
    description: '回调到前端的地址',
    required: false,
  })
  callbackUrl: string

  @IsBoolean()
  @ApiProperty({
    description: 'UI 登陆方式',
    required: false,
    default: false,
    type: 'boolean',
  })
  prompt = false
}

app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
      transformOptions: {
        enableImplicitConversion: true,
      },
    }),
)

@IsBoolean does not convert values

https://github.com/nestjs/nest/blob/f6f8934713c9535d4299464804e86104a1e4275c/packages/common/pipes/validation.pipe.ts#L103

https://github.com/nestjs/nest/blob/f6f8934713c9535d4299464804e86104a1e4275c/packages/common/pipes/validation.pipe.ts#L164

does not seem to handle the case where the metatype is class.

GitHub

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications on top of TypeScript & JavaScript (ES6, ES7, ES8) 🚀 - nest/validation.pipe.ts...

minor basin
#

Query and URL params always come in as strings. You can set enableImplicitTransform to true and it should transform for you

coarse osprey
minor basin
#

Dang. Class-transformer just doesn't have the proper metadata then. You can force it with @Tranform(({ value }) => JSON.parse(value))

coarse osprey
minor basin
#

I know I've answered this on stack overflow before