turkerdev / fastify-type-provider-zod

MIT License
413 stars 25 forks source link

`querystring` needs to restrict to `Record<string, string>` #95

Open BriungRi opened 4 months ago

BriungRi commented 4 months ago

Currently, querystring can accept something like

z.object({
  name: z.string(),
  age: z.number(),
});

which will always fail the validation because all parameters from querystring are string type.

A few ideas:

  1. For querystring only, if a number is expected, parse the string into a number and then do zod validation
  2. Add (static) typing that enforces querystring type
  3. Add (dynamic) type check that enforces querystring type by throwing an error for invalid type
kfiroo commented 3 months ago

Maybe you can try coerce (https://zod.dev/?id=coercion-for-primitives)

z.object({
  name: z.string(),
  age: z.coerce.number(),
});
jsnimda commented 2 months ago

Because I am very desired to use z.number(), I created a plugin because of this, feel free to take a look: fastify-zod-query-coercion