zod-express-middleware icon indicating copy to clipboard operation
zod-express-middleware copied to clipboard

Better typesafety with request methods and url parameters

Open neolectron opened this issue 2 years ago • 0 comments

Hello, I'm facing a situation where I can do this :

const mySchema = z.object({
  name: z.string(),
});

router.get('/', validateRequest({ body: mySchema }), /**/)

Whereas it shouldn't be possible because we already know it's a GET request at this point, and they do not have any body.

Similar issue with :

const mySchema = z.object({
  name: z.string(),
});

router.get('/', validateRequest({ params: mySchema }), /**/)

Whereas we know we do not have any params in the url. Another example is we could (should?) enforce validation of params when we see there's one in the url.

I think there's room for improvement, and I would like to contribute to it, what do you think ?

neolectron avatar Jan 24 '23 19:01 neolectron