Closed tlancina closed 6 months ago
Hi @tlancina ,
Thanks for bringing this up. Let me provide some clarifications about the generated zod schemas.
The main schemas (as you showed above AccountSchema
) are meant to represent the entity fetched with a prisma findXXX
call. Since with Prisma you can load entities partially using "select" and "include", I feel it makes sense to mark all fields optional. There are two variant zod schemas generated:
AccountCreateSchema
All non-optional ZModel fields that don't have default values are required. It's meant for validating a create payload.
AccountUpdateSchema
All fields are optional. The difference with the main schema is that for relations, this variant only contains foreign key fields, but not the relation model fields. It's meant for validating update payload.
It seems that when calling z.partial
on a zod object, "nullish" fields are wrapped with multiple layers of ZodOptional
typing. However, I didn't notice how this breaks things either at compile or at runtime.
Could you let me know about more specifics on the way it's broken to you? Thanks!
Hey thanks for the explanation. My gut reaction is that if Zod is for validating data going in to Prisma, then something like partial
should be opt in, but I don't feel particularly strongly about it (I can also equally call .required
).
At the end of the day this is a nitpick - I don't have a good use case and wasn't even using it to validate input, I was using it to access Enum values as literals for the UI using .shape
😅 (and then had to .unwrap
them).
Hey thanks for the explanation. My gut reaction is that if Zod is for validating data going in to Prisma, then something like
partial
should be opt in, but I don't feel particularly strongly about it (I can also equally call.required
).At the end of the day this is a nitpick - I don't have a good use case and wasn't even using it to validate input, I was using it to access Enum values as literals for the UI using
.shape
😅 (and then had to.unwrap
them).
Ok, I guess I haven't fully understood your scenario but it sounds like an interesting one 😄. Zod schemas are versatile and can be used for different things.
I agree with you that it's best let the user to call ".partial". Let me reopen this issue and think if a change in V2 should be made. Thanks!
Fixed in v2.0.0-beta.1
Hello! I guess it's more of a question: is there a reason for all model properties to be optional? I just upgraded to 1.5.0 from 1.2.0 and it broke a few types. I filed it as a bug since already optional fields are marked as optional again.
1.2.0:
1.5.0:
Environment (please complete the following information):
I haven't had a chance to dig into why (or bisect what version exactly this happened at), just posting in case it's immediately clear. Will dig in more when I get a chance.