How to get req.query req.params if we dont know the shape #831
-
I have an app where we want to accept a wide number of params and query and pull it all together as a object. I was wandering if i we could use Existing Middleware const options = {};
Object.keys(req.query).forEach(param => options[param] = req.query[param]);
Object.keys(req.params).forEach(param => options[param] = req.params[param]);
req.opts = options; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @fbritoferreira ,
Check out this example: https://github.com/RobinTail/express-zod-api/blob/master/example/endpoints/upload-avatar.ts#L18 Another advice could be making your z.object({
params: z.record(
z.string(), z.string(),
)
}) I hope this would help you |
Beta Was this translation helpful? Give feedback.
Hello @fbritoferreira ,
request
properties are combined and into a single object that is being validated against input schema, which has to bez.object
.However, you can easily enable unknown properties on that schema by utilizing
.passthrough()
method.Check out this example:
https://github.com/RobinTail/express-zod-api/blob/master/example/endpoints/upload-avatar.ts#L18
Another advice could be making your
z.record
schema a property inside the input schema object, like this:I hope this would help you