Skip to content

Commit

Permalink
fix(zod): don't include @@Validate rules when generating schema for v…
Browse files Browse the repository at this point in the history
…alidating update input (#1625)
  • Loading branch information
ymc9 authored Jul 30, 2024
1 parent bd8c36c commit 3d7678a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
3 changes: 0 additions & 3 deletions packages/schema/src/plugins/zod/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,6 @@ export const ${upperCaseFirst(model.name)}PrismaCreateSchema = ${prismaCreateSch
.join(',\n')}
})`;
prismaUpdateSchema = this.makePartial(prismaUpdateSchema);
if (refineFuncName) {
prismaUpdateSchema = `${refineFuncName}(${prismaUpdateSchema})`;
}
writer.writeLine(
`
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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();
Expand Down
30 changes: 30 additions & 0 deletions tests/regression/tests/issue-1563.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 3d7678a

Please sign in to comment.