gpt4 book ai didi

nestjs - @Query() 不会转换为 DTO

转载 作者:行者123 更新时间:2023-12-03 15:21:35 25 4
gpt4 key购买 nike

我有一个 Controller 需要接收请求查询字符串中的数据(我不能使用正文,因为我正在与遗留系统交互)。

我将 DTO 映射查询参数写入对象,并使用 ValidationPipe 验证数据并将其转换为我的 DTO。

所以,我有这个:

import { Get, Controller, Query, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common';

class TestDto {
@IsNumber()
field1: number;
@IsBoolean()
field2: boolean;
}

@Controller()
export class AppController {
constructor() {}

@Get()
@UsePipes(new ValidationPipe({ whitelist: false, transform: true}))
root(@Query() dto: TestDto): TestDto {
return dto;
}

}

之前的所有代码都编译并遵循 NestJS 文档,但是当我调用 http://localhost:3000/?field1=15&field2=true 时我明白了:
{
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"field1": "15",
"field2": "true"
},
"value": "15",
"property": "field1",
"children": [],
"constraints": {
"isNumber": "field1 must be a number"
}
},
{
"target": {
"field1": "15",
"field2": "true"
},
"value": "true",
"property": "field2",
"children": [],
"constraints": {
"isBoolean": "field2 must be a boolean value"
}
}
]
}

根据属性,这两个字段都有效,但管道拒绝请求。如果我从 @IsNumber 更改为 @IsNumberString 并从 @IsBoolean 更改为 @IsBooleanString 它会验证,但我没有收到转换后的数据(即我得到一个普通对象而不是我的 DTO)

有人遇到过这样的事情吗?

最佳答案

它不能,因为接口(interface)只能塑造你的结构或告诉一些关于类型的东西。由于同样的事实,您的验证将无法正常工作。

class TestDto


NestJS docs - Auto Validation
NestJS docs - Payload transforming

正如文档中的示例所说:
import { IsEmail, IsNotEmpty } from 'class-validator'; // 'class'

export class CreateUserDto { // notice class
@IsEmail()
email: string;

@IsNotEmpty()
password: string;
}

更新 #1 - 告诉验证器尝试进行隐式转换
@UsePipes( new ValidationPipe( { transform: true, transformOptions: {enableImplicitConversion: true} }))

更新 #2 - 使用自定义 @Query()参数装饰器
import { Controller, createParamDecorator, Get, UsePipes, ValidationPipe } from '@nestjs/common';
import { IsNumber } from 'class-validator';
import { AppService } from './app.service';

const MyField = createParamDecorator((data, req) => {
const result = new TestDto();
result.field1 = Number(req.query.field1);
return result;
});

class TestDto {
@IsNumber()
field1: number;
}

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {
}

@Get()
@UsePipes(new ValidationPipe({ transform: true }))
getHello(@MyField() testDto: TestDto): TestDto {
return testDto;
}
}

关于nestjs - @Query() 不会转换为 DTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58377492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com