Skip to content

validations on reference properties don't work #1563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chunkerchunker opened this issue Jul 8, 2024 · 1 comment
Closed

validations on reference properties don't work #1563

chunkerchunker opened this issue Jul 8, 2024 · 1 comment
Milestone

Comments

@chunkerchunker
Copy link
Contributor

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.

@ymc9
Copy link
Member

ymc9 commented Jul 29, 2024

Thanks for reporting this @chunkerchunker . The current zod schema generated for validating update input is excessively strict. I'm making a fix and will make it part of the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants