Open bowd opened 6 years ago
@bogdan-dumitru this is definitely interesting have you looked into implementing this already? Can we discuss it further?
@dcavanagh somehow this reply got lost in the weeds for me. Is this still of interest? I'll try to bang out a proof of concept, I have to reacquaint myself with the issue/solution a bit though it felt pretty straightforward if I remember correctly.
At the moment when defining a
httpMethod
in the controller themetadata
is pushed onto an array. This works fine for most cases but it provides two disadvantages in my opinion:httpMethod
one, and then we need to look inside the list for a matching key. The only problem with this is readability, I'd like thehttpMethod
decorator to be at the top personally.metadata
from a method parameter decorator, because these are evaluated before the method decorator and thus nometadata
exists at the time.Expected Behavior / Possible Solution
Maybe the controller method
metadata
should be an object indexed by key instead of an array (just like the method parameters metadata created by the parameter decorators). And more importantly thehttpMethod
decorator should merge with any existingmetadata
for that method.Current Behavior
Covered in the summary. Currently you need to jump through a few loopholes to extend the metadata when using method decorators, and it's virtually impossible from the method param decorators.
Context
What I'm trying to accomplish is to validate request payloads using
class-validator
. Request payloads can be either params, body or query. The way I'm validating is by unshifting a middleware that checks the payload against a class definition and replaces the payload on the request object with the class instance or executesnext
with the errors found.Here is some example code and usage:
This works but it feels like repetition. Theoretically I could only need the param injection decorator to also include the validation middleware, similarly to how NestJS does it.