Skip to content

Commit 2e9fe67

Browse files
authored
fix: report validation error when binary expressions have arrays (#719)
1 parent cdc98e0 commit 2e9fe67

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

packages/schema/src/language-server/validator/expression-validator.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ export default class ExpressionValidator implements AstValidator<Expression> {
6464
case '<=':
6565
case '&&':
6666
case '||': {
67+
if (expr.left.$resolvedType?.array) {
68+
accept('error', 'operand cannot be an array', { node: expr.left });
69+
break;
70+
}
71+
72+
if (expr.right.$resolvedType?.array) {
73+
accept('error', 'operand cannot be an array', { node: expr.right });
74+
break;
75+
}
76+
6777
let supportedShapes: ExpressionType[];
6878
if (['>', '>=', '<', '<='].includes(expr.operator)) {
6979
supportedShapes = ['Int', 'Float', 'DateTime', 'Any'];
@@ -105,6 +115,11 @@ export default class ExpressionValidator implements AstValidator<Expression> {
105115

106116
case '==':
107117
case '!=': {
118+
if (!!expr.left.$resolvedType?.array !== !!expr.right.$resolvedType?.array) {
119+
accept('error', 'incompatible operand types', { node: expr });
120+
break;
121+
}
122+
108123
// disallow comparing model type with scalar type or comparison between
109124
// incompatible model types
110125
const leftType = expr.left.$resolvedType?.decl;

packages/schema/tests/schema/validation/attribute-validation.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,50 @@ describe('Attribute tests', () => {
553553
`)
554554
).toContain('incompatible operand types');
555555

556+
expect(
557+
await loadModelWithError(`
558+
${prelude}
559+
model A {
560+
id String @id
561+
x Int
562+
y Int[]
563+
564+
@@allow(true, x == y)
565+
}
566+
`)
567+
).toContain('incompatible operand types');
568+
569+
expect(
570+
await loadModelWithError(`
571+
${prelude}
572+
model A {
573+
id String @id
574+
x Int
575+
y Int[]
576+
577+
@@allow(true, x > y)
578+
}
579+
`)
580+
).toContain('operand cannot be an array');
581+
582+
expect(
583+
await loadModelWithError(`
584+
${prelude}
585+
model User {
586+
id Int @id
587+
foo Foo @relation(fields: [fooId], references: [id])
588+
fooId Int
589+
}
590+
591+
model Foo {
592+
id Int @id
593+
users User[]
594+
595+
@@allow('all', users == auth())
596+
}
597+
`)
598+
).toContain('incompatible operand types');
599+
556600
expect(
557601
await loadModelWithError(`
558602
${prelude}

0 commit comments

Comments
 (0)