Drizzle-zod Include Relations
Hello,
I was wondering if
drizzle-zod
includes support for relations, and if so how I can include them. Thanks!9 Replies
So far, I do not think so. I have added them manually myself.
how were you manually adding them - are you typing out the complete zod definition or using drizzle-zod and appending onto the schema somehow?
I am interested in accomplishing this as well
I have a function which uses drizzle-zod then traverses the drizzle relations and uses zod's merge() function to expand the zod schema.
Can you share it?
Sure, though need to clean up slightly. I have some OpenAPI DTO annotation mixed in and it was a fiddle to get all going together.
@danielsharvey I'm also looking into doing something similar. Would you mind sharing your approach?
I did it like so if anyone needs it for future reference (cc @pudgereyem @shreddish )
You can also do it via extent, albeit with some wonky behaviours sometimes if you start adding in omits and what not
Sorry for slow reply. My approach is similar to @Liam 's mentioned above, I've just written a generic function that utilises
z.object()
and createInsertSchema()
based upon iterating (recursively) the Drizzle relations structures. I will share the code when I can but been swamped recently.