chrishoermann / zod-prisma-types

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

Type error when using Omit a required filed with relational schema #187

Open palprabhat opened 10 months ago

palprabhat commented 10 months ago

Description of issue

I am getting type error when using omit feature on a schema that has relational fields.

Dependencies

"@prisma/client": "4.16.2" "zod": "3.21.1" "zod-prisma-type": "2.5.6" "typescript": "5.0.2"

Also tried with

"@prisma/client": "5.2.0" "zod": "3.21.1" "zod-prisma-type": "2.7.9" "typescript": "5.0.2"

Code

generator client {
    provider = "prisma-client-js"
}

generator zod {
    provider = "zod-prisma-types"
}

datasource db {
    provider     = "mysql"
    url          = env("DATABASE_URL")
    relationMode = "prisma"
}

model Tenant {
    tenantID        String   @id @unique(map: "Tenant_ID") @default(uuid())
    tenantName      String   @unique
    createdAt       DateTime @default(now())
    updatedAt       DateTime @updatedAt
}

model Category {
    /// @zod.custom.omit([input, model])
    tenantID         String
    categoryID       String   @id @unique(map: "Category_ID") @default(uuid())
    categoryName     String
    createdAt        DateTime @default(now())
    updatedAt        DateTime @updatedAt

    items Item[]
}

model Item {
    /// @zod.custom.omit([input, model])
    tenantID     String
    itemID       String   @id @unique(map: "Item_ID") @default(uuid())
    itemName     String
    createdAt    DateTime @default(now())
    updatedAt    DateTime @updatedAt

    Category Category? @relation(fields: [categoryID], references: [categoryID])
    categoryID   String

    @@index([categoryID], name: "fk_category_Item_categoryID_1")
}

Error from typescript

Getting error on this generated code

export const ItemCreateManyCategoryInputEnvelopeSchema: z.ZodType<Prisma.ItemCreateManyCategoryInputEnvelope> = z.object({
  data: z.union([ z.lazy(() => ItemCreateManyCategoryInputSchema),z.lazy(() => ItemCreateManyCategoryInputSchema).array() ]),
  skipDuplicates: z.boolean().optional()
}).strict();

Error

Type 'ZodObject<{ data: ZodUnion<[ZodLazy<ZodType<Omit<ItemCreateManyCategoryInput, "tenantID">, ZodTypeDef, Omit<ItemCreateManyCategoryInput, "tenantID">>>, ZodArray<...>]>; skipDuplicates: ZodOptional<...>; }, "strict", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<ItemCreateManyCategoryInputEnvelope, ZodTypeDef, ItemCreateManyCategoryInputEnvelope>'.
  The types of '_type.data' are incompatible between these types.
    Type 'Omit<ItemCreateManyCategoryInput, "tenantID"> | Omit<ItemCreateManyCategoryInput, "tenantID">[]' is not assignable to type 'Enumerable<ItemCreateManyCategoryInput>'.
      Type 'Omit<ItemCreateManyCategoryInput, "tenantID">' is not assignable to type 'Enumerable<ItemCreateManyCategoryInput>'.
        Property 'tenantID' is missing in type 'Omit<ItemCreateManyCategoryInput, "tenantID">' but required in type 'ItemCreateManyCategoryInput'.typescript(2322)
index.d.ts(4632, 5): 'tenantID' is declared here.
const ItemCreateManyCategoryInputEnvelopeSchema: z.ZodType<Prisma.ItemCreateManyCategoryInputEnvelope, z.ZodTypeDef, Prisma.ItemCreateManyCategoryInputEnvelope>

Link to codesandbox

Darkbound commented 4 months ago

I am having the same issue, practically almost everything that I can do with @z yields TS errors making the rich comments unusable