middleware
middleware copied to clipboard
zValidator causes performance spikes in development
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?
- Create a Turborepo monorepo from scratch
- add hono & zod setup
- Create schemas in
/packages/types - Import schemas and
zValidatorin hono app (still no performance issues) - Call
zValidatorwith 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
After, actually calling the zValidator with
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
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.
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.
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:
tscfreezes - Version 0.7.2:
tscfreezes
Hi @mvanroon Can share a minimal project to reproduce it?
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.
@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
@mvanroon Thanks!
@ozhanefemeral
Ah, it's a trouble thing. The versions mismatch causes the type error.
We are still on zod 3. That might be the issue
@hono/zod-validator >= 0.7.1 do not work with zod 3.
@mvanroon
What is the Zod version? This should be ^3.25.
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