与DTO相比,Nest JS中的DTO接受更多参数

时间:2020-01-30 10:56:32

标签: nestjs

我有一个DTO

render() {
    var webgraph = require("@ustutt/grapheditor-webcomponent");
    GraphEditor.
    return (
        <network-graph classes="red blue" mode="layout" zoom="both">
            <style slot="style">
                svg {width:100%; height: 100%}
            </style>
            <svg slot="graph"></svg>
        </network-graph>
    )
}}

我的控制器看起来像这样


export class UpdateUserRoleDTO {
  @ApiProperty()
  @IsNotEmpty()
  readonly userId:number;

  @ApiProperty()
  @IsNotEmpty()
  @IsNumber()
  readonly roleId: number;
}

每当客户端发送具有以下有效负载的请求

@UsePipes(new ValidationPipe())
  @Post('/update')
  async updateUser(@Body() updateUserDto: UpdateUserDTO): Promise<User> {
    return await this.userService.updateUser(updateUserDto);
  }

它正在打我的服务文件。我要避免这种情况,请确保仅传递DTO中提到的参数,否则应给出400

1 个答案:

答案 0 :(得分:0)

鉴于您的代码,我会将设置为 true whitelist选项传递给您要实例化的ValidationPipe,就像在您的实例中一样控制器:

controller.ts

@UsePipes(new ValidationPipe({ whitelist: true }))
@Post('/update')
async updateUser(@Body() updateUserDto: UpdateUserDTO): Promise<User> {
  return await this.userService.updateUser(updateUserDto);
}

这应该可以完成工作。

让我知道是否有帮助,否则请随时评论和分享您的发现;)