Skip to content

fix: Show validation error for the field comparison not in the same model #912

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 5 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import {
ExpressionType,
isDataModel,
isEnum,
isMemberAccessExpr,
isNullExpr,
isThisExpr,
isDataModelField,
isLiteralExpr,
} from '@zenstackhq/language/ast';
import { isDataModelFieldReference } from '@zenstackhq/sdk';
import { isDataModelFieldReference, isEnumFieldReference } from '@zenstackhq/sdk';
import { ValidationAcceptor } from 'langium';
import { isAuthInvocation, isCollectionPredicate } from '../../utils/ast-utils';
import { getContainingDataModel, isAuthInvocation, isCollectionPredicate } from '../../utils/ast-utils';
import { AstValidator } from '../types';
import { typeAssignable } from './utils';

Expand Down Expand Up @@ -124,6 +127,24 @@ export default class ExpressionValidator implements AstValidator<Expression> {
accept('error', 'incompatible operand types', { node: expr });
break;
}
// not supported:
// - foo.a == bar
// - foo.user.id == userId
// except:
// - future().userId == userId
if(isMemberAccessExpr(expr.left) && isDataModelField(expr.left.member.ref) && expr.left.member.ref.$container != getContainingDataModel(expr)
|| isMemberAccessExpr(expr.right) && isDataModelField(expr.right.member.ref) && expr.right.member.ref.$container != getContainingDataModel(expr))
{
// foo.user.id == auth().id
// foo.user.id == "123"
// foo.user.id == null
// foo.user.id == EnumValue
if(!(this.isNotModelFieldExpr(expr.left) || this.isNotModelFieldExpr(expr.right)))
{
accept('error', 'comparison between fields of different models are not supported', { node: expr });
break;
}
}

if (
(expr.left.$resolvedType?.nullable && isNullExpr(expr.right)) ||
Expand Down Expand Up @@ -183,4 +204,15 @@ export default class ExpressionValidator implements AstValidator<Expression> {
}
}
}


private isNotModelFieldExpr(expr: Expression) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是判定expr是不是一个resolve到model field的reference吗?如果是的话,是不是直接正向检查更保险?类似下面这种

!(isReferenceExpr(expr) && isDataModelField(expr.ref)) &&
!(isMemberAccessExpr(expr) && isDataModelField(expr.member.ref))

return isLiteralExpr(expr) || isEnumFieldReference(expr) || isNullExpr(expr) || this.isAuthOrAuthMemberAccess(expr)
}

private isAuthOrAuthMemberAccess(expr: Expression) {
return isAuthInvocation(expr) || (isMemberAccessExpr(expr) && isAuthInvocation(expr.operand));
}

}

16 changes: 2 additions & 14 deletions packages/schema/src/language-server/zmodel-linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
DataModelFieldType,
Enum,
EnumField,
Expression,
ExpressionType,
FunctionDecl,
FunctionParam,
Expand Down Expand Up @@ -53,7 +52,7 @@ import {
} from 'langium';
import { match } from 'ts-pattern';
import { CancellationToken } from 'vscode-jsonrpc';
import { getAllDeclarationsFromImports, isAuthInvocation } from '../utils/ast-utils';
import { getAllDeclarationsFromImports, isAuthInvocation, getContainingDataModel } from '../utils/ast-utils';
import { mapBuiltinTypeToExpressionType } from './validator/utils';

interface DefaultReference extends Reference {
Expand Down Expand Up @@ -292,24 +291,13 @@ export class ZModelLinker extends DefaultLinker {
}
} else if (funcDecl.name === 'future' && isFromStdlib(funcDecl)) {
// future() function is resolved to current model
node.$resolvedType = { decl: this.getContainingDataModel(node) };
node.$resolvedType = { decl: getContainingDataModel(node) };
} else {
this.resolveToDeclaredType(node, funcDecl.returnType);
}
}
}

private getContainingDataModel(node: Expression): DataModel | undefined {
let curr: AstNode | undefined = node.$container;
while (curr) {
if (isDataModel(curr)) {
return curr;
}
curr = curr.$container;
}
return undefined;
}

private resolveLiteral(node: LiteralExpr) {
const type = match<LiteralExpr, ExpressionType>(node)
.when(isStringLiteral, () => 'String')
Expand Down
12 changes: 12 additions & 0 deletions packages/schema/src/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,15 @@ export function getAllDeclarationsFromImports(documents: LangiumDocuments, model
export function isCollectionPredicate(node: AstNode): node is BinaryExpr {
return isBinaryExpr(node) && ['?', '!', '^'].includes(node.operator);
}


export function getContainingDataModel(node: Expression): DataModel | undefined {
let curr: AstNode | undefined = node.$container;
while (curr) {
if (isDataModel(curr)) {
return curr;
}
curr = curr.$container;
}
return undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,35 @@ describe('Attribute tests', () => {
`)
).toContain('comparison between model-typed fields are not supported');

expect(
await loadModelWithError(`
${prelude}
model User {
id Int @id
lists List[]
todos Todo[]
}

model List {
id Int @id
user User @relation(fields: [userId], references: [id])
userId Int
todos Todo[]
}

model Todo {
id Int @id
user User @relation(fields: [userId], references: [id])
userId Int
list List @relation(fields: [listId], references: [id])
listId Int

@@allow('read', list.user.id == userId)
}

`)
).toContain('comparison between fields of different models are not supported');

expect(
await loadModelWithError(`
${prelude}
Expand Down