zod-prisma-types icon indicating copy to clipboard operation
zod-prisma-types copied to clipboard

Cant omit on 'withRelationsSchema'

Open gregg-cbs opened this issue 2 years ago • 3 comments

I cant omit fields on WithRelationsSchemas because they are z.ZodType .

export const ProductWithRelationsSchema: z.ZodType<ProductWithRelations> = ProductSchema.merge(z.object({
  suppliers: z.lazy(() => SupplierPSchema).array(),
  pricing: z.lazy(() => PricingPSchema),
}))

Ideally I would like to create an Input by doing this:

const ProductInputSchema = ProductWithRelationsSchema.omit({
  id: true, slug: true, permalink: true
})

I enabled and tried createInputTypes = true but that doesnt give the models/schemas that I want. Do you have a suggestion for this?

gregg-cbs avatar Jul 27 '23 12:07 gregg-cbs

Would love to know how to omit relation fields as well =/

renatoaraujoc avatar Jul 31 '23 18:07 renatoaraujoc

I have now decided to use this generator to create schemas which I then copy into my own folder of files and modify and then I import my schema and types from there - i exclude the generated folder from git.

This seems to be the only way to achieve this. The generator is still useful in that it does the bulk of the work. The downside to my approach is that you have to manually update your schemas when your prisma schema changes - which isnt the end of the world.

gregg-cbs avatar Aug 01 '23 15:08 gregg-cbs

To conclude: for normal types this can be implemented but for recursive types the .pick, .omit, etc. methods will never be possible 😞

the explanation for why is in this comment https://github.com/chrishoermann/zod-prisma-types/issues/102#issuecomment-1466197672

noqcks avatar Dec 24 '24 23:12 noqcks