Skip to content

Commit

Permalink
enable array of validations for AstNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesMeierSE authored and spoenemann committed Nov 22, 2023
1 parent 4a963b5 commit 5b203d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/langium/src/validation/validation-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export type ValidationCheck<T extends AstNode = AstNode> = (node: T, accept: Val
export type ValidationChecks<T> = {
[K in keyof T]?: T[K] extends AstNode ? ValidationCheck<T[K]> | Array<ValidationCheck<T[K]>> : never
} & {
AstNode?: ValidationCheck<AstNode>;
AstNode?: ValidationCheck<AstNode> | Array<ValidationCheck<AstNode>>;
}

/**
Expand Down
20 changes: 15 additions & 5 deletions packages/langium/test/validation/document-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ describe('Generic `AstNode` validation applies to all nodes', () => {
grammar
});
const validationChecks: ValidationChecks<object> = {
AstNode: (node, accept) => {
accept('error', 'TEST', { node });
}
// register two different validations for each AstNode
AstNode: [
(node, accept) => {
accept('error', 'TEST', { node });
},
(node, accept) => {
accept('warning', 'Second generic validation', { node });
}
]
};
services.validation.ValidationRegistry.register(validationChecks);
validate = validationHelper(services);
Expand All @@ -95,8 +101,12 @@ describe('Generic `AstNode` validation applies to all nodes', () => {
test('Diagnostics are shown on all elements', async () => {
const validationResult = await validate('a 42');
const diagnostics = validationResult.diagnostics;
// One for each `element`, once on the root model
expect(diagnostics).toHaveLength(3);
// two validations for each AstNode: One for each `element`, once on the root model
expect(diagnostics).toHaveLength(6);
expect(diagnostics.filter(d => d.severity === 1)).toHaveLength(3); // 3 errors
expect(diagnostics.filter(d => d.severity === 2)).toHaveLength(3); // 3 warnings
expect(diagnostics.filter(d => d.severity === 3)).toHaveLength(0);
expect(diagnostics.filter(d => d.severity === 4)).toHaveLength(0);
});

});

0 comments on commit 5b203d2

Please sign in to comment.