honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers
https://hono.dev
436 stars 154 forks source link

How to Convert TypeScript Interfaces to Zod Schemas for Use in zValidator Middleware? #772

Open chengfengfengwang opened 1 week ago

chengfengfengwang commented 1 week ago

How can I use zValidator with TypeScript? zValidator is very useful, but I want to reuse the TypeScript interfaces from the frontend. Some interfaces are complex and it's hard to write Zod schemas from scratch. Is there a convenient way to transform TypeScript types into Zod schemas so that they can be used in the zValidator middleware?

shlroland commented 1 week ago

How can I use zValidator with TypeScript? zValidator is very useful, but I want to reuse the TypeScript interfaces from the frontend. Some interfaces are complex and it's hard to write Zod schemas from scratch. Is there a convenient way to transform TypeScript types into Zod schemas so that they can be used in the zValidator middleware?

zod is just a runtime library, and the interface of TypeScript has already been discarded at compile time. Therefore, it is impossible to use the interface to generate a zod schema.

Fortunately, the typia library can convert interface into validator, and hono also provides support typia-validator.

However, I am currently encountering some issues while trying to integrate typia into hono. You can check this issue #777 for details. I am very eager for these problems to be resolved, as I really enjoy the combination of typia and hono.