Repository navigation
fix(orm): skip a @@validate rule on update when the update sends none of its fields #2905
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
Open
ErikDakoda
wants to merge
3
commits into
zenstackhq:dev
Choose a base branch
from
ErikDakoda:fix/update-validate-partial-data
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−7
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ee5bbda
fix(orm): skip a @@validate rule on update when the update sends none…
ErikDakoda b2101ef
fix(orm): check a partial-update rule when a relation sets its foreig…
ErikDakoda 1535d72
fix(orm): do not check a partial-update rule for a nested update of i…
ErikDakoda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| // https://github.com/zenstackhq/zenstack/issues/2904 | ||
| describe('Regression for issue 2904', () => { | ||
| const schema = ` | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| bots Bot[] | ||
| } | ||
|
|
||
| model Bot { | ||
| id Int @id @default(autoincrement()) | ||
| name String | ||
| useCase String? | ||
| modelName String? | ||
| owner User? @relation(fields: [ownerId], references: [id]) | ||
| ownerId Int? | ||
|
|
||
| @@validate(useCase != null || modelName != null, 'needs a use case or a model') | ||
| } | ||
| `; | ||
|
|
||
| it('accepts an update that sends none of the fields a rule references', async () => { | ||
| const db = await createTestClient(schema); | ||
| const user = await db.user.create({ data: {} }); | ||
| const bot = await db.bot.create({ data: { name: 'a', useCase: 'chat', ownerId: user.id } }); | ||
|
|
||
| await expect(db.bot.update({ where: { id: bot.id }, data: { name: 'b' } })).resolves.toMatchObject({ | ||
| name: 'b', | ||
| useCase: 'chat', | ||
| }); | ||
| await expect(db.bot.updateMany({ where: { id: bot.id }, data: { name: 'c' } })).resolves.toEqual({ | ||
| count: 1, | ||
| }); | ||
| await expect( | ||
| db.bot.upsert({ | ||
| where: { id: bot.id }, | ||
| create: { name: 'x', useCase: 'chat' }, | ||
| update: { name: 'd' }, | ||
| }), | ||
| ).resolves.toMatchObject({ name: 'd' }); | ||
| await expect( | ||
| db.user.update({ | ||
| where: { id: user.id }, | ||
| data: { bots: { update: { where: { id: bot.id }, data: { name: 'e' } } } }, | ||
| include: { bots: true }, | ||
| }), | ||
| ).resolves.toMatchObject({ bots: [{ name: 'e' }] }); | ||
| }); | ||
|
|
||
| it('still checks a rule when the update sends a field it references', async () => { | ||
| const db = await createTestClient(schema); | ||
| const bot = await db.bot.create({ data: { name: 'a', useCase: 'chat' } }); | ||
|
|
||
| await expect( | ||
| db.bot.update({ where: { id: bot.id }, data: { useCase: null, modelName: null } }), | ||
| ).toBeRejectedByValidation(['needs a use case or a model']); | ||
| await expect( | ||
| db.bot.update({ where: { id: bot.id }, data: { useCase: null, modelName: 'gpt' } }), | ||
| ).resolves.toMatchObject({ useCase: null, modelName: 'gpt' }); | ||
| }); | ||
|
|
||
| it('still checks every rule on create', async () => { | ||
| const db = await createTestClient(schema); | ||
| await expect(db.bot.create({ data: { name: 'a' } })).toBeRejectedByValidation(['needs a use case or a model']); | ||
| }); | ||
|
|
||
| it('checks a rule when a relation field sets a foreign key the rule references', async () => { | ||
| const db = await createTestClient(` | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| posts Post[] | ||
| } | ||
|
|
||
| model Post { | ||
| id Int @id @default(autoincrement()) | ||
| title String | ||
| author User? @relation(fields: [authorId], references: [id]) | ||
| authorId Int? | ||
|
|
||
| @@validate(authorId != null, 'needs an author') | ||
| } | ||
| `); | ||
| const user = await db.user.create({ data: {} }); | ||
| const post = await db.post.create({ data: { title: 'a', authorId: user.id } }); | ||
|
|
||
| await expect(db.post.update({ where: { id: post.id }, data: { title: 'b' } })).resolves.toMatchObject({ | ||
| title: 'b', | ||
| }); | ||
| await expect( | ||
| db.post.update({ where: { id: post.id }, data: { author: { disconnect: true } } }), | ||
| ).toBeRejectedByValidation(['needs an author']); | ||
| }); | ||
|
|
||
| it('accepts a nested update of a relation whose foreign key a rule references', async () => { | ||
| const db = await createTestClient(` | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| name String? | ||
| posts Post[] | ||
| } | ||
|
|
||
| model Post { | ||
| id Int @id @default(autoincrement()) | ||
| title String | ||
| author User? @relation(fields: [authorId], references: [id]) | ||
| authorId Int? | ||
|
|
||
| @@validate(authorId != null, 'needs an author') | ||
| } | ||
| `); | ||
| const user = await db.user.create({ data: {} }); | ||
| const post = await db.post.create({ data: { title: 'a', authorId: user.id } }); | ||
|
|
||
| await expect( | ||
| db.post.update({ | ||
| where: { id: post.id }, | ||
| data: { author: { update: { name: 'x' } } }, | ||
| include: { author: true }, | ||
| }), | ||
| ).resolves.toMatchObject({ authorId: user.id, author: { name: 'x' } }); | ||
| await expect( | ||
| db.post.update({ | ||
| where: { id: post.id }, | ||
| data: { author: { update: { name: 'y' }, disconnect: true } }, | ||
| }), | ||
| ).toBeRejectedByValidation(['needs an author']); | ||
| }); | ||
|
|
||
| it('checks a rule that references a field through this', async () => { | ||
| const db = await createTestClient(` | ||
| model Item { | ||
| id Int @id @default(autoincrement()) | ||
| name String | ||
| amount Int | ||
|
|
||
| @@validate(this.amount > 0, 'amount must be positive') | ||
| } | ||
| `); | ||
| const item = await db.item.create({ data: { name: 'a', amount: 1 } }); | ||
|
|
||
| await expect(db.item.update({ where: { id: item.id }, data: { name: 'b' } })).resolves.toMatchObject({ | ||
| name: 'b', | ||
| }); | ||
| await expect(db.item.update({ where: { id: item.id }, data: { amount: -1 } })).toBeRejectedByValidation([ | ||
| 'amount must be positive', | ||
| ]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Script executed:
rg -n 'disconnect|relationChangesForeignKey|make.*Update.*Schema' packages/orm/src/client/zod/factory.ts packages/zod/src/utils.tsRepository: zenstackhq/zenstack
Length of output: 2783
Script executed:
Repository: zenstackhq/zenstack
Length of output: 8975
Script executed:
Repository: zenstackhq/zenstack
Length of output: 6091
Script executed:
Repository: zenstackhq/zenstack
Length of output: 3739
Script executed:
Repository: zenstackhq/zenstack
Length of output: 448
Script executed:
sed -n '492,540p' packages/zod/src/utils.tsRepository: zenstackhq/zenstack
Length of output: 1836
Script executed:
Repository: zenstackhq/zenstack
Length of output: 4183
Skip
disconnect: falsewhen deciding whether a partial update triggers a rule.relationChangesForeignKeytreats{ disconnect: false }as a foreign-key change. The ORM ignores that value, so the foreign key stays unchanged. The rule still runs without the foreign key in the payload.authorId != nullthen fails, and the no-op update is rejected.Suggested fix