Node.js 如何过滤掉多余的模型属性
我有这样一个DTO对象:Node.js 如何过滤掉多余的模型属性,node.js,typescript,nestjs,Node.js,Typescript,Nestjs,我有这样一个DTO对象: export class CreateProductDTO { readonly _id: number; readonly _name: string; readonly _price: number; } DTO用于我的post方法 @Post('users') async addUser(@Response() res, @Body(new ValidationPipe()) createUserDTO: CreateUserDTO) { a
export class CreateProductDTO {
readonly _id: number;
readonly _name: string;
readonly _price: number;
}
DTO用于我的post方法
@Post('users')
async addUser(@Response() res, @Body(new ValidationPipe()) createUserDTO: CreateUserDTO) {
await this.userService.addUser(createUserDTO).subscribe((users) => {
res.status(HttpStatus.OK).json(users);
});
}
当我发布json数据时,它将序列化为CreateProduceDTO对象
{
"_id":1,
"_name":"Lux",
"_age":19
}
但我发布了带有多余属性的json数据,它还序列化为CreateProduceDTO带有多余属性的ObjJet
{
"_id":1,
"_name":"Lux",
"_age":19,
"test":"abcv"
}
CreateUserDTO { _id: 1, _name: 'Lux', _age: 19, test: 'abcv' }
我试着用管子过滤,但我不知道。
谢谢大家。如果您只想删除多余的属性,可以像这样使用ValidationPipe:
new ValidationPipe({whitelist: true})
如果您希望在存在任何非白名单属性时引发错误:
new ValidationPipe({whitelist: true, forbidNonWhitelisted: true})
查看更多选项如果您只想删除多余的属性,您可以像这样使用ValidationPipe:
new ValidationPipe({whitelist: true})
如果您希望在存在任何非白名单属性时引发错误:
new ValidationPipe({whitelist: true, forbidNonWhitelisted: true})
查看更多选项