WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

[@hono/zod-validator] When making a request without a Content-Type Header and Request Body, the zod schema is z.object.partial, and the zValidator will pass validation #1468

@shinexoh

Description

@shinexoh

Which middleware has the bug?

zod-validator

What version of the middleware?

0.7.3

What version of Hono are you using?

4.9.8

What runtime/platform is your app running on? (with version if possible)

Bun 1.2.22

What steps can reproduce the bug?

When I use zValidator to validate a JSON request body and set the schema to z.object({ name: z.string() }).partial(), the validation by zValidator is completely bypassed if the request is made without the Content-Type: application/json header and without a request body.

Ideally, zValidator should validate the request body. Even if the request body is an empty object {}, it should still comply with the validation rules of this schema.

This is the reproduction code.

const schema = z.object({ name: z.string() }).partial();
router.post('/test', zValidator('json', schema), (c) => {
  const body = c.req.valid('json');
  return c.text('ok');
});
// ok

I am using zod 4.1.11. I am not entirely sure if this is a bug or a feature, but it does not quite meet my expectations.

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions