Skip to content

[Feature] Generate Zod Schemas for Prisma Foreign Keys #828

Closed
@kpidoff

Description

@kpidoff

Context

Currently, the generation with the Zod plugin does not generate Zod schemas for foreign keys of Prisma tables when creating an entity. This presents an issue as foreign keys are required to establish relationships with other tables, and it is essential to specify validation constraints for these keys during creation.

Proposal

I propose enhancing the Zod plugin to include the generation of Zod schemas for foreign keys of Prisma tables when creating an entity. This would allow developers to specify validation constraints for foreign keys, such as minimum length, mandatory presence, etc., when creating a new entry in the table.

Benefits

  • Improving the Zod schema generation by including foreign keys would facilitate data validation during entity creation.
  • It would help maintain data consistency and integrity in the database by ensuring that foreign keys are correctly filled.

Exemple

Schema schema.zmodel

model Society {
    id String @id() @default(cuid())
    name String @length(1, 100, 'zod.required')
    brand Brand @relation(fields: [brandId], references: [id])
    brandId String
}

Schema zod

/* eslint-disable */
import { z } from 'zod';
const baseSchema = z.object({
    id: z.string(),
    name: z.string(),
});
export const SocietySchema = baseSchema;
export const SocietyCreateSchema = baseSchema.partial({
    id: true,
    name: true,
    brandId: z.string()
});
export const SocietyUpdateSchema = baseSchema.partial();

Conclusion

I believe that this enhancement would be beneficial for users of the Zod plugin working with Prisma databases. It would streamline the management of foreign keys during entity creation and improve data quality in the database.

Thank you for considering this feature request. I am willing to collaborate and contribute to its implementation if needed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions