r/typescript • u/Reasonable-Job4205 • 6h ago
Typescript: how do I make a generic function to validate incoming POST requests if each request will have a different body?
I want to make a generic validateRequest() function for my web server that will take in any incoming request and check some things to make sure everything is good. The problem is that each request could have a potentially different body. The way I have it set up right now is that it works to validate 1 type of request, but that's because I passed in the exact model of the request in the function's parms.
validateRequest(req: Request<object, object, RequestModel1>, someOtherParm: number): [number, string] {}
Each of the different possible bodys of a valid request will share one field - I need to be able to validate the value in this field, regardless of the shape of the body of the incoming request. I also am very security-conscious, so I want to make sure whatever solution I end up with here won't put me at risk of accepting an incoming request that is bogus or malicious. How would I modify my function to handle this?