| 8 | * Validates toplevel schema. |
| 9 | */ |
| 10 | export default class SchemaValidator implements AstValidator<Model> { |
| 11 | constructor(protected readonly documents: LangiumDocuments) {} |
| 12 | |
| 13 | validate(model: Model, accept: ValidationAcceptor) { |
| 14 | this.validateImports(model, accept); |
| 15 | validateDuplicatedDeclarations(model, model.declarations, accept); |
| 16 | |
| 17 | const importedModels = resolveTransitiveImports(this.documents, model); |
| 18 | |
| 19 | const importedNames = new Set(importedModels.flatMap((m) => m.declarations.map((d) => d.name))); |
| 20 | |
| 21 | for (const declaration of model.declarations) { |
| 22 | if (importedNames.has(declaration.name)) { |
| 23 | accept('error', `A ${declaration.name} already exists in an imported module`, { |
| 24 | node: declaration, |
| 25 | property: 'name', |
| 26 | }); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | if ( |
| 31 | !model.$document?.uri.path.endsWith(STD_LIB_MODULE_NAME) && |
| 32 | !model.$document?.uri.path.endsWith(PLUGIN_MODULE_NAME) |
| 33 | ) { |
| 34 | this.validateDataSources(model, accept); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | private async validateDataSources(model: Model, accept: ValidationAcceptor) { |
| 39 | const dataSources = (await getAllDeclarationsIncludingImports(this.documents, model)).filter((d) => |
| 40 | isDataSource(d), |
| 41 | ); |
| 42 | if (dataSources.length > 1) { |
| 43 | accept('error', 'Multiple datasource declarations are not allowed', { node: dataSources[1]! }); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private validateImports(model: Model, accept: ValidationAcceptor) { |
| 48 | model.imports.forEach((imp) => { |
| 49 | const importedModel = resolveImport(this.documents, imp); |
| 50 | if (!importedModel) { |
| 51 | const importPath = imp.path.endsWith('.zmodel') ? imp.path : `${imp.path}.zmodel`; |
| 52 | accept('error', `Cannot find model file ${importPath}`, { |
| 53 | node: imp, |
| 54 | }); |
| 55 | } |
| 56 | }); |
| 57 | } |
| 58 | } |
nothing calls this directly
no outgoing calls
no test coverage detected