middleware icon indicating copy to clipboard operation
middleware copied to clipboard

zValidator causes performance spikes in development

Open ozhanefemeral opened this issue 4 months ago • 11 comments

Which middleware has the bug?

@hono/zod-validator

What version of the middleware?

0.7.2

What version of Hono are you using?

4.8.5

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

Bun + Turborepo

What steps can reproduce the bug?

  1. Create a Turborepo monorepo from scratch
  2. add hono & zod setup
  3. Create schemas in /packages/types
  4. Import schemas and zValidator in hono app (still no performance issues)
  5. Call zValidator with imported schema (this step causes performance spikes)

What is the expected behavior?

zValidator should be working without performance issues when importing schemas from monorepo.

What do you see instead?

Before, no performance issues until we actually call zValidator with importec schema Image

After, actually calling the zValidator with Image

Additional information

The schemas I've been using

export const LoginRequestSchema = z.object({
  email: z.string().email("Invalid email format"),
  password: z.string().min(6, "Password must be at least 6 characters"),
});

// Register request schema
export const RegisterRequestSchema = z.object({
  email: z.string().email("Invalid email format"),
  password: z
    .string()
    .min(6, "Password must be at least 6 characters")
    .regex(/\d/, "Password must contain at least one number")
    .regex(/[a-z]/, "Password must contain at least one lowercase letter")
    .regex(/[A-Z]/, "Password must contain at least one uppercase letter"),
  name: z.string().min(2, "Name must be at least 2 characters"),
});

ozhanefemeral avatar Jul 31 '25 14:07 ozhanefemeral

@ozhanefemeral

Is that the only matter in a monorepo? I'm not very familiar with Turborepo, so if you can provide a minimal project, I may be able to help you.

Anyway, the performance of type inferences is a theme we have to improve.

yusukebe avatar Aug 04 '25 07:08 yusukebe

We're seeing the same thing. Typescript isn't compiling any more and is giving a out-of-memory warning.

Also seeing this error in my IDE:

Type instantiation is excessively deep and possibly infinite.

Image

It popped up recently, so I will try downgrading to a previous version to see if that fixes things.

Update: It was introduced in version 0.7.1 by this pull request: https://github.com/honojs/middleware/pull/1302

  • Version 0.6.0: no issues
  • Version 0.7.0: no issues
  • Version 0.7.1: tsc freezes
  • Version 0.7.2: tsc freezes

mvanroon avatar Aug 06 '25 09:08 mvanroon

Hi @mvanroon Can share a minimal project to reproduce it?

yusukebe avatar Aug 06 '25 11:08 yusukebe

I tried reproducing it with a blank project, but the issue is not occurring in that project. I hope you're still able to catch the issue.

mvanroon avatar Aug 06 '25 12:08 mvanroon

@yusukebe I realized the installed zod version was ^3.22.4 in packages/types while it was ^4.0.5 in apps/api 🤦‍♂️

having the same version (^4.0.5) between the monorepo packages and apps has fixed the issue for me

ozhanefemeral avatar Aug 06 '25 21:08 ozhanefemeral

@mvanroon Thanks!

yusukebe avatar Aug 06 '25 21:08 yusukebe

@ozhanefemeral

Ah, it's a trouble thing. The versions mismatch causes the type error.

yusukebe avatar Aug 06 '25 21:08 yusukebe

We are still on zod 3. That might be the issue

@hono/zod-validator >= 0.7.1 do not work with zod 3.

mvanroon avatar Aug 07 '25 08:08 mvanroon

@mvanroon

What is the Zod version? This should be ^3.25.

yusukebe avatar Aug 07 '25 08:08 yusukebe

@mvanroon

What is the Zod version? This should be ^3.25.

[email protected]

mvanroon avatar Aug 07 '25 08:08 mvanroon

Without knowing much about this particular issue, it seems likely that the "Excessively deep" issue is possibly mitigated in Zod 4.1.6: https://github.com/colinhacks/zod/pull/5222.

If you need Zod 3, you can fix this issue by updating your tsconfig to use a modern module/moduleResolution setting instead of the legacy "node": https://x.com/colinhacks/status/1966261915115782654

colinhacks avatar Sep 11 '25 23:09 colinhacks