From c813d21151e01d9c4813862dd2962cdd138396db Mon Sep 17 00:00:00 2001 From: rozetko Date: Wed, 18 Jan 2023 18:51:45 +0300 Subject: [PATCH] better task validation --- src/services/exporter.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/services/exporter.ts b/src/services/exporter.ts index 2654f8d..a1fe347 100644 --- a/src/services/exporter.ts +++ b/src/services/exporter.ts @@ -33,13 +33,14 @@ export class Exporter { this._task.id = uuidv4(); this._task.progress = _.cloneDeep(DEFAULT_PROGRESS); + this._validateQueries(task.queries); + this._validateDatasourceUrl(datasourceUrl); + const targets = task.queries.map((query: DashboardQuery) => new Target( query.panel, query.datasource, )); - this._validateTargets(datasourceUrl, targets); - await this._updateProgress(); const queryConfigs = targets.map( @@ -127,15 +128,22 @@ export class Exporter { } } - private _validateTargets(datasourceUrl: string, targets: Target[]) { - if(!targets || !Array.isArray(targets)) { - throw new Error('Incorrect targets format'); + private _validateQueries(queries: DashboardQuery[]) { + if(!queries || !Array.isArray(queries)) { + throw new Error('`queries` field is required and should be an array'); } - if(targets.length > 1) { - throw new Error(`Multiple queries are not supported yet`); + for(const query of queries) { + if(!_.isEmpty(query.datasource)) { + throw new Error('all queries should have a `datasource` field'); + } + if(!_.isEmpty(query.panel.targets)) { + throw new Error('all queries should have a `panel` field with non-empty `targets` field'); + } } + } + private _validateDatasourceUrl(datasourceUrl: string) { const host = new URL(datasourceUrl).origin; const apiKey = apiKeys[host];