chrishoermann / zod-prisma-types

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

Failing to generate import in certain situations #180

Closed datagutt closed 8 months ago

datagutt commented 10 months ago

Description of issue

I have some issues with certain files not importing schemas before using them.

Dependencies

/// @@Gen.model(hide: true) model Role { id String @id @default(uuid()) createdAt DateTime @default(now()) updatedAt DateTime? @updatedAt deletedAt DateTime?

PermissionForRole PermissionForRole[] }

/// @@Gen.model(hide: true) model PermissionForRole { id String @id @default(uuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt deletedAt DateTime?

roleId String role Role @relation(fields: [roleId], references: [id])

@@unique([roleId]) }


This full reproduction repo can be found at https://github.com/datagutt/prisma-zod-bug.
## Current behaviour
In generated/schemas/outputTypeSchemas/RoleFindManyArgsSchema.ts, this line fails:
```typescript

  PermissionForRole: z.union([z.boolean(),z.lazy(() => PermissionForRoleFindManyArgsSchema)]).optional(),

This is due to PermissionForRoleFindManyArgsSchema never being imported. This seems to only be happening in the RoleFindManyArgsSchema.ts-file, not other files.

Expected behaviour

PermissionForRoleFindManyArgsSchema should be imported in the files that use it.

Additional information

It seems that this bug is not triggered if the two models are named something completely different. I think the bug is related to both models starting or ending with "Role". Renaming PermissionForRoleto something different causes the file to be correctly imported.

datagutt commented 10 months ago

Seems that this workaround is what causes the import to disappear. If i remove the .filter(), it starts working again. Hopefully this helps you debug the issue.

datagutt commented 10 months ago

Oh, i see. The .includes() part is to avoid self-reference, but it doesn't actually check if the import starts with /, causing PermissionForRoleFindManyArgsSchema to match, since it contains RoleFindManyArgsSchema. I have a fix for this in #181.

Have a nice day!