Line data Source code
1 : import { stringifyError } from '../../platform/logger/logger.js';
2 : import { loadVega } from './vegalite_loader.js';
3 : import { withViewEncodings } from './vegalite_spec.js';
4 :
5 : /// The logger that Vega-Lite reports problems to
6 : type VegaLiteLogger = NonNullable<NonNullable<Parameters<typeof import('vega-lite').compile>[1]>['logger']>;
7 :
8 : /// The problems of a Vega-Lite spec: the errors and warnings of compiling it without data
9 1 : export async function checkVegaLiteSpec(spec: Record<string, unknown>): Promise<string[]> {
10 1 : const { vegaLite } = await loadVega();
11 1 : const problems: string[] = [];
12 1 : const collect = (...message: unknown[]) => {
13 1 : problems.push(message.map(m => (typeof m === 'string' ? m : stringifyError(m))).join(' '));
14 1 : return logger;
15 1 : };
16 1 : const logger: VegaLiteLogger = { level: () => logger, error: collect, warn: collect, info: () => logger, debug: () => logger };
17 1 : try {
18 1 : vegaLite.compile({ ...withViewEncodings(spec), data: { values: [] } } as Parameters<typeof vegaLite.compile>[0], { logger });
19 1 : } catch (e: unknown) {
20 1 : problems.push(stringifyError(e));
21 1 : }
22 1 : return problems;
23 1 : }
|