From 3d7678a798924f7fce4bcff3a550fbd9e79a31fa Mon Sep 17 00:00:00 2001 From: Yiming Date: Mon, 29 Jul 2024 22:09:22 -0700 Subject: [PATCH] fix(zod): don't include @@validate rules when generating schema for validating update input (#1625) --- packages/schema/src/plugins/zod/generator.ts | 3 -- .../with-policy/field-validation.test.ts | 7 +++-- tests/regression/tests/issue-1563.test.ts | 30 +++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 tests/regression/tests/issue-1563.test.ts diff --git a/packages/schema/src/plugins/zod/generator.ts b/packages/schema/src/plugins/zod/generator.ts index 3f656668d..fecf69ced 100644 --- a/packages/schema/src/plugins/zod/generator.ts +++ b/packages/schema/src/plugins/zod/generator.ts @@ -464,9 +464,6 @@ export const ${upperCaseFirst(model.name)}PrismaCreateSchema = ${prismaCreateSch .join(',\n')} })`; prismaUpdateSchema = this.makePartial(prismaUpdateSchema); - if (refineFuncName) { - prismaUpdateSchema = `${refineFuncName}(${prismaUpdateSchema})`; - } writer.writeLine( ` /** diff --git a/tests/integration/tests/enhancements/with-policy/field-validation.test.ts b/tests/integration/tests/enhancements/with-policy/field-validation.test.ts index 144bf0be6..d508381b4 100644 --- a/tests/integration/tests/enhancements/with-policy/field-validation.test.ts +++ b/tests/integration/tests/enhancements/with-policy/field-validation.test.ts @@ -646,8 +646,9 @@ describe('Model-level validation', () => { const db = enhance(); - await expect(db.model.create({ data: { x: 2, y: 1 } })).toResolveTruthy(); - await expect(db.model.create({ data: { x: 1, y: 2 } })).toBeRejectedByPolicy(); + await expect(db.model.create({ data: { id: 1, x: 2, y: 1 } })).toResolveTruthy(); + await expect(db.model.update({ where: { id: 1 }, data: { y: 3 } })).toBeRejectedByPolicy(); + await expect(db.model.update({ where: { id: 1 }, data: { x: 3 } })).toResolveTruthy(); }); it('int optionality', async () => { @@ -948,7 +949,7 @@ describe('Model-level validation', () => { await expect(db.model.create({ data: { id: 1, x: 0, y: 0 } })).toBeRejectedByPolicy(); await expect(db.model.create({ data: { id: 1, x: 1, y: 0 } })).toResolveTruthy(); - await expect(db.model.update({ where: { id: 1 }, data: {} })).toBeRejectedByPolicy(); + await expect(db.model.update({ where: { id: 1 }, data: {} })).toResolveTruthy(); await expect(db.model.update({ where: { id: 1 }, data: { y: 2 } })).toBeRejectedByPolicy(); await expect(db.model.update({ where: { id: 1 }, data: { y: 1 } })).toResolveTruthy(); await expect(db.model.update({ where: { id: 1 }, data: { x: 2, y: 1 } })).toResolveTruthy(); diff --git a/tests/regression/tests/issue-1563.test.ts b/tests/regression/tests/issue-1563.test.ts new file mode 100644 index 000000000..179bc356f --- /dev/null +++ b/tests/regression/tests/issue-1563.test.ts @@ -0,0 +1,30 @@ +import { loadSchema } from '@zenstackhq/testtools'; + +describe('issue 1563', () => { + it('regression', 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(); + }); +});