Reputation: 457
I have a DTO
export class UpdateUserRoleDTO {
@ApiProperty()
@IsNotEmpty()
readonly userId:number;
@ApiProperty()
@IsNotEmpty()
@IsNumber()
readonly roleId: number;
}
My controller looks like this
@UsePipes(new ValidationPipe())
@Post('/update')
async updateUser(@Body() updateUserDto: UpdateUserDTO): Promise<User> {
return await this.userService.updateUser(updateUserDto);
}
Whenever client sends a request with the following payload
payloadObj = {
userId : 1,
roleId : 1,
xyz : 'assddcds',
someotherkey : 'fsdvs'
}
It's hitting my service file .I want to avoid this make sure only parameter mentioned in DTO should be passed else it should give 400
Upvotes: 2
Views: 2542
Reputation: 3559
given your code I'd pass the whitelist option set to true to the ValidationPipe
you're instantiating, like so in your controller:
@UsePipes(new ValidationPipe({ whitelist: true }))
@Post('/update')
async updateUser(@Body() updateUserDto: UpdateUserDTO): Promise<User> {
return await this.userService.updateUser(updateUserDto);
}
This should do the work.
Let me know if it helps, otherwise don't hesitate to comment & share your findings ;)
Upvotes: 2