chrishoermann / zod-prisma-types

Generator creates zod types for your prisma models with advanced validation
Other
578 stars 43 forks source link

[BUG] Schema created with a forced `ZodType` which prevents using `ZodObject` methods #230

Closed lambertkevin closed 6 months ago

lambertkevin commented 6 months ago

Describe the bug Due to the type explicitly added to each schema as ZodType, it's impossible to use methods attached to a ZodObject like extends, merge which prevents extending those schemas, or strip and passthrough which prevents override the strict method hardcoded to those schemas.

Screenshots

Capture d’écran 2024-01-02 à 19 28 38

The solution proposed by multiple zod github issues:

Capture d’écran 2024-01-02 à 19 31 37

Package versions (please complete the following information):

Additional context satisfies is only available since TS 4.9 while zod requires 4.5+

ashe0047 commented 6 months ago

I am facing this issue as well as I need to extend the User zodobject to include an additional password2 field for validation but cant since the extend is not available. Hopefully this can be fixed soon!

chrishoermann commented 6 months ago

Hey @lambertkevin, @ashe0047, as stated in the docs under known issues and limitations:

The satisfies operator: As some people have pointed out the input and output schemas and some of the relation schemas are typed as z.ZodType. This is required by zod for recursice types to work properly as stated in the docs. This has the downside that some zod methods like .merge(), .omit(), etc. are not available on these types.

It would work for schemas that do not have relations but in my experience even the most simple database schemas have relations and this is where recursive types/schemas come into play.

for extending zodSchemas I'd suggest you use the generated modelSchemas. They do not have a z.ZodType annotation and therefore have all the object methods. It may be a bit more work to make them fit your needs but with the output of options like createOptionalDefaultValuesTypes, createRelationValuesTypes, createPartialTypes you can taylor your own custom schemas.

lambertkevin commented 6 months ago

Thanks for the answer @chrishoermann ! Sorry for missing that part in the doc, my google skills are not that good anymore 😞