-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
fix: properly handle missing fields when evaluating @@validate
model-level rules
#1097
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { loadSchema } from '@zenstackhq/testtools'; | ||
|
||
describe('issue 1078', () => { | ||
it('regression', async () => { | ||
const { prisma, enhance } = await loadSchema( | ||
` | ||
model Counter { | ||
id String @id | ||
|
||
name String | ||
value Int | ||
|
||
@@validate(value >= 0) | ||
@@allow('all', true) | ||
} | ||
` | ||
); | ||
|
||
const db = enhance(); | ||
|
||
const counter = await db.counter.create({ | ||
data: { id: '1', name: 'It should create', value: 1 }, | ||
}); | ||
|
||
//! This query fails validation | ||
const updated = await db.counter.update({ | ||
where: { id: '1' }, | ||
data: { name: 'It should update' }, | ||
}); | ||
}); | ||
Comment on lines
+3
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 'regression' test case effectively demonstrates the handling of missing fields during model-level validation, specifically testing the scenario where the 'value' field is omitted in an update operation. This aligns well with the PR objectives of ensuring that missing fields are correctly handled in validation rules.
|
||
|
||
it('read', async () => { | ||
const { prisma, enhance } = await loadSchema( | ||
` | ||
model Post { | ||
id Int @id() @default(autoincrement()) | ||
title String @allow('read', true, true) | ||
content String | ||
} | ||
`, | ||
{ logPrismaQuery: true } | ||
); | ||
|
||
const db = enhance(); | ||
|
||
const post = await prisma.post.create({ data: { title: 'Post1', content: 'Content' } }); | ||
await expect(db.post.findUnique({ where: { id: post.id } })).toResolveNull(); | ||
await expect(db.post.findUnique({ where: { id: post.id }, select: { title: true } })).resolves.toEqual({ | ||
title: 'Post1', | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The update to the
makeValidationRefinements
function introduces a check for simple field references usingisDataModelFieldReference
. This logic treatsundefined
fields astrue
in the validation context, which aligns with the PR objectives of correctly handling missing fields in model-level validations.undefined
should not be treated astrue
. It might be beneficial to add more tests covering various validation scenarios to ensure the change's correctness and prevent regressions.undefined
fields are treated in validation rules.