Whimsy - hey is there a way to tell zod what ki...
hey is there a way to tell zod what kind of schema shape i want from type, instead of opposite infering type from schema?
15 Replies
yes, but you'll run into issues trying to use any of the advanced features of Zod.
ah
what kind of issues?
ZodType
doesn't have ZodObject
methods on it like pick
, extend
, etc.
actually, satisfies is a little better here:
It won't keep you from doing something non-sensical like having extra properties (since that would also satisfy the type given how structural typing works
– this works still.
In general, I'd suggest you use Zod as the source of truth for the types, and if you're trying to make a schema for some type you do not control (like some third party API schema or something), I'd take a different approach still and only define the schema for the bits you actually use.well i use prisma, so that's my source of truth, its just that there are no good validators for it, and then dont provide one themselves so
might be worth looking into https://github.com/CarterGrimmeisen/zod-prisma
GitHub
GitHub - CarterGrimmeisen/zod-prisma: A custom prisma generator tha...
A custom prisma generator that creates Zod schemas from your Prisma model. - CarterGrimmeisen/zod-prisma
very outdated
meaning it doesn't work?
There are a few other prisma-to-zod generators listed here: https://zod.dev/?id=x-to-zod
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
yeah i already went around whole internet, there's no working one up to date
i searched for days
idk if i can to json and then from json to schema
What does up-to-date mean? Is there some new feature you're using that those codegen tools are not supporting? Maybe we can help get one of them working for your use case?
i mean some i used to get prisma to zod either dont work with latest versions, and i dont want to be on older ones, or either dont support some type of nested relations or such
gotcha. well, I think you have a few options:
1. Use the
satisfies z.ZodType<T>
thing and just be careful not to accidentally put too many properties in your schema.
2. Work with one of those codegen projects to get them up to date.
3. Use Zod for everything except your database models (which should be pretty reliable coming from a relational database via prisma).
If it was me, I'd probably lean toward the third option, which is what I've done in the past, but you might have different values than me.well yeah probably, its just annoying because if i want to allow a user to edit some data via api, and they pass an object, prisma wouldn't accept that, since relations, and then i have to do
connect
or create
for eachyeah, this is why I actually hand-write schemas rather than codegen for the most part: I want control over what the actual object is when it parses, and there are just enough differences between how data arrives in and out of various systems that it's not always feasible to have a general solution. I personally embrace this rather than try to work around it.
i mean same, i ended up using
satisfies z.ZodType<T>