Skip to content

[@hono/zod-openapi] Json body validation stops working when bundling with esbuild #1463

@eric-poitras

Description

@eric-poitras

Which middleware has the bug?

@hono/zod-openapi

What version of the middleware?

0.19.6

What version of Hono are you using?

4.7.8

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

NodeJs 22 + esbuild 0.25.5

What steps can reproduce the bug?

1 - Build a project that uses validation on the Body
2 - Run it using a tsx and everything works correctly.
3 - Bundle it using esbuild -> everything almost works but the json validator no longer register.

esbuild --sourcemap --sources-content=false --bundle --outfile=./dist/index.js --platform=node --target=node22 ./src/index.ts

What is the expected behavior?

Bundled version should works as the unbundled version.

What do you see instead?

Bundled version does not works as the validator is not registered. The req.valid('json') return undefined and the code
explode on some following line.

Additional information

Instanceof operator here fails under esbuild as the prototype chain seems to be the broken:

https://github.com/honojs/middleware/blob/main/packages/zod-openapi/src/index.ts#L509

I previously saw that when you have 2 versions of the lib loaded in the same repository but I checked and it's not the case. It seems as far as I see a side effect of the bundling and the way it emulates CommonJS.

Libraries usually use type guards instead of instanceof operator validate type. This usually provide a more robust solution. I will take a look and provide a PR if needed.

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