Skip to content

Commit 114567e

Browse files
committed
refactor validators
1 parent 1e4704c commit 114567e

File tree

5 files changed

+27
-39
lines changed

5 files changed

+27
-39
lines changed
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
import { AstNode, type ValidationChecks, ValidationRegistry } from 'langium'
1+
import { ValidationAcceptor, type ValidationChecks, ValidationRegistry } from 'langium'
22
import { ContextMappingModelValidator } from './validation/ContextMappingModelValidator.js'
33
import { ValueValidator } from './validation/ValueValidator.js'
4-
import { AbstractContextMapperValidator } from './validation/AbstractContextMapperValidator.js'
5-
import type { ContextMapperDslAstType } from './generated/ast.js'
6-
7-
const validators: AbstractContextMapperValidator<AstNode>[] = [
8-
new ContextMappingModelValidator(),
9-
new ValueValidator()
10-
]
4+
import type { ContextMapperDslAstType, ContextMappingModel, Value } from './generated/ast.js'
115

126
/**
137
* Register custom validation checks.
148
*/
159
export function registerValidationChecks (registry: ValidationRegistry, validator: ContextMapperDslValidator) {
16-
const validatorChecks: ValidationChecks<ContextMapperDslAstType>[] = []
17-
for (const validator of validators) {
18-
validatorChecks.push(validator.getChecks())
10+
const checks: ValidationChecks<ContextMapperDslAstType> = {
11+
ContextMappingModel: validator.checkContextMappingModel,
12+
Value: validator.checkValue
1913
}
20-
const checks: ValidationChecks<ContextMapperDslAstType> = Object.assign({}, ...validatorChecks)
2114
registry.register(checks, validator)
2215
}
2316

2417
export class ContextMapperDslValidator {
18+
private contextMappingModelValidator = new ContextMappingModelValidator()
19+
private valueValidator = new ValueValidator()
20+
21+
checkContextMappingModel (model: ContextMappingModel, acceptor: ValidationAcceptor) {
22+
this.contextMappingModelValidator.validate(model, acceptor)
23+
}
2524

25+
checkValue (value: Value, acceptor: ValidationAcceptor) {
26+
this.valueValidator.validate(value, acceptor)
27+
}
2628
}

src/language/validation/AbstractContextMapperValidator.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { AstNode, ValidationAcceptor } from 'langium'
2+
3+
export interface ContextMapperValidator<T extends AstNode> {
4+
validate (node: T, acceptor: ValidationAcceptor): void
5+
}

src/language/validation/ContextMappingModelValidator.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import type { ValidationAcceptor, ValidationChecks } from 'langium'
2-
import type { ContextMapperDslAstType, ContextMappingModel } from '../generated/ast.js'
3-
import { AbstractContextMapperValidator } from './AbstractContextMapperValidator.js'
4-
5-
export class ContextMappingModelValidator implements AbstractContextMapperValidator<ContextMappingModel> {
6-
getChecks (): ValidationChecks<ContextMapperDslAstType> {
7-
return {
8-
ContextMappingModel: this.validate
9-
}
10-
}
1+
import type { ValidationAcceptor } from 'langium'
2+
import type { ContextMappingModel } from '../generated/ast.js'
3+
import { ContextMapperValidator } from './ContextMapperValidator.js'
114

5+
export class ContextMappingModelValidator implements ContextMapperValidator<ContextMappingModel> {
126
validate (model: ContextMappingModel, acceptor: ValidationAcceptor): void {
137
checkForZeroOrOneContextMap(model, acceptor)
148
}

src/language/validation/ValueValidator.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import { AbstractContextMapperValidator } from './AbstractContextMapperValidator.js'
2-
import { ContextMapperDslAstType, Value } from '../generated/ast.js'
3-
import { ValidationAcceptor, ValidationChecks } from 'langium'
4-
5-
export class ValueValidator implements AbstractContextMapperValidator<Value> {
6-
getChecks (): ValidationChecks<ContextMapperDslAstType> {
7-
return {
8-
Value: this.validate
9-
}
10-
}
1+
import { ContextMapperValidator } from './ContextMapperValidator.js'
2+
import { Value } from '../generated/ast.js'
3+
import { ValidationAcceptor } from 'langium'
114

5+
export class ValueValidator implements ContextMapperValidator<Value> {
126
validate (node: Value, acceptor: ValidationAcceptor): void {
137
if (node.coreValue.length > 1) {
148
acceptor('error', 'There must be zero or one isCore attribute', {

0 commit comments

Comments
 (0)