DEV Community

Discussion on: Injecting request object to a custom validation class in NestJS

Collapse
 
monirul017 profile image
Monirul Islam

yes i am using but its not working btw.I did exactly the way you said

Thread Thread
 
monirul017 profile image
Monirul Islam

even console logged the args but REQUEST_CONTEXT returns undefined

Thread Thread
 
siavash_habil profile image
Siavash Habil

@monirul017 @avantar
I had exactly same problem and after investigating it is happening because of {whitelist: true} of ValidationPipe and when you set it to false it will working properly but I didn't continue in this way because I want to whitelist the properties so temporary I added _requestContext to the related DTO file that I used in my controller and added it as an @IsOptional() decorator

import { REQUEST_CONTEXT } from '../../../interceptors/inject-request-param.interceptor';


  @IsOptional()
  \[REQUEST_CONTEXT\]: any;
Enter fullscreen mode Exit fullscreen mode

Ignore "\" from the above code.

For others trying to use this article as a solution continue using from attached repository.

At the end thank you @avantar for your solution.

Thread Thread
 
avantar profile image
Krzysztof Szala

Thank you, @siavash_habil! This can be helpful as well.

github.com/AvantaR/nestjs-validati...

Thread Thread
 
siavash_habil profile image
Siavash Habil

@avantar This is almost happened at the same time for both of us because I solved it about 20 hours ago. :)
Thank you for sharing.

Thread Thread
 
avantar profile image
Krzysztof Szala

What a coincidence! Magic 🎉