Use zod schema generated with prisma with @hono/zod-openapi
Is there a way to use already generated zod schema with @hono/zod-openapi ?
The Zod schemas generated by Prisma are not enhanced the way @hono/zod-openapi provides. Does anyone know a way to enhance them for use with createRoute from @hono/zod-openapi (request.params, json validator, ...)
42 Replies
you could try using
zod-openapi
as used withhono-openapi
: https://github.com/rhinobase/hono-openapi?tab=readme-ov-file#basic-usageresolver doesn t work, and i can't use import "zod-openapi/extend"; since code is already generated
I didn't find a solution ATM, I'm creating my own zod type and not using ones generated by prisma
when you say code is already generated, you're talking about the schemas, no?
oh, you're saying that the zod instance of the imported schemas won't be extended
yes, i m using zod-prisma-types
you could do something like this then
This way, it works ! Thx !!
(i m using import { z } from "@hono/zod-openapi"; directly)
(i still have a strange problem but i will deal with it)
use the commented GetPlanParamsSchema crash the openapi generation but i can deal with that
use the commented GetPlanParamsSchema crash the openapi generation but i can deal with that
Huh. Crash how? Whatβs the error message?
/doc return {} crashing the swagger ui
(only logs i have for this)
hm. can you add some backend/error handler logs?
what happens if you hit the openapi docs endpoint?
/doc return a status 500 with only {} in response

running out of docker doesn t reproduce the pb, strange behavior
If u really want to give a look, code is available her, readme is not up to date https://github.com/iNeoO/rplan/tree/feat/archi
GitHub
GitHub - iNeoO/rplan at feat/archi
Contribute to iNeoO/rplan development by creating an account on GitHub.
(request.params: InvitationTokenSchema in packages/backend/src/routes/invitation.route.ts is causing the pb)
what is a "pb"?
there aren't any obvious code/type issues
yeah that's really strange
and the fact the pb only happen in docker container :/
again, what is "pb"?
your repo is too complicated for me to spin up locally. i'm happy to try running a minimal example if you can share one
otherwise, i recommend adding some error logs to your backend. there's not much we can diagnose without them
so i'm using docker compose to cluster the application
my zod prisma types are generated in dockerfile
and imported in backed as package with pnpm monorepo way.
running the application direcylu with pnpm doesn't break the /doc
but thx for your help, even if it's not the perfect way, you provide me a way to partial solve the problem
for reference, you can add syntax highlighting to discord code snippets
it uses markdown syntax, so just add the language after the opening backticks: ```typescript
you really should add an error handler with an error log to your backend. that's an mvp requirement
i had a error catch handler but no more logs,
i think my error could be related to this https://github.com/honojs/middleware/issues/865
GitHub
Zod OpenAPI Hono - .catch make all the openapi doc resolve to an em...
Issue description When working with the middleware and adding a catch handler to a route's description, I observed that the OpenAPI documentation fails to generate properly and results in a 500...
using a catch block on a schema like that seems like an anti-pattern
but you could try switching to
hono-openapi
. its more flexible wrt implementationok i will give a try
thx for your time
Do you know if there is something special todo to handle app.route for openapi ?
meaning like a specific method, or some config you need to get routing to work?
(the way you use
.openapi
for the handlers)if i don t use app.get / post at the root of app, routes aren t visible by openApiSpecs
meaning that
exactly
have you tried adding a slash in front of the route name, e.g.,
/auth
instead of auth
i wouldn't expect it to work without the slash, even on vanilla hononow yes
but routes works but aren't showing in openapi swagger
what happens when you visit
/openapi
? do the docs get generated, or nahit s like i have no routes
if i create a route with app.get at root of the application, route is detected by openapi
curious
[Symbol(openapi)] is missing in my app.route routes
when you say root, do you mean
first case
are you using the openapi method on your schemas?
no just default z.object
if i copy the route working at root in a sub route she doesn t work
curious
like so?
not breaking but not interpreted
but that's what you meant by "route that works at root doesn't work in sub route"
hmmm
have you tried
app.openapi doesn t exist
i got it
it's because of my honoCreateApp, i need to handle openapi inside
using const app = new Hono(); instead of const app = createInternalApp(); works
ok strange behavior it's because i wasn't providing a basePath when creating app
huh
my recommendation would be to try and create an isolated + minimal example, and add complexity until it breaks
it looks like you're doing a fair bit of abstraction, which makes it sort of hard to debug, and is known to cause problems with how hono works (though that's mainly for typing)
if nothing else, it will make it easier to spot if/where you're departing from the
@hono/zod-openapi
docs/recommended approachyes i will try that, thx for your help !
do you have any repo to recommand ?
for a
@hono/zod-openapi
app?
no, i'm afraid not. i haven't worked much w it tbh
folks have definitely shared some in #showcase , and in various chats on this server
i would search the discord for @hono/zod-openapi