Skip to content

Commit 2659bc8

Browse files
authored
fix: check and complaint if @@index references fields fro m delegate base (#322)
* fix: check and complaint if `@@index` references fields fro m delegate base fixes #283 * update
1 parent 23adf91 commit 2659bc8

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,13 @@ export default class AttributeApplicationValidator implements AstValidator<Attri
290290
}
291291
}
292292

293-
@check('@@unique')
294293
@check('@@id')
294+
@check('@@index')
295+
@check('@@unique')
295296
// @ts-expect-error
296-
private _checkUnique(attr: AttributeApplication, accept: ValidationAcceptor) {
297+
private _checkConstraint(attr: AttributeApplication, accept: ValidationAcceptor) {
297298
const fields = attr.args[0]?.value;
299+
const attrName = attr.decl.ref?.name;
298300
if (!fields) {
299301
accept('error', `expects an array of field references`, {
300302
node: attr.args[0]!,
@@ -303,7 +305,7 @@ export default class AttributeApplicationValidator implements AstValidator<Attri
303305
}
304306
if (isArrayExpr(fields)) {
305307
if (fields.items.length === 0) {
306-
accept('error', `\`@@unique\` expects at least one field reference`, { node: fields });
308+
accept('error', `\`${attrName}\` expects at least one field reference`, { node: fields });
307309
return;
308310
}
309311
fields.items.forEach((item) => {
@@ -321,7 +323,7 @@ export default class AttributeApplicationValidator implements AstValidator<Attri
321323
}
322324

323325
if (item.target.ref.$container !== attr.$container && isDelegateModel(item.target.ref.$container)) {
324-
accept('error', `Cannot use fields inherited from a polymorphic base model in \`@@unique\``, {
326+
accept('error', `Cannot use fields inherited from a polymorphic base model in \`${attrName}\``, {
325327
node: item,
326328
});
327329
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { loadSchemaWithError } from '@zenstackhq/testtools';
2+
import { describe, it } from 'vitest';
3+
4+
describe('Regression for issue #283', () => {
5+
it('verifies issue 283', async () => {
6+
await loadSchemaWithError(
7+
`
8+
model Base {
9+
id Int @id @default(autoincrement())
10+
x Int
11+
type String
12+
@@delegate(type)
13+
}
14+
15+
model Sub extends Base {
16+
y Int
17+
@@index([x, y])
18+
}
19+
`,
20+
'Cannot use fields inherited from a polymorphic base model',
21+
);
22+
});
23+
});

0 commit comments

Comments
 (0)