DEV Community

Cover image for Injecting request object to a custom validation class in NestJS

Injecting request object to a custom validation class in NestJS

Krzysztof Szala on May 12, 2021

I'm a big fan of how NestJS handle validation using class-validator library. There are many advantages of using an external library for validation....
Collapse
 
walidgerges profile image
Walid-Gerges •

You can go a step forward and use request context.

npmjs.com/package/@medibloc/nestjs...

Thanks for your tips, it helped me.

Collapse
 
monirul017 profile image
Monirul Islam •

this is not working ValidationArguments does not hold REQUEST_CONTEXT

Collapse
 
avantar profile image
Krzysztof Szala •

There was obviously typo in decorator composition class. Instead of AddUseTo, there should be InjectUserTo. Please try if it works for you now.

Collapse
 
avantar profile image
Krzysztof Szala • • Edited

Are you sure you created interceptor, which injects the REQUEST_CONTEXT? I'm using similar code in my production-ready app and it works for me.

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.

Collapse
 
ivan_brezhniev_4728c6e96d profile image
Ivan Brezhniev • • Edited

Have you considered using a Symbol instead of a string for the REQUEST_CONTEXT constant?

export const REQUEST_CONTEXT = Symbol('REQUEST_CONTEXT');

In this case, you will not need to do this omit(value, REQUEST_CONTEXT);

Collapse
 
tkssharma profile image
tkssharma •

this is awesome but not sure if i want to use this !! not a clean solution

Collapse
 
avantar profile image
Krzysztof Szala •

Totally agree, but sometimes we need to do something dirty way 🤷‍♂️ Thanks for your feedback! 🙏

Collapse
 
wahyubaskara profile image
Wahyu Baskara •

ValidationArguments does not hold REQUEST_CONTEXT when using nested validation object

Collapse
 
dolphine_678a2ce7223 profile image
Dolphine Dev • • Edited

When using @ValidateNested(), the args?.object[REQUEST_CONTEXT] is undefined because the context passed to the nested validation is not the same as the one at the top level.

Collapse
 
toetet_aungmyint_05027d profile image
Xjsny •

It didn’t work if I request data with multipart/form-data. Is there any solution for form data request.

Collapse
 
tidi profile image
htdai •

stackoverflow.com/a/75139788

Maybe it's late but I found the solution for inject current user into body when using form-data
I have tested and it works perfectly for me

Collapse
 
davidn96 profile image
davidN96 •

Hey! Thank you for a great article!

Collapse
 
avantar profile image
Krzysztof Szala •

Thanks! Glad you like it! 🙏

Collapse
 
ssukienn profile image
Szymon Sukiennik •

Did you ever managed to port a similar approach to graphql in Nest? Namely, to have access to part or even whole gql context inside validator or validation rule?

Collapse
 
avantar profile image
Krzysztof Szala •

Hi!

No, I haven't tried to use it with GraphQL, sorry.