Skip to content

Commit 4a08dfe

Browse files
committed
update
1 parent bc610f4 commit 4a08dfe

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

packages/language/res/stdlib.zmodel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ function check(field: Any, operation: String?): Boolean {
705705
} @@@expressionContext([AccessPolicy])
706706

707707
/**
708-
* Gets entity's value before an update. Only valid when used in a "update" policy rule.
708+
* Gets entity's value before an update. Only valid when used in a "post-update" policy rule.
709709
*/
710710
function before(): Any {
711711
} @@@expressionContext([AccessPolicy])

packages/language/src/validators/attribute-application-validator.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ export default class AttributeApplicationValidator implements AstValidator<Attri
173173
// there can't possibly be a fk that points to it
174174
this.rejectNonOwnedRelationInExpression(attr.args[1].value, accept);
175175
}
176+
177+
if (kind !== 'post-update' && attr.args[1]?.value) {
178+
const beforeCall = AstUtils.streamAst(attr.args[1]?.value).find(isBeforeInvocation);
179+
if (beforeCall) {
180+
accept('error', `"before()" is only allowed in "post-update" policy rules`, { node: beforeCall });
181+
}
182+
}
176183
}
177184

178185
private rejectNonOwnedRelationInExpression(expr: Expression, accept: ValidationAcceptor) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { describe, it } from 'vitest';
2+
import { loadSchemaWithError } from './utils';
3+
4+
describe('Attribute application validation tests', () => {
5+
it('rejects before in non-post-update policies', async () => {
6+
await loadSchemaWithError(
7+
`
8+
datasource db {
9+
provider = 'sqlite'
10+
url = 'file:./dev.db'
11+
}
12+
13+
model Foo {
14+
id Int @id @default(autoincrement())
15+
x Int
16+
@@allow('all', true)
17+
@@deny('update', before(x) > 2)
18+
}
19+
`,
20+
`"before()" is only allowed in "post-update" policy rules`,
21+
);
22+
});
23+
});

0 commit comments

Comments
 (0)