Skip to content

validations on reference properties don't work #1563

@chunkerchunker

Description

@chunkerchunker

Description and expected behavior

Validations on reference properties work on creation, but fail on updates.

Following is a (contrived) example showing the issue:

    it('reference validation', async () => {
        const { enhance } = await loadSchema(
            `
        model ModelA {
            id String @id @default(cuid())
            ref ModelB[]
        }
        model ModelB {
            id String @id @default(cuid())
            ref ModelA? @relation(fields: [refId], references: [id])
            refId String?

            @@validate(refId != null, "refId must be set")
        }
        `,
            { enhancements: ['validation'] }
        );

        const db = enhance();

        const a = await db.modelA.create({data: {}});
        const b = await db.modelB.create({data: {refId: a.id}});

        await expect(db.modelB.update({ where: {id: b.id}, data: { refId: a.id } })).toResolveTruthy();
  }

The issue seems to be with the generated zod schema for ModelBPrismaUpdateSchema:

export const ModelBPrismaUpdateSchema = refineModelB(z.object({
    id: z.string()
}).partial());

I don't know what the correct fix is, but the following change fixes the test:

export const ModelBPrismaUpdateSchema = refineModelB(z.object({
    id: z.string()
}).merge(fkSchema).partial());

Environment:

  • ZenStack version: 2.2.4
  • Prisma version: 5.13 & 5.16.1 (shouldn't matter)
  • Database type: Postgresql & sqlite (shouldn't matter)

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions