ZodError gets empty path
ivan-kleshnin opened this issue · 1 comments
Setup
const searchLocations = makeEndpoint({
method: "get",
path: "/locations",
response: z.array(z_location), // validation rules don't matter here
parameters: [{
name: "search", // !!!
type: "Query",
schema: z.string().min(1),
}],
errors: z_defaultErrors,
})
export default makeApi([
searchLocations,
])
GET http://localhost:3001/locations?search=
400 Bad Request
👌
{
"context": "query.search",
"error": [
{
"code": "too_small",
"minimum": 1,
"type": "string",
"inclusive": true,
"exact": false,
"message": "String must contain at least 1 character(s)",
"path": [] -- 😨
}
]
}
Explanation
Without a clear reason error[0].path
is empty and path data is reexposed at context
. Might be changed at #76.
I expected path
value to be ["query", "search"]
. Libraries that parse Zod errors (like https://github.com/causaly/zod-validation-error and others) obviously rely on path
so I have to manually split context
value and put it back to path
...
I'm not sure if it's a bug or something intentional, please clarify 🙏
Hello it's not a bug. query search is not an object, that's why zodios adds some context information that is separate.
if you want zod to have a path for other error parsing tools, you can add a refine in your schema.
cf zod documentation about zod custom path :
https://zod.dev/?id=customize-error-path