Skip to content
Merged
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
24 changes: 8 additions & 16 deletions packages/language/src/validators/datasource-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,16 @@ export default class DataSourceValidator implements AstValidator<DataSource> {
}

private validateUrl(ds: DataSource, accept: ValidationAcceptor) {
const url = ds.fields.find((f) => f.name === 'url');
if (!url) {
accept('error', 'datasource must include a "url" field', {
node: ds,
});
const urlField = ds.fields.find((f) => f.name === 'url');
if (!urlField) {
return;
}

for (const fieldName of ['url', 'shadowDatabaseUrl']) {
const field = ds.fields.find((f) => f.name === fieldName);
if (!field) {
continue;
}
const value = getStringLiteral(field.value);
if (!value && !(isInvocationExpr(field.value) && field.value.function.ref?.name === 'env')) {
accept('error', `"${fieldName}" must be set to a string literal or an invocation of "env" function`, {
node: field.value,
});
}
const value = getStringLiteral(urlField.value);
if (!value && !(isInvocationExpr(urlField.value) && urlField.value.function.ref?.name === 'env')) {
accept('error', `"${urlField.name}" must be set to a string literal or an invocation of "env" function`, {
node: urlField.value,
});
}
}

Expand Down
22 changes: 2 additions & 20 deletions packages/sdk/src/ts-schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,32 +559,14 @@ export class TsSchemaGenerator {
);
}

private getDataSourceProvider(
model: Model,
): { type: string; env: undefined; url: string } | { type: string; env: string; url: undefined } {
private getDataSourceProvider(model: Model) {
const dataSource = model.declarations.find(isDataSource);
invariant(dataSource, 'No data source found in the model');

const providerExpr = dataSource.fields.find((f) => f.name === 'provider')?.value;
invariant(isLiteralExpr(providerExpr), 'Provider must be a literal');
const type = providerExpr.value as string;

const urlExpr = dataSource.fields.find((f) => f.name === 'url')?.value;
invariant(isLiteralExpr(urlExpr) || isInvocationExpr(urlExpr), 'URL must be a literal or env function');

if (isLiteralExpr(urlExpr)) {
return { type, url: urlExpr.value as string, env: undefined };
} else if (isInvocationExpr(urlExpr)) {
invariant(urlExpr.function.$refText === 'env', 'only "env" function is supported');
invariant(urlExpr.args.length === 1, 'env function must have one argument');
return {
type,
env: (urlExpr.args[0]!.value as LiteralExpr).value as string,
url: undefined,
};
} else {
throw new Error('Unsupported URL type');
}
return { type };
}

private getFieldMappedDefault(
Expand Down