chrishoermann / zod-prisma-types

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

Imports are not being generated when using createRelationValuesTypes: true #147

Closed hobinwood closed 1 year ago

hobinwood commented 1 year ago

Hello, thank you so much for creating this amazing library.

I'm having an import problem when generating my schema, when I put the following settings:

image

This is the part of my schema that is giving trouble:

image

And this is the part of the generated schema that is coming without the relations imports:

image

Can you tell me if I'm forgetting some important setting? Thanks in advance.

chrishoermann commented 1 year ago

@hobinwood so as I can see from your screenshot the type is not imported in the file? can you please check if the type is even generated in the file where it should be imported from? I assume that should be CustomerGroupHasLanguage in the models folder.

can you also provide a full prisma schema so I can investigate this a bit furhter.

hobinwood commented 1 year ago

Hello, thanks for the reply.

Yes, the files are being generated with the right types, only the imports even if they are not being made.

We managed to make a workaround that apparently worked at the time, but I don't know if it will have consequences...

image

Here is my schema as requested:

generator zod { provider = "zod-prisma-types" output = "../../zod/src/generated" useMultipleFiles = true // default is false // createInputTypes = false // default is true // createModelTypes = false // default is true // addInputTypeValidation = false // default is true // addIncludeType = false // default is true // addSelectType = false // default is true // validateWhereUniqueInput = true // default is false // createOptionalDefaultValuesTypes = true // default is false createRelationValuesTypes = true // default is false // createPartialTypes = true // default is false useDefaultValidators = false // default is true // coerceDate = false // default is true // writeNullishInModelTypes = true // default is false // prismaClientPath = "./path/to/prisma/client" // default is client output path }

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

datasource db { provider = "sqlite" url = env("DATABASE_URL") }

model Product { id String @id @default(cuid()) name String /// @zod.string.min(5, { message: 'Must be at least 5 characters' }) sku String shortName String alwaysSellUnity Boolean @default(true) /// @zod.custom.use(z.coerce.boolean()) isToSellUnityOrWeightInternationally String @default("unity") weight Int /// @zod.custom.use(z.coerce.number().min(1, {message:'precisa ser maior que 0'})) quantity Int @default(0) /// @zod.custom.use(z.coerce.number().min(1, {message:'precisa ser maior que 0'})) isToControlStock Boolean @default(false) /// @zod.custom.use(z.coerce.boolean()) availableAt DateTime @default(now()) isEnabled Boolean @default(true) }

model Account { id String @id @default(cuid()) userId String type String provider String providerAccountId String refresh_token String? // @db.Text access_token String? // @db.Text expires_at Int? token_type String? scope String? id_token String? // @db.Text session_state String? user User @relation(fields: [userId], references: [id], onDelete: Cascade)

@@unique([provider, providerAccountId])

}

model Session { id String @id @default(cuid()) sessionToken String @unique userId String expires DateTime user User @relation(fields: [userId], references: [id], onDelete: Cascade) }

model User { id String @id @default(cuid()) name String? email String? @unique emailVerified DateTime? image String? accounts Account[] sessions Session[] }

model VerificationToken { identifier String token String @unique expires DateTime

@@unique([identifier, token])

}

model Currency { id String @id @default(cuid()) name String code String @unique /// ex: BRL, USD, EUR value Int /// minimum possible @zod.custom.use(z.coerce.number()) symbolRight String @default("") symbolLeft String @default("R$") /// ex: $ decimalPlaces Int @default(2) /// @zod.custom.use(z.coerce.number()) isEnabled Boolean @default(true)

storeMinimumCurrencySubtotals StoreMinimumCurrencySubtotal[]
countries                     Country[]

}

model Store { id String @id @default(cuid()) name String type String /// todo: enum storeMinimumCurrencySubtotals StoreMinimumCurrencySubtotal[] showedCategories Category[] }

model StoreMinimumCurrencySubtotal { id String @id @default(cuid()) minimum Int /// @zod.custom.use(z.coerce.number())

currencyId String
currency   Currency @relation(fields: [currencyId], references: [id])
storeId    String
store      Store    @relation(fields: [storeId], references: [id])

@@unique([storeId, currencyId])

}

model Category { id String @id @default(cuid())

imagePath            String /// @zod.string.min(5, { message: 'Must be at least 5 characters' })
isEnabled            Boolean               @default(true)
friendlyUrl          String /// @zod.string.min(5, { message: 'Must be at least 5 characters' })
sortOrder            Int                   @unique /// @zod.custom.use(z.coerce.number().min(1, {message:'precisa ser maior que 1'}))
categoryHasLanguages CategoryHasLanguage[]
showedInStores       Store[]

}

model CategoryHasLanguage { id String @id @default(cuid()) name String @unique /// @zod.string.min(5, { message: 'Must be at least 5 characters' }) description String /// @zod.string.min(5, { message: 'Must be at least 5 characters' }) metaTitle String @unique metaDescription String

languageId String
language   Language @relation(fields: [languageId], references: [id])
categoryId String
category   Category @relation(fields: [categoryId], references: [id])

@@unique([categoryId, languageId])

}

model Language { id String @id @default(cuid())

name                           String                          @unique /// @zod.string.min(5, { message: 'Must be at least 5 characters' })
code                           String                          @unique /// @zod.string.min(5, { message: 'Must be at least 5 characters' })
imagePath                      String /// @zod.string.min(5, { message: 'Must be at least 5 characters' })
isEnabled                      Boolean                         @default(true)
sortOrder                      Int                             @unique /// @zod.custom.use(z.coerce.number())
categoryHasLanguages           CategoryHasLanguage[]
countries                      Country[]
countryRestrictionHasLanguages CountryRestrictionHasLanguage[]
customerGroupHasLanguages      CustomerGroupHasLanguage[]

}

model Country { id String @id @default(cuid())

name            String  @unique /// @zod.string.min(2, { message: 'Must be at least 2 characters' })
code            String  @unique /// @zod.string.min(2, { message: 'Must be at least 2 characters' })
isEnabled       Boolean @default(true)
restrictForSale Boolean @default(false)

languageId                     String
language                       Language                        @relation(fields: [languageId], references: [id])
currencyId                     String
currency                       Currency                        @relation(fields: [currencyId], references: [id])
countryRestrictionHasLanguages CountryRestrictionHasLanguage[]

}

model CountryRestrictionHasLanguage { id String @id @default(cuid())

name        String @unique /// @zod.string.min(2, { message: 'Must be at least 2 characters' })
description String /// @zod.string.min(5, { message: 'Must be at least 5 characters' })

languageId String
language   Language @relation(fields: [languageId], references: [id])
countryId  String
country    Country  @relation(fields: [countryId], references: [id])

@@unique([countryId, languageId])

}

model CustomerGroup { id String @id @default(cuid()) needApproval Boolean @default(false) sortOrder Int @unique /// @zod.custom.use(z.coerce.number().min(1, {message:'form.error.min,{"size": 1}' })) customerGroupHasLanguages CustomerGroupHasLanguage[]

createdAt DateTime @default(now()) /// zod.custom.omit([input])
updatedAt DateTime @updatedAt /// zod.custom.omit([input])

}

model CustomerGroupHasLanguage { id String @id @default(cuid()) name String /// @zod.string.min(5, { message: 'Must be at least 5 characters' }) description String /// @zod.string.min(5, { message: 'Must be at least 5 characters' })

languageId      String
language        Language      @relation(fields: [languageId], references: [id])
customerGroupId String
customerGroup   CustomerGroup @relation(fields: [customerGroupId], references: [id])

createdAt DateTime @default(now()) /// zod.custom.omit([input])
updatedAt DateTime @updatedAt /// zod.custom.omit([input])

@@unique([customerGroupId, languageId])

}

michiim commented 1 year ago

I have the same problem. Seems to be dependent on the name of the models. If the CustomerGroupHasLanguage was just HasLanguage, then the import would be done. As soon as the referenced model starts with its own model name, it doesn't seem to do the import anymore.

gregg-cbs commented 1 year ago

I can confirm this too. I can import the types, they exist but for some reason the generator is skipping the imports. If I import the missing types and schemas all works fine. It is only happening to this file and this one type 'delivery'. If i remove the delivery field from Orders then all is fine, no other import issues. If i keep delivery but remove all properties from it except for one string, this issue still persists

image

gregg-cbs commented 1 year ago

Seems to be dependent on the name of the models.

I back this. Must be the importer thinking the type has been imported because the name somewhat matches the model name it is being used in. See the below:

model Order {
  delivery OrderDelivery // doesnt import
  delivery OOrderDelivery // doesnt import
  delivery DeliveryOrder // doesnt import
  delivery Tarzan // does import
  delivery Delivery // does import
}
chrishoermann commented 1 year ago

Thank you all for the input. This bug should now be fixed in the latest version.