chrishoermann / zod-prisma-types

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

Add missing With for nested relations #127

Closed MichaelOren closed 1 year ago

MichaelOren commented 1 year ago

Hey Chris,

I ran into a bug in the latest PR.

I have nested relations.

export type BankingProductDetailedOptionalDefaultsRelations = {
  Bank?: BankOptionalDefaultsRelations | null;
  additionalInformation?: AdditionalInfoDetailedOptionalDefaultsRelations | null; // This needs to be WithRelations
};

export type BankingProductDetailedOptionalDefaultsWithRelations = z.infer<typeof BankingProductDetailedOptionalDefaultsSchema> & BankingProductDetailedOptionalDefaultsRelations // This is missing the non relation data

export const BankingProductDetailedOptionalDefaultsWithRelationsSchema: z.ZodType<BankingProductDetailedOptionalDefaultsWithRelations> = BankingProductDetailedOptionalDefaultsSchema.merge(z.object({
  Bank: z.lazy(() => BankOptionalDefaultsWithRelationsSchema).nullish(),
  additionalInformation: z.lazy(() => AdditionalInfoDetailedOptionalDefaultsWithRelationsSchema).nullish(), // This is correct, but the Generics make TS not able to access the non relational data
}))

Without the With I could not access the non relational data, since Typescript does not know they exist

Let me know if there is anything I have not thought of by adding this, hope there is no downstream effects eg if there are not nested relations 🙏

Thanks for this awesome package and the always quick responses (it is Easter, so no expectations 😄 )