-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat(validation): add API to suppress validation #301
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -108,4 +108,66 @@ describe('Custom validation tests', () => { | |
| ).toResolveTruthy(); | ||
| } | ||
| }); | ||
|
|
||
| it('allows disabling validation', async () => { | ||
| const db = await createTestClient( | ||
| ` | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| email String @unique @email | ||
| @@validate(length(email, 8)) | ||
| @@allow('all', true) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await expect( | ||
| db.user.create({ | ||
| data: { | ||
| email: 'xyz', | ||
| }, | ||
| }), | ||
| ).toBeRejectedByValidation(); | ||
| await expect( | ||
| db.user.create({ | ||
| data: { | ||
| email: '[email protected]', | ||
| }, | ||
| }), | ||
| ).toBeRejectedByValidation(); | ||
|
|
||
| await expect( | ||
| db.$setInputValidation(false).user.create({ | ||
| data: { | ||
| id: 1, | ||
| email: 'xyz', | ||
| }, | ||
| }), | ||
| ).toResolveTruthy(); | ||
|
|
||
| await expect( | ||
| db.$setInputValidation(false).user.update({ | ||
| where: { id: 1 }, | ||
| data: { | ||
| email: '[email protected]', | ||
| }, | ||
| }), | ||
| ).toResolveTruthy(); | ||
|
|
||
| // original client not affected | ||
| await expect( | ||
| db.user.create({ | ||
| data: { | ||
| email: 'xyz', | ||
| }, | ||
| }), | ||
| ).toBeRejectedByValidation(); | ||
| await expect( | ||
| db.user.create({ | ||
| data: { | ||
| email: '[email protected]', | ||
| }, | ||
| }), | ||
| ).toBeRejectedByValidation(); | ||
| }); | ||
| }); | ||
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,66 @@ | ||
| import { createPolicyTestClient } from '@zenstackhq/testtools'; | ||
| import { expect, it } from 'vitest'; | ||
|
|
||
| // TODO: field-level policy support | ||
| it.skip('verifies issue 2000', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| type Base { | ||
| id String @id @default(uuid()) @deny('update', true) | ||
| createdAt DateTime @default(now()) @deny('update', true) | ||
| updatedAt DateTime @updatedAt @deny('update', true) | ||
| active Boolean @default(false) | ||
| published Boolean @default(true) | ||
| deleted Boolean @default(false) | ||
| startDate DateTime? | ||
| endDate DateTime? | ||
|
|
||
| @@allow('create', true) | ||
| @@allow('read', true) | ||
| @@allow('update', true) | ||
| } | ||
|
|
||
| enum EntityType { | ||
| User | ||
| Alias | ||
| Group | ||
| Service | ||
| Device | ||
| Organization | ||
| Guest | ||
| } | ||
|
|
||
| model Entity with Base { | ||
| entityType EntityType | ||
| name String? @unique | ||
| members Entity[] @relation("members") | ||
| memberOf Entity[] @relation("members") | ||
| @@delegate(entityType) | ||
|
|
||
|
|
||
| @@allow('create', true) | ||
| @@allow('read', true) | ||
| @@allow('update', true) | ||
| @@validate(!active || (active && name != null), "Active Entities Must Have A Name") | ||
| } | ||
|
|
||
| model User extends Entity { | ||
| profile Json? | ||
| username String @unique | ||
| password String @password | ||
|
|
||
| @@allow('create', true) | ||
| @@allow('read', true) | ||
| @@allow('update', true) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| await expect(db.user.create({ data: { username: 'admin', password: 'abc12345' } })).toResolveTruthy(); | ||
| await expect( | ||
| db.user.update({ where: { username: 'admin' }, data: { password: 'abc123456789123' } }), | ||
| ).toResolveTruthy(); | ||
|
|
||
| // violating validation rules | ||
| await expect(db.user.update({ where: { username: 'admin' }, data: { active: true } })).toBeRejectedByPolicy(); | ||
| }); |
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,91 @@ | ||
| import { createPolicyTestClient } from '@zenstackhq/testtools'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| // TODO: field-level policy support | ||
| describe.skip('Regression for issue 2007', () => { | ||
| it('regression1', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Page { | ||
| id String @id @default(cuid()) | ||
| title String | ||
|
|
||
| images Image[] | ||
|
|
||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model Image { | ||
| id String @id @default(cuid()) @deny('update', true) | ||
| url String | ||
| pageId String? | ||
| page Page? @relation(fields: [pageId], references: [id]) | ||
|
|
||
| @@allow('all', true) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| const image = await db.image.create({ | ||
| data: { | ||
| url: 'https://example.com/image.png', | ||
| }, | ||
| }); | ||
|
|
||
| await expect( | ||
| db.image.update({ | ||
| where: { id: image.id }, | ||
| data: { | ||
| page: { | ||
| create: { | ||
| title: 'Page 1', | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ).toResolveTruthy(); | ||
| }); | ||
|
|
||
| it('regression2', async () => { | ||
| const db = await createPolicyTestClient( | ||
| ` | ||
| model Page { | ||
| id String @id @default(cuid()) | ||
| title String | ||
|
|
||
| images Image[] | ||
|
|
||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model Image { | ||
| id String @id @default(cuid()) | ||
| url String | ||
| pageId String? @deny('update', true) | ||
| page Page? @relation(fields: [pageId], references: [id]) | ||
|
|
||
| @@allow('all', true) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| const image = await db.image.create({ | ||
| data: { | ||
| url: 'https://example.com/image.png', | ||
| }, | ||
| }); | ||
|
|
||
| await expect( | ||
| db.image.update({ | ||
| where: { id: image.id }, | ||
| data: { | ||
| page: { | ||
| create: { | ||
| title: 'Page 1', | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ).toBeRejectedByPolicy(); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.