Z
Zod3mo ago
Whimsy

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?
No description
15 Replies
Scott Trinh
Scott Trinh3mo ago
yes, but you'll run into issues trying to use any of the advanced features of Zod.
const test: z.ZodType<SomeType> = z.object({
field: z.string(),
});
const test: z.ZodType<SomeType> = z.object({
field: z.string(),
});
Whimsy
WhimsyOP3mo ago
ah what kind of issues?
Scott Trinh
Scott Trinh3mo ago
ZodType doesn't have ZodObject methods on it like pick, extend, etc. actually, satisfies is a little better here:
const test = z.object({
field: z.string(),
}) satisfies z.ZodType<SomeType>;
const test = z.object({
field: z.string(),
}) satisfies z.ZodType<SomeType>;
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
const test = z.object({
field: z.string(),
field2: z.number(),
}) satisfies z.ZodType<SomeType>;
const test = z.object({
field: z.string(),
field2: z.number(),
}) satisfies z.ZodType<SomeType>;
– 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.
Whimsy
WhimsyOP3mo ago
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
Scott Trinh
Scott Trinh3mo ago
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
Whimsy
WhimsyOP3mo ago
very outdated
Scott Trinh
Scott Trinh3mo ago
meaning it doesn't work?
Scott Trinh
Scott Trinh3mo ago
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
Whimsy
WhimsyOP3mo ago
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
Scott Trinh
Scott Trinh3mo ago
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?
Whimsy
WhimsyOP3mo ago
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
Scott Trinh
Scott Trinh3mo ago
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.
Whimsy
WhimsyOP3mo ago
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 each
Scott Trinh
Scott Trinh3mo ago
yeah, 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.
Whimsy
WhimsyOP3mo ago
i mean same, i ended up using satisfies z.ZodType<T>
Want results from more Discord servers?
Add your server