[zod-openapi] Absent optional body: validate it as {} (or type it as optional) instead of passing an unvalidated {} · Issue #2188 · honojs/middleware · GitHub
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[zod-openapi] Absent optional body: validate it as {} (or type it as optional) instead of passing an unvalidated {} #2188
The README says that with request.body.required unset, "a request with no body reaches the handler and c.req.valid('json') is {}". That {} is injected without running the schema, but it's typed as the schema's output, so an absent body and an explicit {} behave differently:
With z.object({ name: z.string() }), an absent body reaches the handler as {} typed { name: string }, while an explicit {} is a 400.
Proposal: when an optional body is absent, either run the schema on {}, so defaults apply and errors are reported, or pass undefined and type c.req.valid('json') as T | undefined. Either way, the type matches what the handler actually gets.
Workaround we use: route middleware that, when there's no body and no Content-Type, rewrites the request to content-type: application/json with body {}, so the validator runs.
Related: #891 and #2087 (the 415 for an undeclared content type).
Activity
cqr commentedon Oct 19, 2026
FYI I am happy to build this, but I don't want to pile on and it would help to know which approach the maintainers would prefer