Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/language/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ export function getPluginDocuments(model: Model, schemaPath: string): string[] {
}

const provider = getLiteral<string>(providerField.value);
if (!provider) {
if (!provider || typeof provider !== 'string') {
continue;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/language/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
GeneratorDecl,
InvocationExpr,
Model,
Plugin,
Procedure,
TypeDef,
ZModelAstType,
Expand All @@ -17,6 +18,7 @@ import type { ZModelServices } from './module';
import AttributeValidator from './validators/attribute-validator';
import DataModelValidator from './validators/datamodel-validator';
import DataSourceValidator from './validators/datasource-validator';
import PluginValidator from './validators/plugin-validator';
import EnumValidator from './validators/enum-validator';
import ExpressionValidator from './validators/expression-validator';
import FunctionDeclValidator from './validators/function-decl-validator';
Expand All @@ -34,6 +36,7 @@ export function registerValidationChecks(services: ZModelServices) {
const checks: ValidationChecks<ZModelAstType> = {
Model: validator.checkModel,
DataSource: validator.checkDataSource,
Plugin: validator.checkPlugin,
GeneratorDecl: validator.checkGenerator,
DataModel: validator.checkDataModel,
TypeDef: validator.checkTypeDef,
Expand Down Expand Up @@ -96,4 +99,8 @@ export class ZModelValidator {
checkProcedure(node: Procedure, accept: ValidationAcceptor): void {
new ProcedureValidator().validate(node, accept);
}

checkPlugin(node: Plugin, accept: ValidationAcceptor): void {
new PluginValidator().validate(node, accept);
}
}
31 changes: 31 additions & 0 deletions packages/language/src/validators/plugin-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { ValidationAcceptor } from 'langium';
import { Plugin } from '../generated/ast';
import { getStringLiteral } from '../utils';
import { validateDuplicatedDeclarations, type AstValidator } from './common';

/**
* Validates plugin declarations.
*/
export default class PluginValidator implements AstValidator<Plugin> {
validate(plugin: Plugin, accept: ValidationAcceptor): void {
validateDuplicatedDeclarations(plugin, plugin.fields, accept);
this.validateProvider(plugin, accept);
}

private validateProvider(plugin: Plugin, accept: ValidationAcceptor) {
const provider = plugin.fields.find((f) => f.name === 'provider');
if (!provider) {
accept('error', 'plugin must include a "provider" field', {
node: plugin,
});
return;
}

const providerValue = getStringLiteral(provider.value);
if (!providerValue) {
accept('error', '"provider" must be set to a non-empty string literal', {
node: provider.value,
});
}
}
}
119 changes: 119 additions & 0 deletions packages/language/test/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { describe, it } from 'vitest';
import { loadSchema, loadSchemaWithError } from './utils';

describe('Plugin tests', () => {
it('accepts plugins with a string provider', async () => {
await loadSchema(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid())
}

plugin test {
provider = 'test'
}
`,
);
});

it('rejects plugins without a provider', async () => {
await loadSchemaWithError(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid())
}

plugin test {

}
`,
'plugin must include a "provider" field',
);
});

it('rejects plugins with an empty provider', async () => {
await loadSchemaWithError(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid())
}

plugin test {
provider = ''
}
`,
'"provider" must be set to a non-empty string literal',
);
});

it('rejects plugins with a non-string provider', async () => {
await loadSchemaWithError(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid())
}

plugin test {
provider = []
}
`,
'"provider" must be set to a non-empty string literal',
);

await loadSchemaWithError(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid())
}

plugin test {
provider = true
}
`,
'"provider" must be set to a non-empty string literal',
);

await loadSchemaWithError(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid())
}

plugin test {
provider = {}
}
`,
'"provider" must be set to a non-empty string literal',
);
});
});
Loading