@opendata-ai/openchart-engine 8.0.0-rc.8 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +149 -12
- package/dist/index.js +894 -171
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
- package/src/__test-fixtures__/specs.ts +0 -182
- package/src/__tests__/__snapshots__/compile-snapshot.test.ts.snap +0 -2339
- package/src/__tests__/annotation-behavior.test.ts +0 -852
- package/src/__tests__/axes.test.ts +0 -1579
- package/src/__tests__/compile-animation.test.ts +0 -161
- package/src/__tests__/compile-chart.test.ts +0 -1303
- package/src/__tests__/compile-footnotes.test.ts +0 -277
- package/src/__tests__/compile-layer.test.ts +0 -969
- package/src/__tests__/compile-snapshot.test.ts +0 -156
- package/src/__tests__/compound-labels.test.ts +0 -147
- package/src/__tests__/dimensions.test.ts +0 -918
- package/src/__tests__/encoding-sugar.test.ts +0 -390
- package/src/__tests__/facet.test.ts +0 -215
- package/src/__tests__/helpers/invariants.ts +0 -299
- package/src/__tests__/known-bugs.test.ts +0 -566
- package/src/__tests__/layout-fuzz.test.ts +0 -358
- package/src/__tests__/layout-invariants.test.ts +0 -535
- package/src/__tests__/legend.test.ts +0 -721
- package/src/__tests__/mark-keys.test.ts +0 -294
- package/src/__tests__/ranking-recipes.test.ts +0 -260
- package/src/__tests__/rotation.test.ts +0 -83
- package/src/__tests__/scales.test.ts +0 -323
- package/src/__tests__/series-search.test.ts +0 -92
- package/src/__tests__/spec-sugar.test.ts +0 -801
- package/src/__tests__/you-draw-it.test.ts +0 -160
- package/src/annotations/__tests__/compute.test.ts +0 -1672
- package/src/annotations/__tests__/geometry.test.ts +0 -66
- package/src/annotations/__tests__/placement-fuzz.test.ts +0 -256
- package/src/annotations/__tests__/placement-invariants.test.ts +0 -345
- package/src/annotations/__tests__/placement.test.ts +0 -316
- package/src/annotations/__tests__/resolve-text.test.ts +0 -788
- package/src/annotations/__tests__/rich-text.test.ts +0 -91
- package/src/annotations/__tests__/thinning.test.ts +0 -285
- package/src/annotations/collisions.ts +0 -326
- package/src/annotations/compute.ts +0 -333
- package/src/annotations/constants.ts +0 -162
- package/src/annotations/geometry.ts +0 -376
- package/src/annotations/move.ts +0 -135
- package/src/annotations/placement.ts +0 -636
- package/src/annotations/position.ts +0 -131
- package/src/annotations/resolve-range.ts +0 -117
- package/src/annotations/resolve-refline.ts +0 -157
- package/src/annotations/resolve-text.ts +0 -545
- package/src/annotations/rich-text.ts +0 -65
- package/src/annotations/thinning.ts +0 -190
- package/src/barlist/__tests__/compile-barlist.test.ts +0 -200
- package/src/barlist/compile-barlist.ts +0 -391
- package/src/barlist/types.ts +0 -28
- package/src/charts/__tests__/post-process.test.ts +0 -237
- package/src/charts/__tests__/registry.test.ts +0 -116
- package/src/charts/__tests__/utils.test.ts +0 -195
- package/src/charts/_shared/__tests__/density-filter.test.ts +0 -32
- package/src/charts/_shared/density-filter.ts +0 -26
- package/src/charts/_shared/format-label-value.ts +0 -16
- package/src/charts/bar/__tests__/compute.test.ts +0 -934
- package/src/charts/bar/__tests__/gradient-orient.test.ts +0 -127
- package/src/charts/bar/__tests__/labels.test.ts +0 -292
- package/src/charts/bar/compute.ts +0 -539
- package/src/charts/bar/index.ts +0 -60
- package/src/charts/bar/labels.ts +0 -311
- package/src/charts/beeswarm/__tests__/compute.test.ts +0 -280
- package/src/charts/beeswarm/__tests__/dodge.test.ts +0 -148
- package/src/charts/beeswarm/compute.ts +0 -203
- package/src/charts/beeswarm/dodge.ts +0 -155
- package/src/charts/beeswarm/index.ts +0 -32
- package/src/charts/builtin.ts +0 -75
- package/src/charts/calendar/__tests__/compute.test.ts +0 -417
- package/src/charts/calendar/compute.ts +0 -319
- package/src/charts/calendar/index.ts +0 -32
- package/src/charts/column/__tests__/compute.test.ts +0 -540
- package/src/charts/column/__tests__/labels.test.ts +0 -179
- package/src/charts/column/compute.ts +0 -493
- package/src/charts/column/index.ts +0 -53
- package/src/charts/column/labels.ts +0 -229
- package/src/charts/dot/__tests__/compute.test.ts +0 -380
- package/src/charts/dot/__tests__/labels.test.ts +0 -98
- package/src/charts/dot/compute.ts +0 -285
- package/src/charts/dot/index.ts +0 -62
- package/src/charts/dot/labels.ts +0 -121
- package/src/charts/line/__tests__/compute.test.ts +0 -1194
- package/src/charts/line/__tests__/curves.test.ts +0 -51
- package/src/charts/line/__tests__/labels.test.ts +0 -94
- package/src/charts/line/__tests__/mark-options.test.ts +0 -175
- package/src/charts/line/area.ts +0 -484
- package/src/charts/line/compute.ts +0 -356
- package/src/charts/line/curves.ts +0 -85
- package/src/charts/line/index.ts +0 -176
- package/src/charts/line/labels.ts +0 -177
- package/src/charts/parliament/__tests__/compute.test.ts +0 -411
- package/src/charts/parliament/compute.ts +0 -313
- package/src/charts/parliament/index.ts +0 -32
- package/src/charts/pie/__tests__/compute.test.ts +0 -384
- package/src/charts/pie/__tests__/labels.test.ts +0 -132
- package/src/charts/pie/compute.ts +0 -324
- package/src/charts/pie/index.ts +0 -49
- package/src/charts/pie/labels.ts +0 -143
- package/src/charts/post-process.ts +0 -229
- package/src/charts/range/__tests__/compute.test.ts +0 -486
- package/src/charts/range/compute.ts +0 -273
- package/src/charts/range/index.ts +0 -82
- package/src/charts/range/labels.ts +0 -169
- package/src/charts/rect/__tests__/compute.test.ts +0 -120
- package/src/charts/rect/index.ts +0 -127
- package/src/charts/registry.ts +0 -72
- package/src/charts/rule/index.ts +0 -139
- package/src/charts/scatter/__tests__/compute.test.ts +0 -488
- package/src/charts/scatter/__tests__/trendline.test.ts +0 -191
- package/src/charts/scatter/compute.ts +0 -192
- package/src/charts/scatter/index.ts +0 -46
- package/src/charts/scatter/trendline.ts +0 -100
- package/src/charts/text/__tests__/compute.test.ts +0 -307
- package/src/charts/text/index.ts +0 -141
- package/src/charts/tick/__tests__/compute.test.ts +0 -75
- package/src/charts/tick/index.ts +0 -99
- package/src/charts/utils.ts +0 -170
- package/src/charts/waffle/__tests__/compute.test.ts +0 -305
- package/src/charts/waffle/compute.ts +0 -197
- package/src/charts/waffle/index.ts +0 -31
- package/src/compile/__tests__/color-scale-range.test.ts +0 -158
- package/src/compile/__tests__/color-scale-reverse.test.ts +0 -89
- package/src/compile/__tests__/data-clip.test.ts +0 -127
- package/src/compile/__tests__/fill-patterns.test.ts +0 -154
- package/src/compile/__tests__/size-scale.test.ts +0 -108
- package/src/compile/__tests__/watermark-obstacle.test.ts +0 -93
- package/src/compile/color-scale-range.ts +0 -101
- package/src/compile/data-clip.ts +0 -48
- package/src/compile/fill-patterns.ts +0 -74
- package/src/compile/layer.ts +0 -626
- package/src/compile/size-scale.ts +0 -141
- package/src/compile/spec-sugar.ts +0 -758
- package/src/compile/watermark-obstacle.ts +0 -54
- package/src/compile/you-draw-it.ts +0 -123
- package/src/compile.ts +0 -1450
- package/src/compiler/__tests__/a11y-warnings.test.ts +0 -148
- package/src/compiler/__tests__/animation.test.ts +0 -177
- package/src/compiler/__tests__/compile.test.ts +0 -87
- package/src/compiler/__tests__/fixtures/vl-parity/axis-null.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/color-value.json +0 -20
- package/src/compiler/__tests__/fixtures/vl-parity/count-aggregate.json +0 -22
- package/src/compiler/__tests__/fixtures/vl-parity/data-values.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/layer-axis-null.json +0 -14
- package/src/compiler/__tests__/fixtures/vl-parity/layer-count.json +0 -34
- package/src/compiler/__tests__/fixtures/vl-parity/layer-data-values.json +0 -22
- package/src/compiler/__tests__/fixtures/vl-parity/layer-legend-null.json +0 -41
- package/src/compiler/__tests__/fixtures/vl-parity/layer-scheme.json +0 -38
- package/src/compiler/__tests__/fixtures/vl-parity/layer-sort.json +0 -28
- package/src/compiler/__tests__/fixtures/vl-parity/layer-theta.json +0 -28
- package/src/compiler/__tests__/fixtures/vl-parity/layer-title.json +0 -30
- package/src/compiler/__tests__/fixtures/vl-parity/layer-value-def.json +0 -29
- package/src/compiler/__tests__/fixtures/vl-parity/legend-null.json +0 -32
- package/src/compiler/__tests__/fixtures/vl-parity/scheme.json +0 -25
- package/src/compiler/__tests__/fixtures/vl-parity/sort-by-value.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/theta-arc.json +0 -19
- package/src/compiler/__tests__/fixtures/vl-parity/title-subtitle.json +0 -21
- package/src/compiler/__tests__/fixtures/vl-parity/width-height.json +0 -23
- package/src/compiler/__tests__/keys.test.ts +0 -83
- package/src/compiler/__tests__/normalize-highlight.test.ts +0 -103
- package/src/compiler/__tests__/normalize.test.ts +0 -399
- package/src/compiler/__tests__/sparkline-defaults.test.ts +0 -153
- package/src/compiler/__tests__/validate-did-you-mean.test.ts +0 -170
- package/src/compiler/__tests__/validate-highlight.test.ts +0 -112
- package/src/compiler/__tests__/validate-near-miss.test.ts +0 -251
- package/src/compiler/__tests__/validate.test.ts +0 -629
- package/src/compiler/__tests__/vl-parity.test.ts +0 -68
- package/src/compiler/a11y-warnings.ts +0 -110
- package/src/compiler/animation.ts +0 -142
- package/src/compiler/index.ts +0 -47
- package/src/compiler/keys.ts +0 -55
- package/src/compiler/normalize.ts +0 -734
- package/src/compiler/sparkline-defaults.ts +0 -138
- package/src/compiler/types.ts +0 -241
- package/src/compiler/validate.ts +0 -2041
- package/src/endpoint-labels/__tests__/compute.test.ts +0 -587
- package/src/endpoint-labels/compute.ts +0 -621
- package/src/endpoint-labels/constants.ts +0 -54
- package/src/endpoint-labels/format.ts +0 -60
- package/src/endpoint-labels/predict.ts +0 -139
- package/src/format/__tests__/field-format.test.ts +0 -164
- package/src/format/field-format.ts +0 -27
- package/src/geo/__tests__/compile-map.test.ts +0 -1004
- package/src/geo/compile-map.ts +0 -1341
- package/src/geo/join.ts +0 -115
- package/src/geo/projections.ts +0 -45
- package/src/geo/types.ts +0 -26
- package/src/graphs/__tests__/community.test.ts +0 -228
- package/src/graphs/__tests__/compile-graph.test.ts +0 -396
- package/src/graphs/__tests__/encoding.test.ts +0 -415
- package/src/graphs/community.ts +0 -92
- package/src/graphs/compile-graph.ts +0 -329
- package/src/graphs/encoding.ts +0 -356
- package/src/graphs/types.ts +0 -106
- package/src/index.ts +0 -177
- package/src/layout/__tests__/metrics.test.ts +0 -97
- package/src/layout/__tests__/scales-highlight.test.ts +0 -192
- package/src/layout/axes/rotation.ts +0 -152
- package/src/layout/axes/thinning.ts +0 -98
- package/src/layout/axes/ticks.ts +0 -409
- package/src/layout/axes.ts +0 -757
- package/src/layout/dimensions.ts +0 -912
- package/src/layout/facet.ts +0 -91
- package/src/layout/gridlines.ts +0 -84
- package/src/layout/metrics.ts +0 -132
- package/src/layout/plan.ts +0 -631
- package/src/layout/scales.ts +0 -1053
- package/src/layout/shared.ts +0 -44
- package/src/legend/__tests__/continuous.test.ts +0 -318
- package/src/legend/__tests__/redundancy.test.ts +0 -108
- package/src/legend/__tests__/size.test.ts +0 -348
- package/src/legend/__tests__/suppression.test.ts +0 -382
- package/src/legend/compute.ts +0 -747
- package/src/legend/continuous.ts +0 -327
- package/src/legend/size.ts +0 -407
- package/src/legend/suppression.ts +0 -226
- package/src/legend/wrap.ts +0 -117
- package/src/sankey/__tests__/compile-sankey.test.ts +0 -458
- package/src/sankey/__tests__/layout.test.ts +0 -165
- package/src/sankey/__tests__/node-label-wrap.test.ts +0 -114
- package/src/sankey/__tests__/node-sort.test.ts +0 -45
- package/src/sankey/compile-sankey.ts +0 -706
- package/src/sankey/layout.ts +0 -184
- package/src/sankey/types.ts +0 -41
- package/src/tables/__tests__/bar-column.test.ts +0 -147
- package/src/tables/__tests__/category-colors.test.ts +0 -230
- package/src/tables/__tests__/compile-table.test.ts +0 -236
- package/src/tables/__tests__/format-cells.test.ts +0 -143
- package/src/tables/__tests__/heatmap.test.ts +0 -148
- package/src/tables/__tests__/pagination.test.ts +0 -78
- package/src/tables/__tests__/search.test.ts +0 -94
- package/src/tables/__tests__/sort.test.ts +0 -107
- package/src/tables/__tests__/sparkline.test.ts +0 -122
- package/src/tables/bar-column.ts +0 -94
- package/src/tables/category-colors.ts +0 -78
- package/src/tables/compile-table.ts +0 -427
- package/src/tables/format-cells.ts +0 -115
- package/src/tables/heatmap.ts +0 -141
- package/src/tables/pagination.ts +0 -46
- package/src/tables/search.ts +0 -66
- package/src/tables/sort.ts +0 -69
- package/src/tables/sparkline.ts +0 -113
- package/src/tables/utils.ts +0 -16
- package/src/tilemap/__tests__/compile-tilemap.test.ts +0 -576
- package/src/tilemap/compile-tilemap.ts +0 -755
- package/src/tilemap/layout.ts +0 -172
- package/src/tilemap/types.ts +0 -33
- package/src/tooltips/__tests__/compute.test.ts +0 -582
- package/src/tooltips/compute.ts +0 -700
- package/src/transforms/__tests__/aggregate.test.ts +0 -159
- package/src/transforms/__tests__/bin.test.ts +0 -88
- package/src/transforms/__tests__/calculate.test.ts +0 -146
- package/src/transforms/__tests__/conditional.test.ts +0 -127
- package/src/transforms/__tests__/filter-relative.test.ts +0 -202
- package/src/transforms/__tests__/filter.test.ts +0 -59
- package/src/transforms/__tests__/fold.test.ts +0 -79
- package/src/transforms/__tests__/index.test.ts +0 -93
- package/src/transforms/__tests__/predicates.test.ts +0 -193
- package/src/transforms/__tests__/timeunit.test.ts +0 -129
- package/src/transforms/__tests__/window.test.ts +0 -286
- package/src/transforms/aggregate.ts +0 -131
- package/src/transforms/bin.ts +0 -87
- package/src/transforms/calculate.ts +0 -60
- package/src/transforms/conditional.ts +0 -46
- package/src/transforms/filter.ts +0 -122
- package/src/transforms/fold.ts +0 -49
- package/src/transforms/index.ts +0 -60
- package/src/transforms/predicates.ts +0 -123
- package/src/transforms/timeunit.ts +0 -88
- package/src/transforms/window.ts +0 -185
package/src/compiler/validate.ts
DELETED
|
@@ -1,2041 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Runtime spec validation.
|
|
3
|
-
*
|
|
4
|
-
* TypeScript catches compile-time errors for specs written in code.
|
|
5
|
-
* This module catches runtime errors for specs coming from JSON, APIs,
|
|
6
|
-
* or Claude-generated output where the TypeScript compiler can't help.
|
|
7
|
-
*
|
|
8
|
-
* Every error includes a machine-readable code and an actionable suggestion
|
|
9
|
-
* so consumers (and LLMs) can fix issues programmatically.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
type FieldType,
|
|
14
|
-
inferFieldType,
|
|
15
|
-
MARK_ENCODING_RULES,
|
|
16
|
-
MARK_TYPES,
|
|
17
|
-
type MarkType,
|
|
18
|
-
resolveSchemeName,
|
|
19
|
-
SEQUENTIAL_PALETTES,
|
|
20
|
-
SUPPORTED_SCHEME_NAMES,
|
|
21
|
-
type VizSpec,
|
|
22
|
-
} from '@opendata-ai/openchart-core';
|
|
23
|
-
|
|
24
|
-
import type { ValidationError, ValidationResult } from './types';
|
|
25
|
-
|
|
26
|
-
// ---------------------------------------------------------------------------
|
|
27
|
-
// Helpers
|
|
28
|
-
// ---------------------------------------------------------------------------
|
|
29
|
-
|
|
30
|
-
const VALID_FIELD_TYPES = new Set<string>(['quantitative', 'temporal', 'nominal', 'ordinal']);
|
|
31
|
-
|
|
32
|
-
const VALID_DARK_MODES = new Set<string>(['auto', 'force', 'off']);
|
|
33
|
-
|
|
34
|
-
/** Check if a value looks like a parseable date. */
|
|
35
|
-
function isParseableDate(value: unknown): boolean {
|
|
36
|
-
if (value instanceof Date) return !Number.isNaN(value.getTime());
|
|
37
|
-
if (typeof value === 'string') {
|
|
38
|
-
const d = new Date(value);
|
|
39
|
-
return !Number.isNaN(d.getTime());
|
|
40
|
-
}
|
|
41
|
-
if (typeof value === 'number') return true;
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Check if a value is numeric. */
|
|
46
|
-
function isNumeric(value: unknown): boolean {
|
|
47
|
-
if (typeof value === 'number') return Number.isFinite(value);
|
|
48
|
-
if (typeof value === 'string') {
|
|
49
|
-
const n = Number(value);
|
|
50
|
-
return !Number.isNaN(n) && Number.isFinite(n);
|
|
51
|
-
}
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Levenshtein edit distance between two strings (case-insensitive). Used to
|
|
57
|
-
* turn a misspelled field reference into a "did you mean" repair hint.
|
|
58
|
-
*/
|
|
59
|
-
function editDistance(a: string, b: string): number {
|
|
60
|
-
const s = a.toLowerCase();
|
|
61
|
-
const t = b.toLowerCase();
|
|
62
|
-
const m = s.length;
|
|
63
|
-
const n = t.length;
|
|
64
|
-
if (m === 0) return n;
|
|
65
|
-
if (n === 0) return m;
|
|
66
|
-
// Single-row DP: prev[j] holds distance for the previous source char.
|
|
67
|
-
let prev = Array.from({ length: n + 1 }, (_, j) => j);
|
|
68
|
-
let curr = new Array<number>(n + 1);
|
|
69
|
-
for (let i = 1; i <= m; i++) {
|
|
70
|
-
curr[0] = i;
|
|
71
|
-
for (let j = 1; j <= n; j++) {
|
|
72
|
-
const cost = s[i - 1] === t[j - 1] ? 0 : 1;
|
|
73
|
-
curr[j] = Math.min(prev[j] + 1, curr[j - 1] + 1, prev[j - 1] + cost);
|
|
74
|
-
}
|
|
75
|
-
[prev, curr] = [curr, prev];
|
|
76
|
-
}
|
|
77
|
-
return prev[n];
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Given a misspelled field name and the actual data columns, return the nearest
|
|
82
|
-
* column when it is close enough to be a plausible typo, else undefined.
|
|
83
|
-
*
|
|
84
|
-
* The threshold scales with word length (at most ~40% of the target length, and
|
|
85
|
-
* never more than 3 edits) so short columns don't match unrelated names.
|
|
86
|
-
*/
|
|
87
|
-
function nearestColumn(field: string, columns: string[]): string | undefined {
|
|
88
|
-
let best: string | undefined;
|
|
89
|
-
let bestDistance = Number.POSITIVE_INFINITY;
|
|
90
|
-
for (const column of columns) {
|
|
91
|
-
const distance = editDistance(field, column);
|
|
92
|
-
if (distance < bestDistance) {
|
|
93
|
-
bestDistance = distance;
|
|
94
|
-
best = column;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
if (best === undefined) return undefined;
|
|
98
|
-
const threshold = Math.min(3, Math.max(1, Math.floor(best.length * 0.4)));
|
|
99
|
-
return bestDistance <= threshold ? best : undefined;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Build the "did you mean" clause appended to DATA_FIELD_MISSING suggestions.
|
|
104
|
-
* Returns an empty string when no column is a plausible near-match.
|
|
105
|
-
*/
|
|
106
|
-
function didYouMean(field: string, columns: string[]): string {
|
|
107
|
-
const match = nearestColumn(field, columns);
|
|
108
|
-
return match ? ` Did you mean "${match}"?` : '';
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Push an INVALID_VALUE error when a channel carries an unrecognized
|
|
113
|
-
* scale.scheme name. Chart specs are the only family that consumes schemes
|
|
114
|
-
* this way: known names (including VL aliases) resolve via the core palette
|
|
115
|
-
* registry and are expanded to scale.range by the pre-validation sugar pass,
|
|
116
|
-
* so `resolveSchemeName` failing means the name is unknown.
|
|
117
|
-
*/
|
|
118
|
-
function checkSchemeName(
|
|
119
|
-
channelObj: Record<string, unknown>,
|
|
120
|
-
channelPath: string,
|
|
121
|
-
errors: ValidationError[],
|
|
122
|
-
): void {
|
|
123
|
-
const scale = channelObj.scale as Record<string, unknown> | undefined;
|
|
124
|
-
if (scale && typeof scale.scheme === 'string' && !resolveSchemeName(scale.scheme)) {
|
|
125
|
-
errors.push({
|
|
126
|
-
message: `Spec error: ${channelPath}.scale.scheme "${scale.scheme}" is not a supported scheme name`,
|
|
127
|
-
path: `${channelPath}.scale.scheme`,
|
|
128
|
-
code: 'INVALID_VALUE',
|
|
129
|
-
suggestion: `Use one of the supported scheme names: ${[...SUPPORTED_SCHEME_NAMES].join(', ')}. Or provide explicit colors via scale.range.`,
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Push an INVALID_VALUE error when any channel of an encoding block carries a
|
|
136
|
-
* scale.scheme on a spec family whose compile path never reads it. Even a
|
|
137
|
-
* known scheme name is dead config there — silently accepting it would leave
|
|
138
|
-
* the author believing they chose the colors.
|
|
139
|
-
*/
|
|
140
|
-
function checkSchemeUnused(
|
|
141
|
-
encoding: Record<string, unknown>,
|
|
142
|
-
errors: ValidationError[],
|
|
143
|
-
familyNote: string,
|
|
144
|
-
suggestion: string,
|
|
145
|
-
): void {
|
|
146
|
-
for (const [channel, ch] of Object.entries(encoding)) {
|
|
147
|
-
if (!ch || typeof ch !== 'object' || Array.isArray(ch)) continue;
|
|
148
|
-
const scale = (ch as Record<string, unknown>).scale as Record<string, unknown> | undefined;
|
|
149
|
-
if (scale && typeof scale.scheme === 'string') {
|
|
150
|
-
errors.push({
|
|
151
|
-
message: `Spec error: encoding.${channel}.scale.scheme has no effect — ${familyNote}`,
|
|
152
|
-
path: `encoding.${channel}.scale.scheme`,
|
|
153
|
-
code: 'INVALID_VALUE',
|
|
154
|
-
suggestion,
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/** Palette names the map compile path actually supports (sequential only). */
|
|
161
|
-
const MAP_PALETTE_NAMES = Object.keys(SEQUENTIAL_PALETTES).join(', ');
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Map color channels resolve scale.scheme against SEQUENTIAL_PALETTES with a
|
|
165
|
-
* silent fallback to blue — a typo'd scheme would render the default palette
|
|
166
|
-
* with no signal. Reject names outside the map's supported set here instead.
|
|
167
|
-
* Categorical channels never read schemes at all (colors come from
|
|
168
|
-
* scale.domain/scale.range), so any scheme there is dead config.
|
|
169
|
-
*/
|
|
170
|
-
function checkMapSchemeName(
|
|
171
|
-
channelObj: Record<string, unknown>,
|
|
172
|
-
channelPath: string,
|
|
173
|
-
errors: ValidationError[],
|
|
174
|
-
): void {
|
|
175
|
-
const scale = channelObj.scale as Record<string, unknown> | undefined;
|
|
176
|
-
if (!scale || typeof scale.scheme !== 'string') return;
|
|
177
|
-
|
|
178
|
-
if (channelObj.type === 'nominal' || channelObj.type === 'ordinal') {
|
|
179
|
-
errors.push({
|
|
180
|
-
message: `Spec error: ${channelPath}.scale.scheme has no effect — categorical map colors come from scale.range and scale.domain`,
|
|
181
|
-
path: `${channelPath}.scale.scheme`,
|
|
182
|
-
code: 'INVALID_VALUE',
|
|
183
|
-
suggestion:
|
|
184
|
-
'Remove scale.scheme. To customize categorical map colors, provide explicit colors via scale.range (with scale.domain for category order).',
|
|
185
|
-
});
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (!(scale.scheme in SEQUENTIAL_PALETTES)) {
|
|
190
|
-
errors.push({
|
|
191
|
-
message: `Spec error: ${channelPath}.scale.scheme "${scale.scheme}" is not a supported map palette`,
|
|
192
|
-
path: `${channelPath}.scale.scheme`,
|
|
193
|
-
code: 'INVALID_VALUE',
|
|
194
|
-
suggestion: `Maps support the sequential palettes: ${MAP_PALETTE_NAMES}. The default is "blue".`,
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// ---------------------------------------------------------------------------
|
|
200
|
-
// Range mark validation
|
|
201
|
-
// ---------------------------------------------------------------------------
|
|
202
|
-
|
|
203
|
-
const VALID_RANGE_STYLES = new Set(['dumbbell', 'arrow', 'bar']);
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Range marks need an orientation-dependent encoding pair: x + x2 when the
|
|
207
|
-
* category axis is y (horizontal, the common editorial form), or y + y2 when
|
|
208
|
-
* the category axis is x (vertical). The static MARK_ENCODING_RULES table
|
|
209
|
-
* can't express "x2 required only when y is categorical", so this check
|
|
210
|
-
* enforces it with errors naming exactly what to add.
|
|
211
|
-
*/
|
|
212
|
-
function validateRangeSpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
213
|
-
const encoding = spec.encoding as Record<string, Record<string, unknown> | undefined>;
|
|
214
|
-
const data = spec.data as Record<string, unknown>[];
|
|
215
|
-
const x = encoding.x;
|
|
216
|
-
const y = encoding.y;
|
|
217
|
-
// Missing x/y already produced MISSING_FIELD errors from the generic loop.
|
|
218
|
-
if (!x?.field || typeof x.field !== 'string' || !y?.field || typeof y.field !== 'string') {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const xType = (x.type as string | undefined) ?? inferFieldType(data, x.field);
|
|
223
|
-
const yType = (y.type as string | undefined) ?? inferFieldType(data, y.field);
|
|
224
|
-
const xIsCategory = xType === 'nominal' || xType === 'ordinal';
|
|
225
|
-
const yIsCategory = yType === 'nominal' || yType === 'ordinal';
|
|
226
|
-
|
|
227
|
-
if (xIsCategory && yIsCategory) {
|
|
228
|
-
errors.push({
|
|
229
|
-
message:
|
|
230
|
-
'Spec error: range chart requires a quantitative value axis, but both encoding.x and encoding.y are categorical',
|
|
231
|
-
path: 'encoding',
|
|
232
|
-
code: 'ENCODING_MISMATCH',
|
|
233
|
-
suggestion:
|
|
234
|
-
'Map the range start to a quantitative channel: x (with x2 for the end) for horizontal ranges, or y (with y2) for vertical ranges',
|
|
235
|
-
});
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
if (!xIsCategory && !yIsCategory) {
|
|
239
|
-
errors.push({
|
|
240
|
-
message:
|
|
241
|
-
'Spec error: range chart requires a nominal or ordinal category axis, but neither encoding.x nor encoding.y is categorical',
|
|
242
|
-
path: 'encoding',
|
|
243
|
-
code: 'ENCODING_MISMATCH',
|
|
244
|
-
suggestion:
|
|
245
|
-
'Set encoding.y to a nominal field for horizontal ranges (the common form), or encoding.x for vertical ranges',
|
|
246
|
-
});
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const availableColumns = data.length > 0 ? Object.keys(data[0]).join(', ') : '';
|
|
251
|
-
if (yIsCategory) {
|
|
252
|
-
// Horizontal: x is the range start, x2 the end.
|
|
253
|
-
if (!encoding.x2) {
|
|
254
|
-
errors.push({
|
|
255
|
-
message:
|
|
256
|
-
'Spec error: range chart requires encoding.x2 (the range end) when encoding.y is the category axis, but none was provided',
|
|
257
|
-
path: 'encoding.x2',
|
|
258
|
-
code: 'MISSING_FIELD',
|
|
259
|
-
suggestion: `Add encoding.x2 with the field holding the range's end value, e.g. x2: { field: "endValue" }. Available columns: ${availableColumns}`,
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
if (encoding.y2) {
|
|
263
|
-
errors.push({
|
|
264
|
-
message:
|
|
265
|
-
'Spec error: encoding.y2 is not used on a horizontal range chart (category axis on y); the range end belongs on x2',
|
|
266
|
-
path: 'encoding.y2',
|
|
267
|
-
code: 'INVALID_VALUE',
|
|
268
|
-
suggestion:
|
|
269
|
-
'Remove encoding.y2, or move the category to encoding.x and the values to y/y2 for a vertical range chart',
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
} else {
|
|
273
|
-
// Vertical: y is the range start, y2 the end.
|
|
274
|
-
if (!encoding.y2) {
|
|
275
|
-
errors.push({
|
|
276
|
-
message:
|
|
277
|
-
'Spec error: range chart requires encoding.y2 (the range end) when encoding.x is the category axis, but none was provided',
|
|
278
|
-
path: 'encoding.y2',
|
|
279
|
-
code: 'MISSING_FIELD',
|
|
280
|
-
suggestion: `Add encoding.y2 with the field holding the range's end value, e.g. y2: { field: "endValue" }. Available columns: ${availableColumns}`,
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
if (encoding.x2) {
|
|
284
|
-
errors.push({
|
|
285
|
-
message:
|
|
286
|
-
'Spec error: encoding.x2 is not used on a vertical range chart (category axis on x); the range end belongs on y2',
|
|
287
|
-
path: 'encoding.x2',
|
|
288
|
-
code: 'INVALID_VALUE',
|
|
289
|
-
suggestion:
|
|
290
|
-
'Remove encoding.x2, or move the category to encoding.y and the values to x/x2 for a horizontal range chart',
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// markDef options: style must be one of the known variants.
|
|
296
|
-
const mark = spec.mark;
|
|
297
|
-
if (mark && typeof mark === 'object' && !Array.isArray(mark)) {
|
|
298
|
-
const style = (mark as Record<string, unknown>).style;
|
|
299
|
-
if (style !== undefined && !VALID_RANGE_STYLES.has(style as string)) {
|
|
300
|
-
errors.push({
|
|
301
|
-
message: `Spec error: mark.style "${style}" is not valid for range marks. Must be one of: dumbbell, arrow, bar`,
|
|
302
|
-
path: 'mark.style',
|
|
303
|
-
code: 'INVALID_VALUE',
|
|
304
|
-
suggestion:
|
|
305
|
-
'Use mark: { type: "range", style: "dumbbell" } (default), "arrow" for directional change, or "bar" for a plain floating range bar',
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// ---------------------------------------------------------------------------
|
|
312
|
-
// Calendar mark validation
|
|
313
|
-
// ---------------------------------------------------------------------------
|
|
314
|
-
|
|
315
|
-
const VALID_WEEK_STARTS = new Set(['monday', 'sunday']);
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Calendar heatmaps take daily dates on x and a quantitative value on color;
|
|
319
|
-
* the weeks-x-weekdays geometry is computed internally, so there is no y
|
|
320
|
-
* channel. This check enforces the parts the static MARK_ENCODING_RULES
|
|
321
|
-
* table can't express: no y, a temporal x even when the type is inferred,
|
|
322
|
-
* every date parseable, one row per day (daily granularity), and valid
|
|
323
|
-
* mark-level weekStart/cellRadius options.
|
|
324
|
-
*/
|
|
325
|
-
function validateCalendarSpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
326
|
-
const encoding = spec.encoding as Record<string, Record<string, unknown> | undefined>;
|
|
327
|
-
const data = spec.data as Record<string, unknown>[];
|
|
328
|
-
|
|
329
|
-
if (encoding.y) {
|
|
330
|
-
errors.push({
|
|
331
|
-
message:
|
|
332
|
-
'Spec error: calendar chart does not accept encoding.y (the weeks x weekdays layout is computed from the dates)',
|
|
333
|
-
path: 'encoding.y',
|
|
334
|
-
code: 'ENCODING_MISMATCH',
|
|
335
|
-
suggestion:
|
|
336
|
-
'Remove encoding.y. A calendar needs only x (temporal, daily dates) and color (quantitative).',
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// Mark-level options (weekStart / cellRadius) on the MarkDef object form.
|
|
341
|
-
if (spec.mark && typeof spec.mark === 'object') {
|
|
342
|
-
const markDef = spec.mark as Record<string, unknown>;
|
|
343
|
-
if (markDef.weekStart !== undefined && !VALID_WEEK_STARTS.has(markDef.weekStart as string)) {
|
|
344
|
-
errors.push({
|
|
345
|
-
message: `Spec error: mark.weekStart "${markDef.weekStart}" is not a valid week start`,
|
|
346
|
-
path: 'mark.weekStart',
|
|
347
|
-
code: 'INVALID_VALUE',
|
|
348
|
-
suggestion:
|
|
349
|
-
'Use mark: { type: "calendar", weekStart: "monday" } (default, ISO weeks) or "sunday"',
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
if (
|
|
353
|
-
markDef.cellRadius !== undefined &&
|
|
354
|
-
(typeof markDef.cellRadius !== 'number' ||
|
|
355
|
-
!Number.isFinite(markDef.cellRadius) ||
|
|
356
|
-
markDef.cellRadius < 0)
|
|
357
|
-
) {
|
|
358
|
-
errors.push({
|
|
359
|
-
message: `Spec error: mark.cellRadius must be a non-negative number, got ${JSON.stringify(markDef.cellRadius)}`,
|
|
360
|
-
path: 'mark.cellRadius',
|
|
361
|
-
code: 'INVALID_VALUE',
|
|
362
|
-
suggestion:
|
|
363
|
-
'Use a pixel radius like cellRadius: 1 (default) or cellRadius: 0 for square corners',
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
const x = encoding.x;
|
|
369
|
-
// Missing x already produced a MISSING_FIELD error from the generic loop.
|
|
370
|
-
if (!x?.field || typeof x.field !== 'string') return;
|
|
371
|
-
|
|
372
|
-
// The generic loop rejects a declared non-temporal type against the
|
|
373
|
-
// encoding rules table; this catches the inferred case (type omitted).
|
|
374
|
-
const xType = (x.type as string | undefined) ?? inferFieldType(data, x.field);
|
|
375
|
-
if (xType !== 'temporal') {
|
|
376
|
-
if (!x.type) {
|
|
377
|
-
errors.push({
|
|
378
|
-
message: `Spec error: calendar chart requires temporal encoding.x, but "${x.field}" was inferred as ${xType}`,
|
|
379
|
-
path: 'encoding.x',
|
|
380
|
-
code: 'ENCODING_MISMATCH',
|
|
381
|
-
suggestion: `Provide daily dates in "${x.field}" (ISO 8601 strings like "2024-01-15" or Date objects) and declare x: { field: "${x.field}", type: "temporal" }`,
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
return;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// Transforms (e.g. a timeUnit aggregation) reshape the data before the
|
|
388
|
-
// calendar sees it, so per-row date checks only apply to untransformed specs.
|
|
389
|
-
if (Array.isArray(spec.transform) && spec.transform.length > 0) return;
|
|
390
|
-
|
|
391
|
-
// Every date must parse; report the first offender by row index.
|
|
392
|
-
for (let i = 0; i < data.length; i++) {
|
|
393
|
-
const value = data[i][x.field];
|
|
394
|
-
if (value != null && !isParseableDate(value)) {
|
|
395
|
-
errors.push({
|
|
396
|
-
message: `Spec error: encoding.x.field "${x.field}" contains an unparseable date at data[${i}]: ${JSON.stringify(value)}`,
|
|
397
|
-
path: `data[${i}].${x.field}`,
|
|
398
|
-
code: 'INVALID_VALUE',
|
|
399
|
-
suggestion: `Fix data[${i}].${x.field} to a parseable date (ISO 8601 strings like "2024-01-15" or Date objects)`,
|
|
400
|
-
});
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// Daily granularity: more than one row on the same UTC day means the data
|
|
406
|
-
// is sub-daily (or duplicated) and cells would silently overwrite.
|
|
407
|
-
const seenDays = new Map<string, number>();
|
|
408
|
-
for (let i = 0; i < data.length; i++) {
|
|
409
|
-
const value = data[i][x.field];
|
|
410
|
-
if (value == null) continue;
|
|
411
|
-
const date = value instanceof Date ? value : new Date(value as string | number);
|
|
412
|
-
if (Number.isNaN(date.getTime())) continue;
|
|
413
|
-
const dayKey = `${date.getUTCFullYear()}-${date.getUTCMonth()}-${date.getUTCDate()}`;
|
|
414
|
-
const firstIndex = seenDays.get(dayKey);
|
|
415
|
-
if (firstIndex !== undefined) {
|
|
416
|
-
errors.push({
|
|
417
|
-
message: `Spec error: calendar chart requires daily granularity, but data[${firstIndex}] and data[${i}] fall on the same day (${JSON.stringify(value)})`,
|
|
418
|
-
path: `data[${i}].${x.field}`,
|
|
419
|
-
code: 'ENCODING_MISMATCH',
|
|
420
|
-
suggestion: `Aggregate to one row per day first, e.g. transform: [{ timeUnit: "yearmonthdate", field: "${x.field}", as: "day" }] plus an aggregate over the value field, or pre-aggregate the data.`,
|
|
421
|
-
});
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
seenDays.set(dayKey, i);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
// ---------------------------------------------------------------------------
|
|
429
|
-
// Chart validation
|
|
430
|
-
// ---------------------------------------------------------------------------
|
|
431
|
-
|
|
432
|
-
function validateChartSpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
433
|
-
const markType =
|
|
434
|
-
typeof spec.mark === 'string' ? spec.mark : (spec.mark as Record<string, unknown>)?.type;
|
|
435
|
-
|
|
436
|
-
// Check data
|
|
437
|
-
if (!Array.isArray(spec.data)) {
|
|
438
|
-
// Near-miss: the VL `data: { url }` form. Fetching is out of scope for a
|
|
439
|
-
// rendering library, so reject it with a pointed message. The `{ values }`
|
|
440
|
-
// form is unwrapped by the pre-validation sugar expansion and never
|
|
441
|
-
// reaches this check on the compile path.
|
|
442
|
-
if (spec.data && typeof spec.data === 'object' && 'url' in (spec.data as object)) {
|
|
443
|
-
errors.push({
|
|
444
|
-
message: 'Spec error: data.url is not supported (openchart does not fetch remote data)',
|
|
445
|
-
path: 'data.url',
|
|
446
|
-
code: 'INVALID_VALUE',
|
|
447
|
-
suggestion:
|
|
448
|
-
'Fetch the data in your application and provide the rows inline: data: [...] or data: { values: [...] }',
|
|
449
|
-
});
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
errors.push({
|
|
453
|
-
message: 'Spec error: "data" must be an array',
|
|
454
|
-
path: 'data',
|
|
455
|
-
code: 'INVALID_TYPE',
|
|
456
|
-
suggestion: 'Provide data as an array of objects, e.g. data: [{ x: 1, y: 2 }]',
|
|
457
|
-
});
|
|
458
|
-
return; // Can't validate further without data
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
if (spec.data.length === 0) {
|
|
462
|
-
errors.push({
|
|
463
|
-
message: 'Spec error: "data" must be a non-empty array',
|
|
464
|
-
path: 'data',
|
|
465
|
-
code: 'EMPTY_DATA',
|
|
466
|
-
suggestion: 'Add at least one data row, e.g. data: [{ x: 1, y: 2 }]',
|
|
467
|
-
});
|
|
468
|
-
return;
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// Validate data entries are objects
|
|
472
|
-
const firstRow = spec.data[0] as unknown;
|
|
473
|
-
if (typeof firstRow !== 'object' || firstRow === null || Array.isArray(firstRow)) {
|
|
474
|
-
errors.push({
|
|
475
|
-
message: 'Spec error: each item in "data" must be a plain object',
|
|
476
|
-
path: 'data[0]',
|
|
477
|
-
code: 'INVALID_TYPE',
|
|
478
|
-
suggestion:
|
|
479
|
-
'Each data item should be an object with key-value pairs, e.g. { name: "Alice", value: 10 }',
|
|
480
|
-
});
|
|
481
|
-
return;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
// Check encoding exists
|
|
485
|
-
if (!spec.encoding || typeof spec.encoding !== 'object') {
|
|
486
|
-
const rules = MARK_ENCODING_RULES[markType as MarkType];
|
|
487
|
-
const requiredChannels = Object.entries(rules)
|
|
488
|
-
.filter(([, rule]) => rule.required)
|
|
489
|
-
.map(([ch]) => ch);
|
|
490
|
-
errors.push({
|
|
491
|
-
message: `Spec error: ${markType} chart requires an "encoding" object`,
|
|
492
|
-
path: 'encoding',
|
|
493
|
-
code: 'MISSING_FIELD',
|
|
494
|
-
suggestion: `Add an encoding object with required channels: ${requiredChannels.join(', ')}. Example: encoding: { ${requiredChannels.map((ch) => `${ch}: { field: "...", type: "..." }`).join(', ')} }`,
|
|
495
|
-
});
|
|
496
|
-
return;
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
const rules = MARK_ENCODING_RULES[markType as MarkType];
|
|
500
|
-
const encoding = spec.encoding as Record<string, unknown>;
|
|
501
|
-
const dataColumns = new Set(Object.keys(firstRow as Record<string, unknown>));
|
|
502
|
-
const columnList = [...dataColumns];
|
|
503
|
-
const availableColumns = columnList.join(', ');
|
|
504
|
-
|
|
505
|
-
// Validate required channels. Arc's canonical channel is theta; waffle and
|
|
506
|
-
// parliament still key on y with theta as an alias. For backward compat,
|
|
507
|
-
// validateSpec (a public entry point that callers may invoke on un-expanded
|
|
508
|
-
// specs) accepts y as satisfying a theta requirement and vice versa on
|
|
509
|
-
// theta-capable marks.
|
|
510
|
-
const THETA_MARKS = new Set(['arc', 'waffle', 'parliament']);
|
|
511
|
-
const isThetaMark = THETA_MARKS.has(markType as string);
|
|
512
|
-
for (const [channel, rule] of Object.entries(rules)) {
|
|
513
|
-
if (!rule.required) continue;
|
|
514
|
-
const satisfiedByAlias =
|
|
515
|
-
isThetaMark && ((channel === 'theta' && encoding.y) || (channel === 'y' && encoding.theta));
|
|
516
|
-
if (!encoding[channel] && !satisfiedByAlias) {
|
|
517
|
-
const allowedTypes = rule.allowedTypes.join(' or ');
|
|
518
|
-
const suggestedChannel = channel === 'y' && isThetaMark ? 'theta' : channel;
|
|
519
|
-
errors.push({
|
|
520
|
-
message: `Spec error: ${markType} chart requires encoding.${suggestedChannel} but none was provided`,
|
|
521
|
-
path: `encoding.${suggestedChannel}`,
|
|
522
|
-
code: 'MISSING_FIELD',
|
|
523
|
-
suggestion: `Add encoding.${suggestedChannel} with a field from your data (${availableColumns}) and type (${allowedTypes}). Example: ${suggestedChannel}: { field: "${[...dataColumns][0] ?? 'myField'}", type: "${rule.allowedTypes[0]}" }`,
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
// Beeswarm axis combination: the channel rules mark both axes optional, so
|
|
529
|
-
// enforce the shape here. Exactly one positional channel is the quantitative
|
|
530
|
-
// value axis; the other, when present, is the nominal/ordinal lane channel.
|
|
531
|
-
// Only declared types are checked; omitted types are inferred later.
|
|
532
|
-
if (markType === 'beeswarm') {
|
|
533
|
-
const xChannel = encoding.x as Record<string, unknown> | undefined;
|
|
534
|
-
const yChannel = encoding.y as Record<string, unknown> | undefined;
|
|
535
|
-
const xType = xChannel?.type as string | undefined;
|
|
536
|
-
const yType = yChannel?.type as string | undefined;
|
|
537
|
-
if (!xChannel && !yChannel) {
|
|
538
|
-
errors.push({
|
|
539
|
-
message:
|
|
540
|
-
'Spec error: beeswarm chart requires a quantitative encoding.x or encoding.y (the value axis)',
|
|
541
|
-
path: 'encoding',
|
|
542
|
-
code: 'MISSING_FIELD',
|
|
543
|
-
suggestion: `Add the value axis, e.g. x: { field: "${[...dataColumns][0] ?? 'myField'}", type: "quantitative" }. Optionally add a nominal channel on the other axis for grouped swarms.`,
|
|
544
|
-
});
|
|
545
|
-
} else if (xType === 'quantitative' && yType === 'quantitative') {
|
|
546
|
-
errors.push({
|
|
547
|
-
message:
|
|
548
|
-
'Spec error: beeswarm chart accepts only one quantitative axis (the value axis), but both encoding.x and encoding.y are quantitative',
|
|
549
|
-
path: 'encoding',
|
|
550
|
-
code: 'ENCODING_MISMATCH',
|
|
551
|
-
suggestion:
|
|
552
|
-
'Keep the value axis quantitative and change the other channel to nominal/ordinal (grouped swarm lanes), or remove it for a single swarm. For two quantitative axes use mark: "point".',
|
|
553
|
-
});
|
|
554
|
-
} else if (
|
|
555
|
-
xType !== 'quantitative' &&
|
|
556
|
-
yType !== 'quantitative' &&
|
|
557
|
-
(!xChannel || !!xType) &&
|
|
558
|
-
(!yChannel || !!yType)
|
|
559
|
-
) {
|
|
560
|
-
errors.push({
|
|
561
|
-
message:
|
|
562
|
-
'Spec error: beeswarm chart requires one quantitative axis (the value axis), but neither encoding.x nor encoding.y is quantitative',
|
|
563
|
-
path: 'encoding',
|
|
564
|
-
code: 'ENCODING_MISMATCH',
|
|
565
|
-
suggestion:
|
|
566
|
-
'Set the value axis to type: "quantitative" (x for a horizontal swarm, y for a vertical one).',
|
|
567
|
-
});
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
// Range marks: orientation-dependent x2/y2 requirement + mark.style options
|
|
572
|
-
if (markType === 'range') {
|
|
573
|
-
validateRangeSpec(spec, errors);
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
// Calendar marks: temporal daily x, no y, parseable dates, mark options
|
|
577
|
-
if (markType === 'calendar') {
|
|
578
|
-
validateCalendarSpec(spec, errors);
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
// Near-miss: VL's string expression form of calculate. A restricted string
|
|
582
|
-
// grammar is deliberately not supported (decision: structured form only);
|
|
583
|
-
// point authors at the structured equivalent instead.
|
|
584
|
-
if (Array.isArray(spec.transform)) {
|
|
585
|
-
const transforms = spec.transform as Record<string, unknown>[];
|
|
586
|
-
for (let i = 0; i < transforms.length; i++) {
|
|
587
|
-
const t = transforms[i];
|
|
588
|
-
if (t && typeof t === 'object' && typeof t.calculate === 'string') {
|
|
589
|
-
errors.push({
|
|
590
|
-
message: `Spec error: transform[${i}].calculate must be a structured expression object, not a string`,
|
|
591
|
-
path: `transform[${i}].calculate`,
|
|
592
|
-
code: 'INVALID_TYPE',
|
|
593
|
-
suggestion:
|
|
594
|
-
'Use the structured form for single operations, e.g. calculate: { op: "/", field: "a", field2: "b" } with as: "ratio". For running or grouped computations use the window or aggregate transforms.',
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
// Collect fields that transforms will create, so we don't reject them
|
|
601
|
-
const transformFields = new Set<string>();
|
|
602
|
-
if (Array.isArray(spec.transform)) {
|
|
603
|
-
for (const t of spec.transform as Record<string, unknown>[]) {
|
|
604
|
-
// filter/bin/calculate/timeUnit/fold write their output(s) to `as`.
|
|
605
|
-
if (typeof t.as === 'string') transformFields.add(t.as);
|
|
606
|
-
if (Array.isArray(t.as)) {
|
|
607
|
-
for (const f of t.as) {
|
|
608
|
-
if (typeof f === 'string') transformFields.add(f);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
// aggregate/window carry their output field names inside their op arrays,
|
|
612
|
-
// and aggregate preserves its groupby fields on the output rows.
|
|
613
|
-
const opList = (t.aggregate ?? t.window) as Record<string, unknown>[] | undefined;
|
|
614
|
-
if (Array.isArray(opList)) {
|
|
615
|
-
for (const op of opList) {
|
|
616
|
-
if (op && typeof op.as === 'string') transformFields.add(op.as);
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
if (Array.isArray(t.groupby)) {
|
|
620
|
-
for (const g of t.groupby) {
|
|
621
|
-
if (typeof g === 'string') transformFields.add(g);
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
// Validate provided channels
|
|
628
|
-
for (const [channel, channelSpec] of Object.entries(encoding)) {
|
|
629
|
-
if (!channelSpec || typeof channelSpec !== 'object') continue;
|
|
630
|
-
|
|
631
|
-
// Tooltip can be an array of encoding channels
|
|
632
|
-
if (channel === 'tooltip' && Array.isArray(channelSpec)) {
|
|
633
|
-
for (let i = 0; i < channelSpec.length; i++) {
|
|
634
|
-
const elem = channelSpec[i] as Record<string, unknown> | null;
|
|
635
|
-
if (!elem || typeof elem !== 'object') continue;
|
|
636
|
-
if (!elem.field || typeof elem.field !== 'string') {
|
|
637
|
-
errors.push({
|
|
638
|
-
message: `Spec error: encoding.tooltip[${i}] must have a "field" string`,
|
|
639
|
-
path: `encoding.tooltip[${i}].field`,
|
|
640
|
-
code: 'MISSING_FIELD',
|
|
641
|
-
suggestion: `Add a field name from your data columns: ${availableColumns}`,
|
|
642
|
-
});
|
|
643
|
-
continue;
|
|
644
|
-
}
|
|
645
|
-
if (!dataColumns.has(elem.field) && !transformFields.has(elem.field)) {
|
|
646
|
-
errors.push({
|
|
647
|
-
message: `Spec error: encoding.tooltip[${i}].field "${elem.field}" does not exist in data. Available columns: ${availableColumns}`,
|
|
648
|
-
path: `encoding.tooltip[${i}].field`,
|
|
649
|
-
code: 'DATA_FIELD_MISSING',
|
|
650
|
-
suggestion: `Use one of the available data columns: ${availableColumns}.${didYouMean(elem.field, columnList)}`,
|
|
651
|
-
});
|
|
652
|
-
}
|
|
653
|
-
if (elem.type && !VALID_FIELD_TYPES.has(elem.type as string)) {
|
|
654
|
-
errors.push({
|
|
655
|
-
message: `Spec error: encoding.tooltip[${i}].type "${elem.type}" is not valid. Must be one of: ${[...VALID_FIELD_TYPES].join(', ')}`,
|
|
656
|
-
path: `encoding.tooltip[${i}].type`,
|
|
657
|
-
code: 'INVALID_VALUE',
|
|
658
|
-
suggestion: `Use one of: ${[...VALID_FIELD_TYPES].join(', ')}`,
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
continue;
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
const channelObj = channelSpec as Record<string, unknown>;
|
|
666
|
-
const channelRule = rules[channel as keyof typeof rules];
|
|
667
|
-
|
|
668
|
-
// Skip ConditionalValueDef channels (they have 'condition' instead of 'field')
|
|
669
|
-
if ('condition' in channelObj) continue;
|
|
670
|
-
|
|
671
|
-
// Check field exists
|
|
672
|
-
if (!channelObj.field || typeof channelObj.field !== 'string') {
|
|
673
|
-
errors.push({
|
|
674
|
-
message: `Spec error: encoding.${channel} must have a "field" string`,
|
|
675
|
-
path: `encoding.${channel}.field`,
|
|
676
|
-
code: 'MISSING_FIELD',
|
|
677
|
-
suggestion: `For constant colors, use mark.fill (e.g., mark: { type: "bar", fill: "#1b7fa3" }) instead of encoding.${channel}. Encoding channels require a data field: ${availableColumns}`,
|
|
678
|
-
});
|
|
679
|
-
continue;
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
// Check field references a column in data (or will be created by a transform)
|
|
683
|
-
if (!dataColumns.has(channelObj.field) && !transformFields.has(channelObj.field)) {
|
|
684
|
-
errors.push({
|
|
685
|
-
message: `Spec error: encoding.${channel}.field "${channelObj.field}" does not exist in data. Available columns: ${availableColumns}`,
|
|
686
|
-
path: `encoding.${channel}.field`,
|
|
687
|
-
code: 'DATA_FIELD_MISSING',
|
|
688
|
-
suggestion: `Use one of the available data columns: ${availableColumns}.${didYouMean(channelObj.field, columnList)}`,
|
|
689
|
-
});
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
// Check field type is valid
|
|
693
|
-
if (channelObj.type && !VALID_FIELD_TYPES.has(channelObj.type as string)) {
|
|
694
|
-
errors.push({
|
|
695
|
-
message: `Spec error: encoding.${channel}.type "${channelObj.type}" is not valid. Must be one of: ${[...VALID_FIELD_TYPES].join(', ')}`,
|
|
696
|
-
path: `encoding.${channel}.type`,
|
|
697
|
-
code: 'INVALID_VALUE',
|
|
698
|
-
suggestion: `Use one of: ${[...VALID_FIELD_TYPES].join(', ')}`,
|
|
699
|
-
});
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
// Check scale.scheme names. Known names (including VL aliases) are
|
|
703
|
-
// resolved to palette ranges by the pre-validation sugar expansion, so a
|
|
704
|
-
// scheme surviving to this point on the compile path is an unknown name.
|
|
705
|
-
checkSchemeName(channelObj, `encoding.${channel}`, errors);
|
|
706
|
-
|
|
707
|
-
// Check field type is allowed for this channel
|
|
708
|
-
if (channelRule && channelObj.type && channelRule.allowedTypes.length > 0) {
|
|
709
|
-
if (!channelRule.allowedTypes.includes(channelObj.type as FieldType)) {
|
|
710
|
-
errors.push({
|
|
711
|
-
message: `Spec error: encoding.${channel} for ${markType} chart does not accept type "${channelObj.type}". Allowed types: ${channelRule.allowedTypes.join(', ')}`,
|
|
712
|
-
path: `encoding.${channel}.type`,
|
|
713
|
-
code: 'ENCODING_MISMATCH',
|
|
714
|
-
suggestion: `Change encoding.${channel}.type to one of: ${channelRule.allowedTypes.join(', ')}`,
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
// Check field values match declared type
|
|
720
|
-
if (channelObj.type && channelObj.field && dataColumns.has(channelObj.field as string)) {
|
|
721
|
-
const data = spec.data as Record<string, unknown>[];
|
|
722
|
-
const fieldName = channelObj.field as string;
|
|
723
|
-
const fieldType = channelObj.type as string;
|
|
724
|
-
// Sample up to 5 values for type checking
|
|
725
|
-
const sampleSize = Math.min(5, data.length);
|
|
726
|
-
|
|
727
|
-
if (fieldType === 'temporal') {
|
|
728
|
-
let nonDateCount = 0;
|
|
729
|
-
for (let i = 0; i < sampleSize; i++) {
|
|
730
|
-
const val = data[i][fieldName];
|
|
731
|
-
if (val != null && !isParseableDate(val)) {
|
|
732
|
-
nonDateCount++;
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
|
-
if (nonDateCount > 0) {
|
|
736
|
-
errors.push({
|
|
737
|
-
message: `Spec error: encoding.${channel}.field "${fieldName}" is declared as temporal but contains non-date values`,
|
|
738
|
-
path: `encoding.${channel}`,
|
|
739
|
-
code: 'ENCODING_MISMATCH',
|
|
740
|
-
suggestion: `Either change the type to "nominal" or ensure "${fieldName}" values are parseable dates (ISO 8601 strings like "2024-01-15" or Date objects)`,
|
|
741
|
-
});
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
if (fieldType === 'quantitative') {
|
|
746
|
-
let nonNumericCount = 0;
|
|
747
|
-
for (let i = 0; i < sampleSize; i++) {
|
|
748
|
-
const val = data[i][fieldName];
|
|
749
|
-
if (val != null && !isNumeric(val)) {
|
|
750
|
-
nonNumericCount++;
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
if (nonNumericCount > 0) {
|
|
754
|
-
errors.push({
|
|
755
|
-
message: `Spec error: encoding.${channel}.field "${fieldName}" is declared as quantitative but contains non-numeric values`,
|
|
756
|
-
path: `encoding.${channel}`,
|
|
757
|
-
code: 'ENCODING_MISMATCH',
|
|
758
|
-
suggestion: `Either change the type to "nominal" or ensure "${fieldName}" values are numbers`,
|
|
759
|
-
});
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
// Validate highlight on the color channel
|
|
766
|
-
if (encoding) {
|
|
767
|
-
const colorCh = (encoding as Record<string, Record<string, unknown> | undefined>).color;
|
|
768
|
-
if (colorCh && colorCh.highlight != null) {
|
|
769
|
-
const hlVal = colorCh.highlight;
|
|
770
|
-
const isValidType =
|
|
771
|
-
typeof hlVal === 'string' ||
|
|
772
|
-
(Array.isArray(hlVal) && hlVal.every((v: unknown) => typeof v === 'string'));
|
|
773
|
-
if (!isValidType) {
|
|
774
|
-
errors.push({
|
|
775
|
-
message: 'Spec error: encoding.color.highlight must be a string or array of strings',
|
|
776
|
-
path: 'encoding.color.highlight',
|
|
777
|
-
code: 'INVALID_TYPE',
|
|
778
|
-
suggestion: 'Provide a series name (string) or an array of series names.',
|
|
779
|
-
});
|
|
780
|
-
} else {
|
|
781
|
-
const colorType =
|
|
782
|
-
(colorCh.type as string | undefined) ??
|
|
783
|
-
(colorCh.field && Array.isArray(spec.data) && spec.data.length > 0
|
|
784
|
-
? inferFieldType(spec.data as Record<string, unknown>[], colorCh.field as string)
|
|
785
|
-
: undefined);
|
|
786
|
-
if (colorType === 'quantitative') {
|
|
787
|
-
errors.push({
|
|
788
|
-
message:
|
|
789
|
-
'Spec error: encoding.color.highlight is not supported on quantitative color channels',
|
|
790
|
-
path: 'encoding.color.highlight',
|
|
791
|
-
code: 'INVALID_VALUE',
|
|
792
|
-
suggestion:
|
|
793
|
-
'Highlight works with nominal/ordinal color (categorical series). Remove highlight or change encoding.color.type to nominal.',
|
|
794
|
-
});
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
}
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
// Validate facet channel if provided
|
|
801
|
-
if (encoding.facet && typeof encoding.facet === 'object') {
|
|
802
|
-
const facet = encoding.facet as Record<string, unknown>;
|
|
803
|
-
if (!facet.field || typeof facet.field !== 'string') {
|
|
804
|
-
errors.push({
|
|
805
|
-
message: 'Spec error: encoding.facet.field is required and must be a non-empty string',
|
|
806
|
-
path: 'encoding.facet.field',
|
|
807
|
-
code: 'MISSING_FIELD',
|
|
808
|
-
suggestion:
|
|
809
|
-
'Add a field property to encoding.facet (e.g. { field: "category", type: "nominal" })',
|
|
810
|
-
});
|
|
811
|
-
} else if (
|
|
812
|
-
!dataColumns.has(facet.field as string) &&
|
|
813
|
-
!transformFields.has(facet.field as string)
|
|
814
|
-
) {
|
|
815
|
-
errors.push({
|
|
816
|
-
message: `Spec error: encoding.facet.field "${facet.field}" does not exist in data. Available columns: ${availableColumns}`,
|
|
817
|
-
path: 'encoding.facet.field',
|
|
818
|
-
code: 'DATA_FIELD_MISSING',
|
|
819
|
-
suggestion: `Use one of the available data columns: ${availableColumns}.${didYouMean(facet.field as string, columnList)}`,
|
|
820
|
-
});
|
|
821
|
-
}
|
|
822
|
-
if (!facet.type || (facet.type !== 'nominal' && facet.type !== 'ordinal')) {
|
|
823
|
-
errors.push({
|
|
824
|
-
message: `Spec error: encoding.facet.type must be "nominal" or "ordinal"${facet.type ? `, got "${facet.type}"` : ''}`,
|
|
825
|
-
path: 'encoding.facet.type',
|
|
826
|
-
code: facet.type ? 'INVALID_VALUE' : 'MISSING_FIELD',
|
|
827
|
-
suggestion: 'Set encoding.facet.type to "nominal" or "ordinal"',
|
|
828
|
-
});
|
|
829
|
-
}
|
|
830
|
-
if (facet.columns !== undefined) {
|
|
831
|
-
const cols = Number(facet.columns);
|
|
832
|
-
if (!Number.isInteger(cols) || cols < 1) {
|
|
833
|
-
errors.push({
|
|
834
|
-
message: 'Spec error: encoding.facet.columns must be a positive integer',
|
|
835
|
-
path: 'encoding.facet.columns',
|
|
836
|
-
code: 'INVALID_VALUE',
|
|
837
|
-
suggestion: 'Set columns to a positive integer (e.g. 3)',
|
|
838
|
-
});
|
|
839
|
-
}
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
|
|
843
|
-
// Validate darkMode if provided
|
|
844
|
-
if (spec.darkMode !== undefined && !VALID_DARK_MODES.has(spec.darkMode as string)) {
|
|
845
|
-
errors.push({
|
|
846
|
-
message: `Spec error: darkMode must be "auto", "force", or "off"`,
|
|
847
|
-
path: 'darkMode',
|
|
848
|
-
code: 'INVALID_VALUE',
|
|
849
|
-
suggestion:
|
|
850
|
-
'Use one of: "auto" (system preference), "force" (always dark), or "off" (always light)',
|
|
851
|
-
});
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
// Validate youDrawIt: line marks only, single-series only, "from" required.
|
|
855
|
-
if (spec.youDrawIt !== undefined && spec.youDrawIt !== false) {
|
|
856
|
-
if (markType !== 'line') {
|
|
857
|
-
errors.push({
|
|
858
|
-
message: `Spec error: youDrawIt is only supported on line charts, but mark is "${markType}"`,
|
|
859
|
-
path: 'youDrawIt',
|
|
860
|
-
code: 'ENCODING_MISMATCH',
|
|
861
|
-
suggestion: 'Remove youDrawIt or change mark to "line".',
|
|
862
|
-
});
|
|
863
|
-
} else {
|
|
864
|
-
const config = spec.youDrawIt as Record<string, unknown>;
|
|
865
|
-
if (
|
|
866
|
-
typeof config !== 'object' ||
|
|
867
|
-
Array.isArray(config) ||
|
|
868
|
-
(config.from !== 0 && !config.from)
|
|
869
|
-
) {
|
|
870
|
-
errors.push({
|
|
871
|
-
message: 'Spec error: youDrawIt.from is required (the x value where drawing starts)',
|
|
872
|
-
path: 'youDrawIt.from',
|
|
873
|
-
code: 'MISSING_FIELD',
|
|
874
|
-
suggestion:
|
|
875
|
-
'Set youDrawIt.from to an x value from your data, e.g. youDrawIt: { from: "2010" }',
|
|
876
|
-
});
|
|
877
|
-
} else if (typeof config.from !== 'string' && typeof config.from !== 'number') {
|
|
878
|
-
errors.push({
|
|
879
|
-
message: 'Spec error: youDrawIt.from must be a string or number',
|
|
880
|
-
path: 'youDrawIt.from',
|
|
881
|
-
code: 'INVALID_TYPE',
|
|
882
|
-
suggestion: 'Provide the x value as a string or number matching your data.',
|
|
883
|
-
});
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
const data = Array.isArray(spec.data) ? (spec.data as Record<string, unknown>[]) : [];
|
|
887
|
-
const colorCh = encoding?.color as Record<string, unknown> | undefined;
|
|
888
|
-
if (colorCh && typeof colorCh === 'object' && typeof colorCh.field === 'string') {
|
|
889
|
-
const colorType =
|
|
890
|
-
(colorCh.type as string | undefined) ?? inferFieldType(data, colorCh.field);
|
|
891
|
-
if (colorType !== 'quantitative') {
|
|
892
|
-
const distinct = new Set(data.map((row) => row[colorCh.field as string]));
|
|
893
|
-
if (distinct.size > 1) {
|
|
894
|
-
errors.push({
|
|
895
|
-
message:
|
|
896
|
-
'Spec error: youDrawIt only supports single-series line charts, but encoding.color has multiple distinct values',
|
|
897
|
-
path: 'youDrawIt',
|
|
898
|
-
code: 'ENCODING_MISMATCH',
|
|
899
|
-
suggestion:
|
|
900
|
-
'Remove encoding.color (or filter the data to one series) to use youDrawIt.',
|
|
901
|
-
});
|
|
902
|
-
}
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
// ---------------------------------------------------------------------------
|
|
910
|
-
// Table validation
|
|
911
|
-
// ---------------------------------------------------------------------------
|
|
912
|
-
|
|
913
|
-
function validateTableSpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
914
|
-
if (!Array.isArray(spec.data)) {
|
|
915
|
-
errors.push({
|
|
916
|
-
message: 'Spec error: "data" must be an array',
|
|
917
|
-
path: 'data',
|
|
918
|
-
code: 'INVALID_TYPE',
|
|
919
|
-
suggestion: 'Provide data as an array of objects, e.g. data: [{ name: "Alice", age: 30 }]',
|
|
920
|
-
});
|
|
921
|
-
return;
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
if (spec.data.length === 0) {
|
|
925
|
-
errors.push({
|
|
926
|
-
message: 'Spec error: "data" must be a non-empty array',
|
|
927
|
-
path: 'data',
|
|
928
|
-
code: 'EMPTY_DATA',
|
|
929
|
-
suggestion: 'Add at least one data row to the data array',
|
|
930
|
-
});
|
|
931
|
-
return;
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
if (!Array.isArray(spec.columns)) {
|
|
935
|
-
errors.push({
|
|
936
|
-
message: 'Spec error: table spec requires a "columns" array',
|
|
937
|
-
path: 'columns',
|
|
938
|
-
code: 'MISSING_FIELD',
|
|
939
|
-
suggestion:
|
|
940
|
-
'Add a columns array defining which data fields to display, e.g. columns: [{ key: "name" }, { key: "age" }]',
|
|
941
|
-
});
|
|
942
|
-
return;
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
const data = spec.data as Record<string, unknown>[];
|
|
946
|
-
const firstRow = data[0];
|
|
947
|
-
if (typeof firstRow !== 'object' || firstRow === null || Array.isArray(firstRow)) {
|
|
948
|
-
errors.push({
|
|
949
|
-
message: 'Spec error: each item in "data" must be a plain object',
|
|
950
|
-
path: 'data[0]',
|
|
951
|
-
code: 'INVALID_TYPE',
|
|
952
|
-
suggestion:
|
|
953
|
-
'Each data item should be an object with key-value pairs, e.g. { name: "Alice", age: 30 }',
|
|
954
|
-
});
|
|
955
|
-
return;
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
const dataColumns = new Set(Object.keys(firstRow as Record<string, unknown>));
|
|
959
|
-
const columnList = [...dataColumns];
|
|
960
|
-
const availableColumns = columnList.join(', ');
|
|
961
|
-
const columns = spec.columns as Record<string, unknown>[];
|
|
962
|
-
|
|
963
|
-
for (let i = 0; i < columns.length; i++) {
|
|
964
|
-
const col = columns[i];
|
|
965
|
-
if (!col || typeof col !== 'object') {
|
|
966
|
-
errors.push({
|
|
967
|
-
message: `Spec error: columns[${i}] must be an object`,
|
|
968
|
-
path: `columns[${i}]`,
|
|
969
|
-
code: 'INVALID_TYPE',
|
|
970
|
-
suggestion: 'Each column entry should be an object, e.g. { key: "fieldName" }',
|
|
971
|
-
});
|
|
972
|
-
continue;
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
// Check key exists
|
|
976
|
-
if (!col.key || typeof col.key !== 'string') {
|
|
977
|
-
errors.push({
|
|
978
|
-
message: `Spec error: columns[${i}] must have a "key" string`,
|
|
979
|
-
path: `columns[${i}].key`,
|
|
980
|
-
code: 'MISSING_FIELD',
|
|
981
|
-
suggestion: `Add a key referencing a data field. Available columns: ${availableColumns}`,
|
|
982
|
-
});
|
|
983
|
-
continue;
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
// Check key references a field in data
|
|
987
|
-
if (!dataColumns.has(col.key as string)) {
|
|
988
|
-
errors.push({
|
|
989
|
-
message: `Spec error: columns[${i}].key "${col.key}" does not exist in data. Available columns: ${availableColumns}`,
|
|
990
|
-
path: `columns[${i}].key`,
|
|
991
|
-
code: 'DATA_FIELD_MISSING',
|
|
992
|
-
suggestion: `Use one of the available data columns: ${availableColumns}.${didYouMean(col.key as string, columnList)}`,
|
|
993
|
-
});
|
|
994
|
-
}
|
|
995
|
-
|
|
996
|
-
// Check at most one visual enhancement
|
|
997
|
-
const visuals = ['heatmap', 'bar', 'sparkline', 'image', 'flag', 'categoryColors'].filter(
|
|
998
|
-
(v) => col[v] != null && col[v] !== false,
|
|
999
|
-
);
|
|
1000
|
-
if (visuals.length > 1) {
|
|
1001
|
-
errors.push({
|
|
1002
|
-
message: `Spec error: columns[${i}] has multiple visual features (${visuals.join(', ')}). Only one is allowed per column.`,
|
|
1003
|
-
path: `columns[${i}]`,
|
|
1004
|
-
code: 'INVALID_VALUE',
|
|
1005
|
-
suggestion: `Keep only one visual feature per column. Remove all but one of: ${visuals.join(', ')}`,
|
|
1006
|
-
});
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
// ---------------------------------------------------------------------------
|
|
1012
|
-
// Graph validation
|
|
1013
|
-
// ---------------------------------------------------------------------------
|
|
1014
|
-
|
|
1015
|
-
function validateGraphSpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
1016
|
-
// Validate nodes array exists and is non-empty
|
|
1017
|
-
if (!Array.isArray(spec.nodes)) {
|
|
1018
|
-
errors.push({
|
|
1019
|
-
message: 'Spec error: graph spec requires a "nodes" array',
|
|
1020
|
-
path: 'nodes',
|
|
1021
|
-
code: 'MISSING_FIELD',
|
|
1022
|
-
suggestion:
|
|
1023
|
-
'Add a nodes array with objects that have an "id" field, e.g. nodes: [{ id: "a" }, { id: "b" }]',
|
|
1024
|
-
});
|
|
1025
|
-
return; // Can't validate further without nodes
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
|
-
if (spec.nodes.length === 0) {
|
|
1029
|
-
errors.push({
|
|
1030
|
-
message: 'Spec error: "nodes" must be a non-empty array',
|
|
1031
|
-
path: 'nodes',
|
|
1032
|
-
code: 'EMPTY_DATA',
|
|
1033
|
-
suggestion: 'Add at least one node, e.g. nodes: [{ id: "a" }]',
|
|
1034
|
-
});
|
|
1035
|
-
return;
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
|
-
// Validate each node has a string id
|
|
1039
|
-
const nodeIds = new Set<string>();
|
|
1040
|
-
const nodes = spec.nodes as Record<string, unknown>[];
|
|
1041
|
-
for (let i = 0; i < nodes.length; i++) {
|
|
1042
|
-
const node = nodes[i];
|
|
1043
|
-
if (!node || typeof node !== 'object') {
|
|
1044
|
-
errors.push({
|
|
1045
|
-
message: `Spec error: nodes[${i}] must be an object`,
|
|
1046
|
-
path: `nodes[${i}]`,
|
|
1047
|
-
code: 'INVALID_TYPE',
|
|
1048
|
-
suggestion: 'Each node must be an object with at least an "id" field, e.g. { id: "a" }',
|
|
1049
|
-
});
|
|
1050
|
-
continue;
|
|
1051
|
-
}
|
|
1052
|
-
if (typeof node.id !== 'string' || node.id === '') {
|
|
1053
|
-
errors.push({
|
|
1054
|
-
message: `Spec error: nodes[${i}] must have a non-empty string "id" field`,
|
|
1055
|
-
path: `nodes[${i}].id`,
|
|
1056
|
-
code: 'MISSING_FIELD',
|
|
1057
|
-
suggestion: 'Add a string id to the node, e.g. { id: "node1" }',
|
|
1058
|
-
});
|
|
1059
|
-
} else {
|
|
1060
|
-
nodeIds.add(node.id);
|
|
1061
|
-
}
|
|
1062
|
-
}
|
|
1063
|
-
|
|
1064
|
-
// Validate edges array exists
|
|
1065
|
-
if (!Array.isArray(spec.edges)) {
|
|
1066
|
-
errors.push({
|
|
1067
|
-
message: 'Spec error: graph spec requires an "edges" array',
|
|
1068
|
-
path: 'edges',
|
|
1069
|
-
code: 'MISSING_FIELD',
|
|
1070
|
-
suggestion: 'Add an edges array (can be empty), e.g. edges: [{ source: "a", target: "b" }]',
|
|
1071
|
-
});
|
|
1072
|
-
return;
|
|
1073
|
-
}
|
|
1074
|
-
|
|
1075
|
-
// Validate each edge has string source and target that reference existing nodes
|
|
1076
|
-
const edges = spec.edges as Record<string, unknown>[];
|
|
1077
|
-
for (let i = 0; i < edges.length; i++) {
|
|
1078
|
-
const edge = edges[i];
|
|
1079
|
-
if (!edge || typeof edge !== 'object') {
|
|
1080
|
-
errors.push({
|
|
1081
|
-
message: `Spec error: edges[${i}] must be an object`,
|
|
1082
|
-
path: `edges[${i}]`,
|
|
1083
|
-
code: 'INVALID_TYPE',
|
|
1084
|
-
suggestion:
|
|
1085
|
-
'Each edge must be an object with "source" and "target" fields, e.g. { source: "a", target: "b" }',
|
|
1086
|
-
});
|
|
1087
|
-
continue;
|
|
1088
|
-
}
|
|
1089
|
-
|
|
1090
|
-
if (typeof edge.source !== 'string' || edge.source === '') {
|
|
1091
|
-
errors.push({
|
|
1092
|
-
message: `Spec error: edges[${i}] must have a non-empty string "source" field`,
|
|
1093
|
-
path: `edges[${i}].source`,
|
|
1094
|
-
code: 'MISSING_FIELD',
|
|
1095
|
-
suggestion: 'Add a source node id, e.g. { source: "a", target: "b" }',
|
|
1096
|
-
});
|
|
1097
|
-
} else if (nodeIds.size > 0 && !nodeIds.has(edge.source)) {
|
|
1098
|
-
errors.push({
|
|
1099
|
-
message: `Spec error: edges[${i}].source "${edge.source}" does not reference an existing node id`,
|
|
1100
|
-
path: `edges[${i}].source`,
|
|
1101
|
-
code: 'DATA_FIELD_MISSING',
|
|
1102
|
-
suggestion: `Use one of the existing node ids: ${[...nodeIds].slice(0, 5).join(', ')}${nodeIds.size > 5 ? '...' : ''}.${didYouMean(edge.source, [...nodeIds])}`,
|
|
1103
|
-
});
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
|
-
if (typeof edge.target !== 'string' || edge.target === '') {
|
|
1107
|
-
errors.push({
|
|
1108
|
-
message: `Spec error: edges[${i}] must have a non-empty string "target" field`,
|
|
1109
|
-
path: `edges[${i}].target`,
|
|
1110
|
-
code: 'MISSING_FIELD',
|
|
1111
|
-
suggestion: 'Add a target node id, e.g. { source: "a", target: "b" }',
|
|
1112
|
-
});
|
|
1113
|
-
} else if (nodeIds.size > 0 && !nodeIds.has(edge.target)) {
|
|
1114
|
-
errors.push({
|
|
1115
|
-
message: `Spec error: edges[${i}].target "${edge.target}" does not reference an existing node id`,
|
|
1116
|
-
path: `edges[${i}].target`,
|
|
1117
|
-
code: 'DATA_FIELD_MISSING',
|
|
1118
|
-
suggestion: `Use one of the existing node ids: ${[...nodeIds].slice(0, 5).join(', ')}${nodeIds.size > 5 ? '...' : ''}.${didYouMean(edge.target, [...nodeIds])}`,
|
|
1119
|
-
});
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
|
|
1123
|
-
// Validate encoding fields exist on at least the first node/edge
|
|
1124
|
-
if (spec.encoding && typeof spec.encoding === 'object') {
|
|
1125
|
-
const encoding = spec.encoding as Record<string, unknown>;
|
|
1126
|
-
const firstNode = nodes[0] as Record<string, unknown>;
|
|
1127
|
-
const firstEdge = edges.length > 0 ? (edges[0] as Record<string, unknown>) : null;
|
|
1128
|
-
const nodeFields = firstNode ? new Set(Object.keys(firstNode)) : new Set<string>();
|
|
1129
|
-
const edgeFields = firstEdge ? new Set(Object.keys(firstEdge)) : new Set<string>();
|
|
1130
|
-
|
|
1131
|
-
const nodeChannels = ['nodeColor', 'nodeSize', 'nodeLabel'] as const;
|
|
1132
|
-
for (const channel of nodeChannels) {
|
|
1133
|
-
const ch = encoding[channel] as Record<string, unknown> | undefined;
|
|
1134
|
-
if (ch?.field && typeof ch.field === 'string' && !nodeFields.has(ch.field)) {
|
|
1135
|
-
errors.push({
|
|
1136
|
-
message: `Spec error: encoding.${channel}.field "${ch.field}" does not exist on nodes. Available fields: ${[...nodeFields].join(', ')}`,
|
|
1137
|
-
path: `encoding.${channel}.field`,
|
|
1138
|
-
code: 'DATA_FIELD_MISSING',
|
|
1139
|
-
suggestion: `Use one of the node fields: ${[...nodeFields].join(', ')}.${didYouMean(ch.field, [...nodeFields])}`,
|
|
1140
|
-
});
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
|
-
const edgeChannels = ['edgeColor', 'edgeWidth'] as const;
|
|
1145
|
-
for (const channel of edgeChannels) {
|
|
1146
|
-
const ch = encoding[channel] as Record<string, unknown> | undefined;
|
|
1147
|
-
if (ch?.field && typeof ch.field === 'string' && firstEdge && !edgeFields.has(ch.field)) {
|
|
1148
|
-
errors.push({
|
|
1149
|
-
message: `Spec error: encoding.${channel}.field "${ch.field}" does not exist on edges. Available fields: ${[...edgeFields].join(', ')}`,
|
|
1150
|
-
path: `encoding.${channel}.field`,
|
|
1151
|
-
code: 'DATA_FIELD_MISSING',
|
|
1152
|
-
suggestion: `Use one of the edge fields: ${[...edgeFields].join(', ')}.${didYouMean(ch.field, [...edgeFields])}`,
|
|
1153
|
-
});
|
|
1154
|
-
}
|
|
1155
|
-
}
|
|
1156
|
-
|
|
1157
|
-
// Graph never reads scale.scheme: colors come from scale.range/scale.domain
|
|
1158
|
-
checkSchemeUnused(
|
|
1159
|
-
encoding,
|
|
1160
|
-
errors,
|
|
1161
|
-
'graph colors come from scale.range and scale.domain',
|
|
1162
|
-
'Remove scale.scheme. To customize graph colors, provide explicit colors via scale.range.',
|
|
1163
|
-
);
|
|
1164
|
-
}
|
|
1165
|
-
|
|
1166
|
-
// Validate layout type if specified
|
|
1167
|
-
if (spec.layout && typeof spec.layout === 'object') {
|
|
1168
|
-
const layout = spec.layout as Record<string, unknown>;
|
|
1169
|
-
if (layout.type && layout.type !== 'force') {
|
|
1170
|
-
errors.push({
|
|
1171
|
-
message: `Spec error: layout.type "${layout.type}" is not supported. Only "force" is currently supported`,
|
|
1172
|
-
path: 'layout.type',
|
|
1173
|
-
code: 'INVALID_VALUE',
|
|
1174
|
-
suggestion:
|
|
1175
|
-
'Use layout.type: "force" or omit the layout field to use the default force layout',
|
|
1176
|
-
});
|
|
1177
|
-
}
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
|
|
1181
|
-
// ---------------------------------------------------------------------------
|
|
1182
|
-
// Sankey validation
|
|
1183
|
-
// ---------------------------------------------------------------------------
|
|
1184
|
-
|
|
1185
|
-
function validateSankeySpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
1186
|
-
// Validate data
|
|
1187
|
-
if (!Array.isArray(spec.data)) {
|
|
1188
|
-
errors.push({
|
|
1189
|
-
message: 'Spec error: sankey spec requires a "data" array',
|
|
1190
|
-
path: 'data',
|
|
1191
|
-
code: 'INVALID_TYPE',
|
|
1192
|
-
suggestion:
|
|
1193
|
-
'Provide data as an array of objects, e.g. data: [{ source: "A", target: "B", value: 10 }]',
|
|
1194
|
-
});
|
|
1195
|
-
return;
|
|
1196
|
-
}
|
|
1197
|
-
|
|
1198
|
-
if (spec.data.length === 0) {
|
|
1199
|
-
errors.push({
|
|
1200
|
-
message: 'Spec error: "data" must be a non-empty array',
|
|
1201
|
-
path: 'data',
|
|
1202
|
-
code: 'EMPTY_DATA',
|
|
1203
|
-
suggestion: 'Add at least one data row, e.g. data: [{ source: "A", target: "B", value: 10 }]',
|
|
1204
|
-
});
|
|
1205
|
-
return;
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
const firstRow = spec.data[0] as unknown;
|
|
1209
|
-
if (typeof firstRow !== 'object' || firstRow === null || Array.isArray(firstRow)) {
|
|
1210
|
-
errors.push({
|
|
1211
|
-
message: 'Spec error: each item in "data" must be a plain object',
|
|
1212
|
-
path: 'data[0]',
|
|
1213
|
-
code: 'INVALID_TYPE',
|
|
1214
|
-
suggestion:
|
|
1215
|
-
'Each data item should be an object, e.g. { source: "A", target: "B", value: 10 }',
|
|
1216
|
-
});
|
|
1217
|
-
return;
|
|
1218
|
-
}
|
|
1219
|
-
|
|
1220
|
-
// Validate encoding
|
|
1221
|
-
if (!spec.encoding || typeof spec.encoding !== 'object') {
|
|
1222
|
-
errors.push({
|
|
1223
|
-
message:
|
|
1224
|
-
'Spec error: sankey spec requires an "encoding" object with source, target, and value channels',
|
|
1225
|
-
path: 'encoding',
|
|
1226
|
-
code: 'MISSING_FIELD',
|
|
1227
|
-
suggestion:
|
|
1228
|
-
'Add an encoding object, e.g. encoding: { source: { field: "source", type: "nominal" }, target: { field: "target", type: "nominal" }, value: { field: "value", type: "quantitative" } }',
|
|
1229
|
-
});
|
|
1230
|
-
return;
|
|
1231
|
-
}
|
|
1232
|
-
|
|
1233
|
-
const encoding = spec.encoding as Record<string, unknown>;
|
|
1234
|
-
const dataColumns = new Set(Object.keys(firstRow as Record<string, unknown>));
|
|
1235
|
-
const columnList = [...dataColumns];
|
|
1236
|
-
const availableColumns = columnList.join(', ');
|
|
1237
|
-
|
|
1238
|
-
// Required channels
|
|
1239
|
-
for (const channel of ['source', 'target', 'value'] as const) {
|
|
1240
|
-
const ch = encoding[channel] as Record<string, unknown> | undefined;
|
|
1241
|
-
if (!ch || typeof ch !== 'object') {
|
|
1242
|
-
errors.push({
|
|
1243
|
-
message: `Spec error: sankey encoding requires "${channel}" channel`,
|
|
1244
|
-
path: `encoding.${channel}`,
|
|
1245
|
-
code: 'MISSING_FIELD',
|
|
1246
|
-
suggestion: `Add encoding.${channel} with a field from your data (${availableColumns}). Example: ${channel}: { field: "${columnList[0] ?? 'myField'}", type: "${channel === 'value' ? 'quantitative' : 'nominal'}" }`,
|
|
1247
|
-
});
|
|
1248
|
-
continue;
|
|
1249
|
-
}
|
|
1250
|
-
|
|
1251
|
-
if (!ch.field || typeof ch.field !== 'string') {
|
|
1252
|
-
errors.push({
|
|
1253
|
-
message: `Spec error: encoding.${channel} must have a "field" string`,
|
|
1254
|
-
path: `encoding.${channel}.field`,
|
|
1255
|
-
code: 'MISSING_FIELD',
|
|
1256
|
-
suggestion: `Add a field name from your data columns: ${availableColumns}`,
|
|
1257
|
-
});
|
|
1258
|
-
continue;
|
|
1259
|
-
}
|
|
1260
|
-
|
|
1261
|
-
if (!dataColumns.has(ch.field as string)) {
|
|
1262
|
-
errors.push({
|
|
1263
|
-
message: `Spec error: encoding.${channel}.field "${ch.field}" does not exist in data. Available columns: ${availableColumns}`,
|
|
1264
|
-
path: `encoding.${channel}.field`,
|
|
1265
|
-
code: 'DATA_FIELD_MISSING',
|
|
1266
|
-
suggestion: `Use one of the available data columns: ${availableColumns}.${didYouMean(ch.field as string, columnList)}`,
|
|
1267
|
-
});
|
|
1268
|
-
}
|
|
1269
|
-
}
|
|
1270
|
-
|
|
1271
|
-
// Sankey never reads scale.scheme: node colors cycle theme.colors.categorical
|
|
1272
|
-
checkSchemeUnused(
|
|
1273
|
-
encoding,
|
|
1274
|
-
errors,
|
|
1275
|
-
'sankey node colors cycle the theme categorical palette',
|
|
1276
|
-
'Remove scale.scheme. To customize sankey colors, set theme.colors.categorical.',
|
|
1277
|
-
);
|
|
1278
|
-
|
|
1279
|
-
// Validate darkMode if provided
|
|
1280
|
-
if (spec.darkMode !== undefined && !VALID_DARK_MODES.has(spec.darkMode as string)) {
|
|
1281
|
-
errors.push({
|
|
1282
|
-
message: 'Spec error: darkMode must be "auto", "force", or "off"',
|
|
1283
|
-
path: 'darkMode',
|
|
1284
|
-
code: 'INVALID_VALUE',
|
|
1285
|
-
suggestion:
|
|
1286
|
-
'Use one of: "auto" (system preference), "force" (always dark), or "off" (always light)',
|
|
1287
|
-
});
|
|
1288
|
-
}
|
|
1289
|
-
}
|
|
1290
|
-
|
|
1291
|
-
// ---------------------------------------------------------------------------
|
|
1292
|
-
// TileMap validation
|
|
1293
|
-
// ---------------------------------------------------------------------------
|
|
1294
|
-
|
|
1295
|
-
function validateTileMapSpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
1296
|
-
// Validate data (can be record or array)
|
|
1297
|
-
if (!spec.data || typeof spec.data !== 'object') {
|
|
1298
|
-
errors.push({
|
|
1299
|
-
message: 'Spec error: tilemap spec requires a "data" field (record or array)',
|
|
1300
|
-
path: 'data',
|
|
1301
|
-
code: 'INVALID_TYPE',
|
|
1302
|
-
suggestion:
|
|
1303
|
-
'Provide data as either a record mapping state codes to values (e.g. { "CA": 12000, "TX": 8500 }) or an array of objects with state and value fields',
|
|
1304
|
-
});
|
|
1305
|
-
return;
|
|
1306
|
-
}
|
|
1307
|
-
|
|
1308
|
-
// If data is an object (record), validate it has at least one entry
|
|
1309
|
-
if (!Array.isArray(spec.data) && Object.keys(spec.data as Record<string, unknown>).length === 0) {
|
|
1310
|
-
errors.push({
|
|
1311
|
-
message: 'Spec error: "data" must have at least one entry',
|
|
1312
|
-
path: 'data',
|
|
1313
|
-
code: 'EMPTY_DATA',
|
|
1314
|
-
suggestion: 'Add at least one state-value pair, e.g. { "CA": 12000 }',
|
|
1315
|
-
});
|
|
1316
|
-
return;
|
|
1317
|
-
}
|
|
1318
|
-
|
|
1319
|
-
// If data is an array, validate it's non-empty
|
|
1320
|
-
if (Array.isArray(spec.data)) {
|
|
1321
|
-
if (spec.data.length === 0) {
|
|
1322
|
-
errors.push({
|
|
1323
|
-
message: 'Spec error: "data" array must be non-empty',
|
|
1324
|
-
path: 'data',
|
|
1325
|
-
code: 'EMPTY_DATA',
|
|
1326
|
-
suggestion: 'Add at least one data row',
|
|
1327
|
-
});
|
|
1328
|
-
return;
|
|
1329
|
-
}
|
|
1330
|
-
|
|
1331
|
-
const firstRow = spec.data[0] as unknown;
|
|
1332
|
-
if (typeof firstRow !== 'object' || firstRow === null || Array.isArray(firstRow)) {
|
|
1333
|
-
errors.push({
|
|
1334
|
-
message: 'Spec error: each item in "data" must be a plain object',
|
|
1335
|
-
path: 'data[0]',
|
|
1336
|
-
code: 'INVALID_TYPE',
|
|
1337
|
-
suggestion: 'Each data item should be an object, e.g. { state: "CA", value: 12000 }',
|
|
1338
|
-
});
|
|
1339
|
-
return;
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
// If data is array, encoding is required
|
|
1343
|
-
if (!spec.encoding || typeof spec.encoding !== 'object') {
|
|
1344
|
-
errors.push({
|
|
1345
|
-
message:
|
|
1346
|
-
'Spec error: tilemap spec with array data requires an "encoding" object with state and value channels',
|
|
1347
|
-
path: 'encoding',
|
|
1348
|
-
code: 'MISSING_FIELD',
|
|
1349
|
-
suggestion:
|
|
1350
|
-
'Add an encoding object, e.g. encoding: { state: { field: "state", type: "nominal" }, value: { field: "value", type: "quantitative" } }',
|
|
1351
|
-
});
|
|
1352
|
-
return;
|
|
1353
|
-
}
|
|
1354
|
-
|
|
1355
|
-
const encoding = spec.encoding as Record<string, unknown>;
|
|
1356
|
-
const dataColumns = new Set(Object.keys(firstRow as Record<string, unknown>));
|
|
1357
|
-
const columnList = [...dataColumns];
|
|
1358
|
-
const availableColumns = columnList.join(', ');
|
|
1359
|
-
|
|
1360
|
-
// Required channels
|
|
1361
|
-
for (const channel of ['state', 'value'] as const) {
|
|
1362
|
-
const ch = encoding[channel] as Record<string, unknown> | undefined;
|
|
1363
|
-
if (!ch || typeof ch !== 'object') {
|
|
1364
|
-
errors.push({
|
|
1365
|
-
message: `Spec error: tilemap encoding requires "${channel}" channel`,
|
|
1366
|
-
path: `encoding.${channel}`,
|
|
1367
|
-
code: 'MISSING_FIELD',
|
|
1368
|
-
suggestion: `Add encoding.${channel} with a field from your data (${availableColumns}). Example: ${channel}: { field: "${columnList[0] ?? 'myField'}", type: "${channel === 'value' ? 'quantitative' : 'nominal'}" }`,
|
|
1369
|
-
});
|
|
1370
|
-
continue;
|
|
1371
|
-
}
|
|
1372
|
-
|
|
1373
|
-
if (!ch.field || typeof ch.field !== 'string') {
|
|
1374
|
-
errors.push({
|
|
1375
|
-
message: `Spec error: encoding.${channel} must have a "field" string`,
|
|
1376
|
-
path: `encoding.${channel}.field`,
|
|
1377
|
-
code: 'MISSING_FIELD',
|
|
1378
|
-
suggestion: `Add a field name from your data columns: ${availableColumns}`,
|
|
1379
|
-
});
|
|
1380
|
-
continue;
|
|
1381
|
-
}
|
|
1382
|
-
|
|
1383
|
-
if (!dataColumns.has(ch.field as string)) {
|
|
1384
|
-
errors.push({
|
|
1385
|
-
message: `Spec error: encoding.${channel}.field "${ch.field}" does not exist in data. Available columns: ${availableColumns}`,
|
|
1386
|
-
path: `encoding.${channel}.field`,
|
|
1387
|
-
code: 'DATA_FIELD_MISSING',
|
|
1388
|
-
suggestion: `Use one of the available data columns: ${availableColumns}.${didYouMean(ch.field as string, columnList)}`,
|
|
1389
|
-
});
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
|
|
1394
|
-
// Tilemap never reads scale.scheme: fills come from the top-level palette
|
|
1395
|
-
// prop (encoding may be present in both record-map and array-data modes)
|
|
1396
|
-
if (spec.encoding && typeof spec.encoding === 'object') {
|
|
1397
|
-
checkSchemeUnused(
|
|
1398
|
-
spec.encoding as Record<string, unknown>,
|
|
1399
|
-
errors,
|
|
1400
|
-
'tilemap fills come from the top-level palette property',
|
|
1401
|
-
`Remove scale.scheme and set the top-level palette property instead, e.g. palette: "green". Supported: ${MAP_PALETTE_NAMES}.`,
|
|
1402
|
-
);
|
|
1403
|
-
}
|
|
1404
|
-
|
|
1405
|
-
// Validate darkMode if provided
|
|
1406
|
-
if (spec.darkMode !== undefined && !VALID_DARK_MODES.has(spec.darkMode as string)) {
|
|
1407
|
-
errors.push({
|
|
1408
|
-
message: 'Spec error: darkMode must be "auto", "force", or "off"',
|
|
1409
|
-
path: 'darkMode',
|
|
1410
|
-
code: 'INVALID_VALUE',
|
|
1411
|
-
suggestion:
|
|
1412
|
-
'Use one of: "auto" (system preference), "force" (always dark), or "off" (always light)',
|
|
1413
|
-
});
|
|
1414
|
-
}
|
|
1415
|
-
}
|
|
1416
|
-
|
|
1417
|
-
// ---------------------------------------------------------------------------
|
|
1418
|
-
// Map validation
|
|
1419
|
-
// ---------------------------------------------------------------------------
|
|
1420
|
-
|
|
1421
|
-
function validateMapSpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
1422
|
-
if (!spec.geo || typeof spec.geo !== 'object') {
|
|
1423
|
-
errors.push({
|
|
1424
|
-
message: 'Spec error: map spec requires a "geo" object with a TopoJSON "features" field',
|
|
1425
|
-
path: 'geo',
|
|
1426
|
-
code: 'MISSING_FIELD',
|
|
1427
|
-
suggestion: 'Add a geo object, e.g. geo: { features: topoJsonData, projection: "identity" }',
|
|
1428
|
-
});
|
|
1429
|
-
return;
|
|
1430
|
-
}
|
|
1431
|
-
|
|
1432
|
-
const geo = spec.geo as Record<string, unknown>;
|
|
1433
|
-
if (!geo.features) {
|
|
1434
|
-
errors.push({
|
|
1435
|
-
message: 'Spec error: geo requires a "features" field containing TopoJSON topology',
|
|
1436
|
-
path: 'geo.features',
|
|
1437
|
-
code: 'MISSING_FIELD',
|
|
1438
|
-
suggestion:
|
|
1439
|
-
'Import a TopoJSON file and pass it as geo.features, e.g. import topo from "us-atlas/states-albers-10m.json"',
|
|
1440
|
-
});
|
|
1441
|
-
return;
|
|
1442
|
-
}
|
|
1443
|
-
|
|
1444
|
-
if (!spec.encoding || typeof spec.encoding !== 'object') {
|
|
1445
|
-
errors.push({
|
|
1446
|
-
message: 'Spec error: map spec requires an "encoding" object with key and color channels',
|
|
1447
|
-
path: 'encoding',
|
|
1448
|
-
code: 'MISSING_FIELD',
|
|
1449
|
-
suggestion:
|
|
1450
|
-
'Add an encoding object, e.g. encoding: { key: { field: "id", type: "nominal" }, color: { field: "value", type: "quantitative" } }',
|
|
1451
|
-
});
|
|
1452
|
-
return;
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
// Map color channels resolve scale.scheme against the sequential palettes
|
|
1456
|
-
// with a silent blue fallback; reject unsupported names here instead.
|
|
1457
|
-
const mapEncoding = spec.encoding as Record<string, unknown>;
|
|
1458
|
-
if (mapEncoding.color && typeof mapEncoding.color === 'object') {
|
|
1459
|
-
checkMapSchemeName(mapEncoding.color as Record<string, unknown>, 'encoding.color', errors);
|
|
1460
|
-
}
|
|
1461
|
-
if (spec.points && typeof spec.points === 'object') {
|
|
1462
|
-
const pointsLayer = spec.points as Record<string, unknown>;
|
|
1463
|
-
if (pointsLayer.color && typeof pointsLayer.color === 'object') {
|
|
1464
|
-
checkMapSchemeName(pointsLayer.color as Record<string, unknown>, 'points.color', errors);
|
|
1465
|
-
}
|
|
1466
|
-
}
|
|
1467
|
-
|
|
1468
|
-
// Validate geo.focus if present
|
|
1469
|
-
if ('focus' in geo && geo.focus !== undefined && geo.focus !== null) {
|
|
1470
|
-
const focus = geo.focus;
|
|
1471
|
-
if (typeof focus === 'string' || typeof focus === 'number') {
|
|
1472
|
-
// valid: single id
|
|
1473
|
-
} else if (Array.isArray(focus)) {
|
|
1474
|
-
for (let i = 0; i < focus.length; i++) {
|
|
1475
|
-
if (typeof focus[i] !== 'string' && typeof focus[i] !== 'number') {
|
|
1476
|
-
errors.push({
|
|
1477
|
-
message: `Spec error: geo.focus array element [${i}] must be a string or number`,
|
|
1478
|
-
path: `geo.focus[${i}]`,
|
|
1479
|
-
code: 'INVALID_TYPE',
|
|
1480
|
-
suggestion: 'Each element in the focus array should be a feature id (string or number)',
|
|
1481
|
-
});
|
|
1482
|
-
}
|
|
1483
|
-
}
|
|
1484
|
-
} else if (typeof focus === 'object') {
|
|
1485
|
-
const focusObj = focus as Record<string, unknown>;
|
|
1486
|
-
if ('points' in focusObj) {
|
|
1487
|
-
// Points form: { points: true | { field, value }, padding? } fits the
|
|
1488
|
-
// point layer's cluster (all points, or a matching subset).
|
|
1489
|
-
const pts = focusObj.points;
|
|
1490
|
-
const isSubset =
|
|
1491
|
-
typeof pts === 'object' &&
|
|
1492
|
-
pts !== null &&
|
|
1493
|
-
typeof (pts as Record<string, unknown>).field === 'string' &&
|
|
1494
|
-
'value' in (pts as Record<string, unknown>);
|
|
1495
|
-
if (pts !== true && !isSubset) {
|
|
1496
|
-
errors.push({
|
|
1497
|
-
message:
|
|
1498
|
-
'Spec error: geo.focus.points must be true or { field: string, value: string | number }',
|
|
1499
|
-
path: 'geo.focus.points',
|
|
1500
|
-
code: 'INVALID_TYPE',
|
|
1501
|
-
suggestion:
|
|
1502
|
-
'Use { points: true } to fit all points, or { points: { field: "rating", value: "F" } } to fit a subset.',
|
|
1503
|
-
});
|
|
1504
|
-
}
|
|
1505
|
-
if (focusObj.padding !== undefined && typeof focusObj.padding !== 'number') {
|
|
1506
|
-
errors.push({
|
|
1507
|
-
message: 'Spec error: geo.focus.padding must be a number',
|
|
1508
|
-
path: 'geo.focus.padding',
|
|
1509
|
-
code: 'INVALID_TYPE',
|
|
1510
|
-
suggestion: 'Provide padding as a number in map-local units, e.g. padding: 16',
|
|
1511
|
-
});
|
|
1512
|
-
}
|
|
1513
|
-
} else if (!('features' in focusObj) || focusObj.features == null) {
|
|
1514
|
-
errors.push({
|
|
1515
|
-
message: 'Spec error: geo.focus object must have a "features" or "points" property',
|
|
1516
|
-
path: 'geo.focus.features',
|
|
1517
|
-
code: 'MISSING_FIELD',
|
|
1518
|
-
suggestion:
|
|
1519
|
-
'Provide feature id(s) to focus on, e.g. { features: "36", padding: 32 }, or { points: true } to fit the point cluster.',
|
|
1520
|
-
});
|
|
1521
|
-
} else {
|
|
1522
|
-
const feats = focusObj.features;
|
|
1523
|
-
if (typeof feats !== 'string' && typeof feats !== 'number' && !Array.isArray(feats)) {
|
|
1524
|
-
errors.push({
|
|
1525
|
-
message:
|
|
1526
|
-
'Spec error: geo.focus.features must be a string, number, or array of strings/numbers',
|
|
1527
|
-
path: 'geo.focus.features',
|
|
1528
|
-
code: 'INVALID_TYPE',
|
|
1529
|
-
suggestion: 'Use a feature id or array of ids, e.g. "36" or ["36", "34"]',
|
|
1530
|
-
});
|
|
1531
|
-
} else if (Array.isArray(feats)) {
|
|
1532
|
-
for (let i = 0; i < feats.length; i++) {
|
|
1533
|
-
if (typeof feats[i] !== 'string' && typeof feats[i] !== 'number') {
|
|
1534
|
-
errors.push({
|
|
1535
|
-
message: `Spec error: geo.focus.features[${i}] must be a string or number`,
|
|
1536
|
-
path: `geo.focus.features[${i}]`,
|
|
1537
|
-
code: 'INVALID_TYPE',
|
|
1538
|
-
suggestion: 'Each element should be a feature id (string or number)',
|
|
1539
|
-
});
|
|
1540
|
-
}
|
|
1541
|
-
}
|
|
1542
|
-
}
|
|
1543
|
-
if (focusObj.padding !== undefined && typeof focusObj.padding !== 'number') {
|
|
1544
|
-
errors.push({
|
|
1545
|
-
message: 'Spec error: geo.focus.padding must be a number',
|
|
1546
|
-
path: 'geo.focus.padding',
|
|
1547
|
-
code: 'INVALID_TYPE',
|
|
1548
|
-
suggestion: 'Provide padding as a number in map-local units, e.g. padding: 32',
|
|
1549
|
-
});
|
|
1550
|
-
}
|
|
1551
|
-
}
|
|
1552
|
-
} else {
|
|
1553
|
-
errors.push({
|
|
1554
|
-
message:
|
|
1555
|
-
'Spec error: geo.focus must be a string, number, array of ids, or object with { features, padding? } or { points: true, padding? }',
|
|
1556
|
-
path: 'geo.focus',
|
|
1557
|
-
code: 'INVALID_TYPE',
|
|
1558
|
-
suggestion:
|
|
1559
|
-
'Use a feature id, array of ids, { features: "36", padding: 16 }, or { points: true }. Pass null to clear focus.',
|
|
1560
|
-
});
|
|
1561
|
-
}
|
|
1562
|
-
}
|
|
1563
|
-
|
|
1564
|
-
const encoding = spec.encoding as Record<string, unknown>;
|
|
1565
|
-
const hasPoints = spec.points && typeof spec.points === 'object' && !Array.isArray(spec.points);
|
|
1566
|
-
|
|
1567
|
-
// key is required unless basemap-only mode (points present + empty/no choropleth data)
|
|
1568
|
-
const specData = spec.data as unknown[] | undefined;
|
|
1569
|
-
const isBasemapOnly = hasPoints && (!specData || specData.length === 0);
|
|
1570
|
-
const keyCh = encoding.key as Record<string, unknown> | undefined;
|
|
1571
|
-
if (!isBasemapOnly) {
|
|
1572
|
-
if (!keyCh || typeof keyCh !== 'object') {
|
|
1573
|
-
errors.push({
|
|
1574
|
-
message: 'Spec error: map encoding requires "key" channel',
|
|
1575
|
-
path: 'encoding.key',
|
|
1576
|
-
code: 'MISSING_FIELD',
|
|
1577
|
-
suggestion: 'Add encoding.key, e.g. key: { field: "id", type: "nominal" }',
|
|
1578
|
-
});
|
|
1579
|
-
} else if (!keyCh.field || typeof keyCh.field !== 'string') {
|
|
1580
|
-
errors.push({
|
|
1581
|
-
message: 'Spec error: encoding.key must have a "field" string',
|
|
1582
|
-
path: 'encoding.key.field',
|
|
1583
|
-
code: 'MISSING_FIELD',
|
|
1584
|
-
suggestion: 'Add a field name, e.g. key: { field: "id" }',
|
|
1585
|
-
});
|
|
1586
|
-
}
|
|
1587
|
-
}
|
|
1588
|
-
|
|
1589
|
-
// color is required UNLESS points is present (basemap-only mode)
|
|
1590
|
-
const colorCh = encoding.color as Record<string, unknown> | undefined;
|
|
1591
|
-
if (!hasPoints) {
|
|
1592
|
-
if (!colorCh || typeof colorCh !== 'object') {
|
|
1593
|
-
errors.push({
|
|
1594
|
-
message:
|
|
1595
|
-
'Spec error: map encoding requires "color" channel (or add a "points" layer for basemap-only mode)',
|
|
1596
|
-
path: 'encoding.color',
|
|
1597
|
-
code: 'MISSING_FIELD',
|
|
1598
|
-
suggestion: 'Add encoding.color, e.g. color: { field: "value", type: "quantitative" }',
|
|
1599
|
-
});
|
|
1600
|
-
} else if (!colorCh.field || typeof colorCh.field !== 'string') {
|
|
1601
|
-
errors.push({
|
|
1602
|
-
message: 'Spec error: encoding.color must have a "field" string',
|
|
1603
|
-
path: 'encoding.color.field',
|
|
1604
|
-
code: 'MISSING_FIELD',
|
|
1605
|
-
suggestion: 'Add a field name, e.g. color: { field: "value" }',
|
|
1606
|
-
});
|
|
1607
|
-
}
|
|
1608
|
-
} else if (
|
|
1609
|
-
colorCh &&
|
|
1610
|
-
typeof colorCh === 'object' &&
|
|
1611
|
-
(!colorCh.field || typeof colorCh.field !== 'string')
|
|
1612
|
-
) {
|
|
1613
|
-
errors.push({
|
|
1614
|
-
message: 'Spec error: encoding.color must have a "field" string when provided',
|
|
1615
|
-
path: 'encoding.color.field',
|
|
1616
|
-
code: 'MISSING_FIELD',
|
|
1617
|
-
suggestion: 'Add a field name, e.g. color: { field: "value" }',
|
|
1618
|
-
});
|
|
1619
|
-
}
|
|
1620
|
-
|
|
1621
|
-
// Validate points layer if present
|
|
1622
|
-
if (hasPoints) {
|
|
1623
|
-
const points = spec.points as Record<string, unknown>;
|
|
1624
|
-
if (!Array.isArray(points.data)) {
|
|
1625
|
-
errors.push({
|
|
1626
|
-
message: 'Spec error: points.data must be an array of data rows',
|
|
1627
|
-
path: 'points.data',
|
|
1628
|
-
code: 'INVALID_TYPE',
|
|
1629
|
-
suggestion: 'Provide data as an array of objects with lat/lng fields',
|
|
1630
|
-
});
|
|
1631
|
-
}
|
|
1632
|
-
for (const ch of ['longitude', 'latitude'] as const) {
|
|
1633
|
-
const enc = points[ch] as Record<string, unknown> | undefined;
|
|
1634
|
-
if (!enc || typeof enc !== 'object') {
|
|
1635
|
-
errors.push({
|
|
1636
|
-
message: `Spec error: points requires "${ch}" encoding channel`,
|
|
1637
|
-
path: `points.${ch}`,
|
|
1638
|
-
code: 'MISSING_FIELD',
|
|
1639
|
-
suggestion: `Add points.${ch}, e.g. ${ch}: { field: "${ch === 'longitude' ? 'lon' : 'lat'}", type: "quantitative" }`,
|
|
1640
|
-
});
|
|
1641
|
-
} else if (!enc.field || typeof enc.field !== 'string') {
|
|
1642
|
-
errors.push({
|
|
1643
|
-
message: `Spec error: points.${ch} must have a "field" string`,
|
|
1644
|
-
path: `points.${ch}.field`,
|
|
1645
|
-
code: 'MISSING_FIELD',
|
|
1646
|
-
suggestion: `Add a field name, e.g. ${ch}: { field: "${ch === 'longitude' ? 'lon' : 'lat'}" }`,
|
|
1647
|
-
});
|
|
1648
|
-
}
|
|
1649
|
-
}
|
|
1650
|
-
for (const ch of ['size', 'color', 'key'] as const) {
|
|
1651
|
-
const enc = points[ch] as Record<string, unknown> | undefined;
|
|
1652
|
-
if (enc && typeof enc === 'object' && (!enc.field || typeof enc.field !== 'string')) {
|
|
1653
|
-
errors.push({
|
|
1654
|
-
message: `Spec error: points.${ch} must have a "field" string when provided`,
|
|
1655
|
-
path: `points.${ch}.field`,
|
|
1656
|
-
code: 'MISSING_FIELD',
|
|
1657
|
-
suggestion: `Add a field name, e.g. ${ch}: { field: "myField" }`,
|
|
1658
|
-
});
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1661
|
-
}
|
|
1662
|
-
}
|
|
1663
|
-
|
|
1664
|
-
// ---------------------------------------------------------------------------
|
|
1665
|
-
// BarList validation
|
|
1666
|
-
// ---------------------------------------------------------------------------
|
|
1667
|
-
|
|
1668
|
-
function validateBarListSpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
1669
|
-
if (!Array.isArray(spec.data)) {
|
|
1670
|
-
errors.push({
|
|
1671
|
-
message: 'Spec error: barlist spec requires a "data" array',
|
|
1672
|
-
path: 'data',
|
|
1673
|
-
code: 'INVALID_TYPE',
|
|
1674
|
-
suggestion:
|
|
1675
|
-
'Provide data as an array of objects, e.g. data: [{ label: "Category A", value: 42 }]',
|
|
1676
|
-
});
|
|
1677
|
-
return;
|
|
1678
|
-
}
|
|
1679
|
-
|
|
1680
|
-
if (spec.data.length === 0) {
|
|
1681
|
-
errors.push({
|
|
1682
|
-
message: 'Spec error: "data" array must be non-empty',
|
|
1683
|
-
path: 'data',
|
|
1684
|
-
code: 'EMPTY_DATA',
|
|
1685
|
-
suggestion: 'Add at least one data row',
|
|
1686
|
-
});
|
|
1687
|
-
return;
|
|
1688
|
-
}
|
|
1689
|
-
|
|
1690
|
-
const firstRow = spec.data[0] as unknown;
|
|
1691
|
-
if (typeof firstRow !== 'object' || firstRow === null || Array.isArray(firstRow)) {
|
|
1692
|
-
errors.push({
|
|
1693
|
-
message: 'Spec error: each item in "data" must be a plain object',
|
|
1694
|
-
path: 'data[0]',
|
|
1695
|
-
code: 'INVALID_TYPE',
|
|
1696
|
-
suggestion: 'Each data item should be an object, e.g. { label: "Category A", value: 42 }',
|
|
1697
|
-
});
|
|
1698
|
-
return;
|
|
1699
|
-
}
|
|
1700
|
-
|
|
1701
|
-
if (!spec.encoding || typeof spec.encoding !== 'object') {
|
|
1702
|
-
errors.push({
|
|
1703
|
-
message:
|
|
1704
|
-
'Spec error: barlist spec requires an "encoding" object with label and value channels',
|
|
1705
|
-
path: 'encoding',
|
|
1706
|
-
code: 'MISSING_FIELD',
|
|
1707
|
-
suggestion:
|
|
1708
|
-
'Add an encoding object, e.g. encoding: { label: { field: "name", type: "nominal" }, value: { field: "count", type: "quantitative" } }',
|
|
1709
|
-
});
|
|
1710
|
-
return;
|
|
1711
|
-
}
|
|
1712
|
-
|
|
1713
|
-
const encoding = spec.encoding as Record<string, unknown>;
|
|
1714
|
-
const dataColumns = new Set(Object.keys(firstRow as Record<string, unknown>));
|
|
1715
|
-
const columnList = [...dataColumns];
|
|
1716
|
-
const availableColumns = columnList.join(', ');
|
|
1717
|
-
|
|
1718
|
-
for (const channel of ['label', 'value'] as const) {
|
|
1719
|
-
const ch = encoding[channel] as Record<string, unknown> | undefined;
|
|
1720
|
-
if (!ch || typeof ch !== 'object') {
|
|
1721
|
-
errors.push({
|
|
1722
|
-
message: `Spec error: barlist encoding requires "${channel}" channel`,
|
|
1723
|
-
path: `encoding.${channel}`,
|
|
1724
|
-
code: 'MISSING_FIELD',
|
|
1725
|
-
suggestion: `Add encoding.${channel} with a field from your data (${availableColumns}). Example: ${channel}: { field: "${columnList[0] ?? 'myField'}", type: "${channel === 'value' ? 'quantitative' : 'nominal'}" }`,
|
|
1726
|
-
});
|
|
1727
|
-
continue;
|
|
1728
|
-
}
|
|
1729
|
-
|
|
1730
|
-
if (!ch.field || typeof ch.field !== 'string') {
|
|
1731
|
-
errors.push({
|
|
1732
|
-
message: `Spec error: encoding.${channel} must have a "field" string`,
|
|
1733
|
-
path: `encoding.${channel}.field`,
|
|
1734
|
-
code: 'MISSING_FIELD',
|
|
1735
|
-
suggestion: `Add a field name from your data columns: ${availableColumns}`,
|
|
1736
|
-
});
|
|
1737
|
-
continue;
|
|
1738
|
-
}
|
|
1739
|
-
|
|
1740
|
-
if (!dataColumns.has(ch.field as string)) {
|
|
1741
|
-
errors.push({
|
|
1742
|
-
message: `Spec error: encoding.${channel}.field "${ch.field}" does not exist in data. Available columns: ${availableColumns}`,
|
|
1743
|
-
path: `encoding.${channel}.field`,
|
|
1744
|
-
code: 'DATA_FIELD_MISSING',
|
|
1745
|
-
suggestion: `Use one of the available data columns: ${availableColumns}.${didYouMean(ch.field as string, columnList)}`,
|
|
1746
|
-
});
|
|
1747
|
-
}
|
|
1748
|
-
}
|
|
1749
|
-
|
|
1750
|
-
// Validate optional encoding channels
|
|
1751
|
-
for (const channel of ['subtitle', 'color', 'tooltip'] as const) {
|
|
1752
|
-
const ch = encoding[channel] as Record<string, unknown> | undefined;
|
|
1753
|
-
if (!ch) continue;
|
|
1754
|
-
const field = ch.field;
|
|
1755
|
-
if (field && typeof field === 'string' && !dataColumns.has(field)) {
|
|
1756
|
-
errors.push({
|
|
1757
|
-
message: `Spec error: encoding.${channel}.field "${field}" does not exist in data. Available columns: ${availableColumns}`,
|
|
1758
|
-
path: `encoding.${channel}.field`,
|
|
1759
|
-
code: 'DATA_FIELD_MISSING',
|
|
1760
|
-
suggestion: `Use one of the available data columns: ${availableColumns}.${didYouMean(field, columnList)}`,
|
|
1761
|
-
});
|
|
1762
|
-
}
|
|
1763
|
-
}
|
|
1764
|
-
|
|
1765
|
-
// Barlist never reads scale.scheme: bars cycle the built-in barlist palette
|
|
1766
|
-
checkSchemeUnused(
|
|
1767
|
-
encoding,
|
|
1768
|
-
errors,
|
|
1769
|
-
'bar list colors cycle the built-in barlist palette',
|
|
1770
|
-
'Remove scale.scheme. Bar list colors are not configurable via encoding; they cycle the built-in palette.',
|
|
1771
|
-
);
|
|
1772
|
-
|
|
1773
|
-
if (spec.darkMode !== undefined && !VALID_DARK_MODES.has(spec.darkMode as string)) {
|
|
1774
|
-
errors.push({
|
|
1775
|
-
message: 'Spec error: darkMode must be "auto", "force", or "off"',
|
|
1776
|
-
path: 'darkMode',
|
|
1777
|
-
code: 'INVALID_VALUE',
|
|
1778
|
-
suggestion:
|
|
1779
|
-
'Use one of: "auto" (system preference), "force" (always dark), or "off" (always light)',
|
|
1780
|
-
});
|
|
1781
|
-
}
|
|
1782
|
-
}
|
|
1783
|
-
|
|
1784
|
-
// ---------------------------------------------------------------------------
|
|
1785
|
-
// Layer validation
|
|
1786
|
-
// ---------------------------------------------------------------------------
|
|
1787
|
-
|
|
1788
|
-
function validateLayerSpec(spec: Record<string, unknown>, errors: ValidationError[]): void {
|
|
1789
|
-
const layer = spec.layer as unknown[];
|
|
1790
|
-
|
|
1791
|
-
if (layer.length === 0) {
|
|
1792
|
-
errors.push({
|
|
1793
|
-
message: 'Spec error: "layer" must be a non-empty array',
|
|
1794
|
-
path: 'layer',
|
|
1795
|
-
code: 'EMPTY_DATA',
|
|
1796
|
-
suggestion: 'Add at least one layer with a mark and encoding',
|
|
1797
|
-
});
|
|
1798
|
-
return;
|
|
1799
|
-
}
|
|
1800
|
-
|
|
1801
|
-
for (let i = 0; i < layer.length; i++) {
|
|
1802
|
-
const child = layer[i];
|
|
1803
|
-
if (!child || typeof child !== 'object' || Array.isArray(child)) {
|
|
1804
|
-
errors.push({
|
|
1805
|
-
message: `Spec error: layer[${i}] must be an object`,
|
|
1806
|
-
path: `layer[${i}]`,
|
|
1807
|
-
code: 'INVALID_TYPE',
|
|
1808
|
-
suggestion:
|
|
1809
|
-
'Each layer must be a chart spec (with mark) or a nested layer spec (with layer)',
|
|
1810
|
-
});
|
|
1811
|
-
continue;
|
|
1812
|
-
}
|
|
1813
|
-
|
|
1814
|
-
const childObj = child as Record<string, unknown>;
|
|
1815
|
-
const isNestedLayer = 'layer' in childObj && Array.isArray(childObj.layer);
|
|
1816
|
-
const isChildChart = 'mark' in childObj;
|
|
1817
|
-
|
|
1818
|
-
if (!isNestedLayer && !isChildChart) {
|
|
1819
|
-
errors.push({
|
|
1820
|
-
message: `Spec error: layer[${i}] must have a "mark" field or a "layer" array`,
|
|
1821
|
-
path: `layer[${i}]`,
|
|
1822
|
-
code: 'MISSING_FIELD',
|
|
1823
|
-
suggestion:
|
|
1824
|
-
'Each layer must be a chart spec (with mark + encoding) or a nested layer spec (with layer array)',
|
|
1825
|
-
});
|
|
1826
|
-
continue;
|
|
1827
|
-
}
|
|
1828
|
-
|
|
1829
|
-
if (isNestedLayer) {
|
|
1830
|
-
validateLayerSpec(childObj, errors);
|
|
1831
|
-
} else if (isChildChart) {
|
|
1832
|
-
// Validate mark type
|
|
1833
|
-
const mark = childObj.mark;
|
|
1834
|
-
let markValue: string | undefined;
|
|
1835
|
-
if (typeof mark === 'string') {
|
|
1836
|
-
markValue = mark;
|
|
1837
|
-
} else if (mark && typeof mark === 'object' && !Array.isArray(mark)) {
|
|
1838
|
-
markValue = (mark as Record<string, unknown>).type as string | undefined;
|
|
1839
|
-
}
|
|
1840
|
-
|
|
1841
|
-
if (!markValue || !MARK_TYPES.has(markValue)) {
|
|
1842
|
-
errors.push({
|
|
1843
|
-
message: `Spec error: layer[${i}].mark "${markValue ?? String(mark)}" is not a valid mark type`,
|
|
1844
|
-
path: `layer[${i}].mark`,
|
|
1845
|
-
code: 'INVALID_VALUE',
|
|
1846
|
-
suggestion: `Change mark to one of: ${[...MARK_TYPES].join(', ')}`,
|
|
1847
|
-
});
|
|
1848
|
-
continue;
|
|
1849
|
-
}
|
|
1850
|
-
|
|
1851
|
-
// Child layers can inherit data and encoding from parent, so only validate
|
|
1852
|
-
// if the child has its own data (or the parent provides shared data).
|
|
1853
|
-
const hasOwnData = Array.isArray(childObj.data) && (childObj.data as unknown[]).length > 0;
|
|
1854
|
-
const parentHasData = Array.isArray(spec.data) && (spec.data as unknown[]).length > 0;
|
|
1855
|
-
|
|
1856
|
-
if (hasOwnData || parentHasData) {
|
|
1857
|
-
// Build a merged spec for validation purposes
|
|
1858
|
-
const mergedForValidation = { ...childObj };
|
|
1859
|
-
if (!hasOwnData && parentHasData) {
|
|
1860
|
-
mergedForValidation.data = spec.data;
|
|
1861
|
-
}
|
|
1862
|
-
// Merge encoding: parent fields are inherited unless child overrides
|
|
1863
|
-
if (spec.encoding && typeof spec.encoding === 'object') {
|
|
1864
|
-
mergedForValidation.encoding = {
|
|
1865
|
-
...(spec.encoding as Record<string, unknown>),
|
|
1866
|
-
...((childObj.encoding as Record<string, unknown>) ?? {}),
|
|
1867
|
-
};
|
|
1868
|
-
}
|
|
1869
|
-
if (mergedForValidation.data && mergedForValidation.encoding) {
|
|
1870
|
-
validateChartSpec(mergedForValidation, errors);
|
|
1871
|
-
}
|
|
1872
|
-
}
|
|
1873
|
-
}
|
|
1874
|
-
}
|
|
1875
|
-
}
|
|
1876
|
-
|
|
1877
|
-
// ---------------------------------------------------------------------------
|
|
1878
|
-
// Public API
|
|
1879
|
-
// ---------------------------------------------------------------------------
|
|
1880
|
-
|
|
1881
|
-
/**
|
|
1882
|
-
* Validate a spec at runtime.
|
|
1883
|
-
*
|
|
1884
|
-
* Checks structure, required fields, encoding rules, data shape, and
|
|
1885
|
-
* field type compatibility. Returns structured errors with machine-readable
|
|
1886
|
-
* codes and actionable suggestions for each problem found.
|
|
1887
|
-
*/
|
|
1888
|
-
export function validateSpec(spec: unknown): ValidationResult {
|
|
1889
|
-
const errors: ValidationError[] = [];
|
|
1890
|
-
|
|
1891
|
-
// Basic shape check
|
|
1892
|
-
if (!spec || typeof spec !== 'object' || Array.isArray(spec)) {
|
|
1893
|
-
return {
|
|
1894
|
-
valid: false,
|
|
1895
|
-
errors: [
|
|
1896
|
-
{
|
|
1897
|
-
message: 'Spec error: spec must be a non-null object',
|
|
1898
|
-
code: 'INVALID_TYPE',
|
|
1899
|
-
suggestion:
|
|
1900
|
-
'Pass a spec object with at least a "mark" field for charts, e.g. { mark: "line", data: [...], encoding: {...} }',
|
|
1901
|
-
},
|
|
1902
|
-
],
|
|
1903
|
-
normalized: null,
|
|
1904
|
-
};
|
|
1905
|
-
}
|
|
1906
|
-
|
|
1907
|
-
const obj = spec as Record<string, unknown>;
|
|
1908
|
-
|
|
1909
|
-
// Determine spec type via structural discrimination:
|
|
1910
|
-
// - Layer specs have a 'layer' array
|
|
1911
|
-
// - Chart specs have a 'mark' field (string or object with type property)
|
|
1912
|
-
// - Table specs have type: 'table'
|
|
1913
|
-
// - Graph specs have type: 'graph'
|
|
1914
|
-
// - Sankey specs have type: 'sankey'
|
|
1915
|
-
// - TileMap specs have type: 'tilemap'
|
|
1916
|
-
const hasLayer = 'layer' in obj && Array.isArray(obj.layer);
|
|
1917
|
-
const hasMark = 'mark' in obj;
|
|
1918
|
-
const isTable = obj.type === 'table';
|
|
1919
|
-
const isGraph = obj.type === 'graph';
|
|
1920
|
-
const isSankey = obj.type === 'sankey';
|
|
1921
|
-
const isTileMap = obj.type === 'tilemap';
|
|
1922
|
-
const isMap = obj.type === 'map';
|
|
1923
|
-
const isBarList = obj.type === 'barlist';
|
|
1924
|
-
const isLayer =
|
|
1925
|
-
hasLayer && !isTable && !isGraph && !isSankey && !isTileMap && !isMap && !isBarList;
|
|
1926
|
-
const isChart =
|
|
1927
|
-
hasMark && !hasLayer && !isTable && !isGraph && !isSankey && !isTileMap && !isMap && !isBarList;
|
|
1928
|
-
|
|
1929
|
-
if (
|
|
1930
|
-
!isChart &&
|
|
1931
|
-
!isTable &&
|
|
1932
|
-
!isGraph &&
|
|
1933
|
-
!isSankey &&
|
|
1934
|
-
!isTileMap &&
|
|
1935
|
-
!isMap &&
|
|
1936
|
-
!isBarList &&
|
|
1937
|
-
!isLayer
|
|
1938
|
-
) {
|
|
1939
|
-
// Near-misses for VL composition operators that are unsupported by decision
|
|
1940
|
-
if ('hconcat' in obj || 'vconcat' in obj) {
|
|
1941
|
-
const key = 'hconcat' in obj ? 'hconcat' : 'vconcat';
|
|
1942
|
-
return {
|
|
1943
|
-
valid: false,
|
|
1944
|
-
errors: [
|
|
1945
|
-
{
|
|
1946
|
-
message: `Spec error: "${key}" composition is not supported`,
|
|
1947
|
-
path: key,
|
|
1948
|
-
code: 'INVALID_VALUE',
|
|
1949
|
-
suggestion:
|
|
1950
|
-
'Render each chart in its own container instead. For small multiples of the same chart, use the facet encoding channel: encoding.facet = { field, type }',
|
|
1951
|
-
},
|
|
1952
|
-
],
|
|
1953
|
-
normalized: null,
|
|
1954
|
-
};
|
|
1955
|
-
}
|
|
1956
|
-
if ('facet' in obj && 'spec' in obj) {
|
|
1957
|
-
return {
|
|
1958
|
-
valid: false,
|
|
1959
|
-
errors: [
|
|
1960
|
-
{
|
|
1961
|
-
message: 'Spec error: the top-level "facet" operator is not supported',
|
|
1962
|
-
path: 'facet',
|
|
1963
|
-
code: 'INVALID_VALUE',
|
|
1964
|
-
suggestion:
|
|
1965
|
-
'Use the facet encoding channel on a regular chart spec instead: encoding.facet = { field: "category", type: "nominal" }',
|
|
1966
|
-
},
|
|
1967
|
-
],
|
|
1968
|
-
normalized: null,
|
|
1969
|
-
};
|
|
1970
|
-
}
|
|
1971
|
-
return {
|
|
1972
|
-
valid: false,
|
|
1973
|
-
errors: [
|
|
1974
|
-
{
|
|
1975
|
-
message:
|
|
1976
|
-
'Spec error: spec must have a "mark" field for charts, a "layer" array for layered charts, or a "type" field for tables/graphs/sankey/tilemap/map/barlist',
|
|
1977
|
-
path: 'mark',
|
|
1978
|
-
code: 'MISSING_FIELD',
|
|
1979
|
-
suggestion: `Add a "mark" field for charts (e.g. mark: "bar"), a "layer" array for layered charts, or a "type" field (type: "table", type: "graph", type: "sankey", type: "tilemap", type: "map", or type: "barlist"). Valid mark types: ${[...MARK_TYPES].join(', ')}`,
|
|
1980
|
-
},
|
|
1981
|
-
],
|
|
1982
|
-
normalized: null,
|
|
1983
|
-
};
|
|
1984
|
-
}
|
|
1985
|
-
|
|
1986
|
-
// For layer specs, validate each child layer recursively
|
|
1987
|
-
if (isLayer) {
|
|
1988
|
-
validateLayerSpec(obj, errors);
|
|
1989
|
-
}
|
|
1990
|
-
|
|
1991
|
-
// For chart specs, validate the mark field
|
|
1992
|
-
if (isChart) {
|
|
1993
|
-
const mark = obj.mark;
|
|
1994
|
-
let markValue: string | undefined;
|
|
1995
|
-
|
|
1996
|
-
if (typeof mark === 'string') {
|
|
1997
|
-
markValue = mark;
|
|
1998
|
-
} else if (mark && typeof mark === 'object' && !Array.isArray(mark)) {
|
|
1999
|
-
markValue = (mark as Record<string, unknown>).type as string | undefined;
|
|
2000
|
-
}
|
|
2001
|
-
|
|
2002
|
-
if (!markValue || !MARK_TYPES.has(markValue)) {
|
|
2003
|
-
return {
|
|
2004
|
-
valid: false,
|
|
2005
|
-
errors: [
|
|
2006
|
-
{
|
|
2007
|
-
message: `Spec error: "${markValue ?? String(mark)}" is not a valid mark type. Valid mark types: ${[...MARK_TYPES].join(', ')}`,
|
|
2008
|
-
path: 'mark',
|
|
2009
|
-
code: 'INVALID_VALUE',
|
|
2010
|
-
suggestion: `Change mark to one of: ${[...MARK_TYPES].join(', ')}`,
|
|
2011
|
-
},
|
|
2012
|
-
],
|
|
2013
|
-
normalized: null,
|
|
2014
|
-
};
|
|
2015
|
-
}
|
|
2016
|
-
|
|
2017
|
-
validateChartSpec(obj, errors);
|
|
2018
|
-
} else if (isTable) {
|
|
2019
|
-
validateTableSpec(obj, errors);
|
|
2020
|
-
} else if (isGraph) {
|
|
2021
|
-
validateGraphSpec(obj, errors);
|
|
2022
|
-
} else if (isSankey) {
|
|
2023
|
-
validateSankeySpec(obj, errors);
|
|
2024
|
-
} else if (isTileMap) {
|
|
2025
|
-
validateTileMapSpec(obj, errors);
|
|
2026
|
-
} else if (isMap) {
|
|
2027
|
-
validateMapSpec(obj, errors);
|
|
2028
|
-
} else if (isBarList) {
|
|
2029
|
-
validateBarListSpec(obj, errors);
|
|
2030
|
-
}
|
|
2031
|
-
|
|
2032
|
-
if (errors.length > 0) {
|
|
2033
|
-
return { valid: false, errors, normalized: null };
|
|
2034
|
-
}
|
|
2035
|
-
|
|
2036
|
-
return {
|
|
2037
|
-
valid: true,
|
|
2038
|
-
errors: [],
|
|
2039
|
-
normalized: spec as VizSpec,
|
|
2040
|
-
};
|
|
2041
|
-
}
|