New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent Handling of Empty JSON Request Body in Hono Validator #2651
Comments
Hello @ilshm, can you send the code to see how did you setup the middleware, and how are you making the request? I just reproduce the steps to see the bug, but the request is successful, I have no error. |
Hello again, something like this, as I remember: const route = app.post(
'/posts',
zValidator(
'json',
z.object({
title: z.string().optional(),
})
),
(c) => {
const title = c.req.valid("json");
// ... use your validated data
}
) Request was made from cURL and Thunder Client. Hope this helps! |
I thought you were using the This is how I made the request with curl: curl -X POST "http://localhost:8787/posts" \
-H "Content-Type: application/json" \
-d "{}" |
I made it without any JSON in body, and without any headers, like so:
I get To circumvent this issue, I've adjusted the middleware ( validator.ts
|
Oh, I didn't know that I don't think the behavior should be changed in |
What version of Hono are you using?
4.3.3
What runtime/platform is your app running on?
Bun
What steps can reproduce the bug?
What is the expected behavior?
The hono/validator middleware should gracefully handle empty JSON request bodies, considering them valid without triggering exceptions. This behavior is crucial for scenarios where empty JSON bodies are legitimate or expected.
What do you see instead?
The current behavior negatively affects applications where empty JSON request bodies should be permitted. This inconsistency leads to unexpected errors and hinders the module's usability.
Additional information
This issue is critical for applications relying on hono/validator. A swift resolution or workaround would greatly benefit the developer community.
The text was updated successfully, but these errors were encountered: