Closed foxgem closed 1 year ago
querystrings only pass strings, because there is no way for a client to tell if something is a number or a boolean. what you are looking for is coercing, to convert this string into a number. See https://zod.dev/?id=coercion-for-primitives
@kibertoad thanks for quick response!
According to this link: https://webapplicationconsultant.com/javascript/how-to-parse-boolean-or-numeric-values-in-fastify-query-string/ , it looks like fastify schema can convert the type automatically?
if you are using zod, fastify conversion, which relies on ajv, is not going to work, you need to use zod mechanisms instead
thanks, I will close this, :).
@foxgem I created a plugin because of this, so that you can use z.boolean() or z.number() on querystring, feel free to take a look :D https://www.npmjs.com/package/fastify-zod-query-coercion
Hi, here is the schema I defined
However, it always complained 400 for
curl "http://localhost:3006/echo?id=123"
But, when I changed
id
inquerystring
toz.string()
, everything is fine. Could you point out what's wrong?Thanks