Closed crummy closed 7 months ago
In your API handler you would access the query parameters from the request object which is an abstraction on top of NextRequest
(App Router) and NextApiRequest
(Page Router) objects. I believe these objects do not by default support providing the query parameters as non-string values and this would essentially break the way those objects are supposed to work with Next.js. Instead, I would narrow the BaseQuery
type to allow only string values for the query parameters for now to better document how the query schema is meant to be defined.
This leads me to another observation that the usage of Zod transforms in general is problematic as neither the query parameters nor the request body are parsed values using the Zod schema but they are untouched from the original request, so they are only validated, not parsed. However, I see the benefit of using Zod transforms so this needs to be further investigated if we could pass through the actual parsed values instead of just validating the request bodies and query parameters. For now I suggest doing the string transformation in your handler function.
Hey, v6.0.0-beta.1
is now released that should address your issue. Zod transforms are now supported as the Zod-parsed results from the query parameters, search parameters and request bodies are now injected into a cloned request object, passed into the API handler.
I still hit this on v6.0.0.
I see the parsed type injected into the hander, however I still get the type error described in the original description above.
I have an API endpoint that takes some numbers as inputs. I know they are handled as strings, but I would like to use Zod to transform them to numbers for easier handling later.
This works:
However, if I try to transform it I get an error:
Here's the error:
I understand why
query
is typed asRecord<string, string | string[]>
as that's what they come in as - but is there no way to transform this input to a different type?