Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access request object within validation pipe

Tags:

nestjs

I am trying to access to Request object from within a Validation Pipe in nestjs

In order to verify uniqueness of certain fields, I require the ID/UUID parameters supplied with PUT/PATCH request (not available in the data structure itself)

any idea?

like image 397
gal Avatar asked Jul 31 '26 07:07

gal


1 Answers

Currently, it is not possible to access the request object at all in a pipe. If you need the request you can use a guard or an interceptor.

If you are working on verifying uniqueness, that sounds like a part of business logic more than just about anything else, so I would put it in a service and handle the query to the database there. Just my two cents.

Edit 11/17/2020

After learning way more about how the framework works as a whole, technically it is possible to get the entire request object in a pipe. There are two ways to go about it.

  1. Make the pipe @Injectable({ scope: Scope.REQUEST }) so that it is request scoped. This will end up creating a new pipe on each request, but hey, if that's your cup of tea then great.

  2. Make a custom parameter decorator as custom decorators get completely passed into pipes as they are. Do note, that this could impact how the ValidationPipe is functioning if that is bound globally, at the class, or method level.

like image 103
Jay McDoniel Avatar answered Aug 07 '26 04:08

Jay McDoniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!