Skip to content

fix(zod): don't include @@validate rules when generating schema for validating update input #1625

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

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
Loading