|
1 | 1 | import type { ValidationAcceptor } from 'langium'; |
2 | 2 | import { SUPPORTED_PROVIDERS } from '../constants'; |
3 | | -import { DataSource, isConfigArrayExpr, isInvocationExpr, isLiteralExpr } from '../generated/ast'; |
| 3 | +import { DataSource, isConfigArrayExpr, isDataModel, isEnum, isInvocationExpr, isLiteralExpr } from '../generated/ast'; |
4 | 4 | import { getStringLiteral } from '../utils'; |
5 | 5 | import { validateDuplicatedDeclarations, type AstValidator } from './common'; |
6 | 6 |
|
@@ -70,14 +70,28 @@ export default class DataSourceValidator implements AstValidator<DataSource> { |
70 | 70 | accept('error', '"schemas" must be an array of string literals', { |
71 | 71 | node: schemasField, |
72 | 72 | }); |
73 | | - } else if ( |
74 | | - // validate `defaultSchema` is included in `schemas` |
75 | | - defaultSchemaValue && |
76 | | - !schemasValue.items.some((e) => getStringLiteral(e) === defaultSchemaValue) |
77 | | - ) { |
78 | | - accept('error', `"${defaultSchemaValue}" must be included in the "schemas" array`, { |
79 | | - node: schemasField, |
80 | | - }); |
| 73 | + } else { |
| 74 | + const schemasArray = schemasValue.items.map((e) => getStringLiteral(e)!); |
| 75 | + |
| 76 | + if (defaultSchemaValue) { |
| 77 | + // validate `defaultSchema` is included in `schemas` |
| 78 | + if (!schemasArray.includes(defaultSchemaValue)) { |
| 79 | + accept('error', `"${defaultSchemaValue}" must be included in the "schemas" array`, { |
| 80 | + node: schemasField, |
| 81 | + }); |
| 82 | + } |
| 83 | + } else { |
| 84 | + // if no explicit default schema is specified, and there are models or enums without '@@schema', |
| 85 | + // "public" is implicitly used, so it must be included in the "schemas" array |
| 86 | + const hasImplicitPublicSchema = ds.$container.declarations.some( |
| 87 | + (d) => (isDataModel(d) || isEnum(d)) && !d.attributes.some((a) => a.decl.$refText === 'schema'), |
| 88 | + ); |
| 89 | + if (hasImplicitPublicSchema && !schemasArray.includes('public')) { |
| 90 | + accept('error', `"public" must be included in the "schemas" array`, { |
| 91 | + node: schemasField, |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
81 | 95 | } |
82 | 96 | } |
83 | 97 | } |
|
0 commit comments