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....
For further actions, you may consider blocking this person and/or reporting abuse
You can go a step forward and use request context.
npmjs.com/package/@medibloc/nestjs...
Thanks for your tips, it helped me.
this is not working ValidationArguments does not hold REQUEST_CONTEXT
There was obviously typo in decorator composition class. Instead of
AddUseTo, there should beInjectUserTo. Please try if it works for you now.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.
yes i am using but its not working btw.I did exactly the way you said
even console logged the args but REQUEST_CONTEXT returns undefined
@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_requestContextto the related DTO file that I used in my controller and added it as an @IsOptional() decoratorIgnore "\" 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.
Thank you, @siavash_habil! This can be helpful as well.
github.com/AvantaR/nestjs-validati...
@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.
What a coincidence! Magic 🎉
Have you considered using a Symbol instead of a string for the
REQUEST_CONTEXTconstant?export const REQUEST_CONTEXT = Symbol('REQUEST_CONTEXT');In this case, you will not need to do this
omit(value, REQUEST_CONTEXT);this is awesome but not sure if i want to use this !! not a clean solution
Totally agree, but sometimes we need to do something dirty way 🤷♂️ Thanks for your feedback! 🙏
ValidationArguments does not hold REQUEST_CONTEXT when using nested validation object
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.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?
Hi!
No, I haven't tried to use it with GraphQL, sorry.
Hey! Thank you for a great article!
Thanks! Glad you like it! 🙏
It didn’t work if I request data with multipart/form-data. Is there any solution for form data request.
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