@terreno/blocks 57.10.1

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.
Files changed (237) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +62 -0
  3. package/biome.jsonc +18 -0
  4. package/bunfig.toml +6 -0
  5. package/dist/asks/errors.d.ts +77 -0
  6. package/dist/asks/errors.js +364 -0
  7. package/dist/asks/files.d.ts +53 -0
  8. package/dist/asks/files.js +171 -0
  9. package/dist/asks/formValues.d.ts +32 -0
  10. package/dist/asks/formValues.js +229 -0
  11. package/dist/asks/headless.d.ts +227 -0
  12. package/dist/asks/headless.js +94 -0
  13. package/dist/asks/jsonSchema.d.ts +6 -0
  14. package/dist/asks/jsonSchema.js +62 -0
  15. package/dist/asks/limits.d.ts +58 -0
  16. package/dist/asks/limits.js +61 -0
  17. package/dist/asks/prompt.d.ts +10 -0
  18. package/dist/asks/prompt.js +144 -0
  19. package/dist/asks/schema.d.ts +826 -0
  20. package/dist/asks/schema.js +864 -0
  21. package/dist/asks/simpleCard.d.ts +85 -0
  22. package/dist/asks/simpleCard.js +279 -0
  23. package/dist/asks/validateInput.d.ts +13 -0
  24. package/dist/asks/validateInput.js +19 -0
  25. package/dist/asks/validateResponse.d.ts +13 -0
  26. package/dist/asks/validateResponse.js +278 -0
  27. package/dist/cli.d.ts +11 -0
  28. package/dist/cli.js +58 -0
  29. package/dist/errors.d.ts +51 -0
  30. package/dist/errors.js +106 -0
  31. package/dist/index.d.ts +31 -0
  32. package/dist/index.js +111 -0
  33. package/dist/jsonSchema.d.ts +5 -0
  34. package/dist/jsonSchema.js +12 -0
  35. package/dist/limits.d.ts +27 -0
  36. package/dist/limits.js +30 -0
  37. package/dist/lint.d.ts +21 -0
  38. package/dist/lint.js +388 -0
  39. package/dist/parse.d.ts +16 -0
  40. package/dist/parse.js +95 -0
  41. package/dist/parsePartial.d.ts +12 -0
  42. package/dist/parsePartial.js +105 -0
  43. package/dist/prompt.d.ts +9 -0
  44. package/dist/prompt.js +95 -0
  45. package/dist/schema.d.ts +199 -0
  46. package/dist/schema.js +277 -0
  47. package/dist/validate.d.ts +17 -0
  48. package/dist/validate.js +331 -0
  49. package/package.json +60 -0
  50. package/schemas/pendingAsksResponse.schema.json +195 -0
  51. package/schemas/simpleCard.schema.json +135 -0
  52. package/schemas/turnRequest.schema.json +108 -0
  53. package/schemas/turnResponse.schema.json +204 -0
  54. package/scripts/writeJsonSchemas.ts +11 -0
  55. package/src/asks/docParity.test.ts +65 -0
  56. package/src/asks/errors.test.ts +250 -0
  57. package/src/asks/errors.ts +449 -0
  58. package/src/asks/files.test.ts +294 -0
  59. package/src/asks/files.ts +216 -0
  60. package/src/asks/fixtures/invalid/choice-blank-text.json +20 -0
  61. package/src/asks/fixtures/invalid/choice-default-not-in-options.json +13 -0
  62. package/src/asks/fixtures/invalid/choice-default-selects-two.json +16 -0
  63. package/src/asks/fixtures/invalid/choice-description-too-long.json +24 -0
  64. package/src/asks/fixtures/invalid/choice-duplicate-ids.json +17 -0
  65. package/src/asks/fixtures/invalid/choice-label-too-long.json +23 -0
  66. package/src/asks/fixtures/invalid/choice-many-bounds-not-whole.json +17 -0
  67. package/src/asks/fixtures/invalid/choice-many-bounds-out-of-range.json +17 -0
  68. package/src/asks/fixtures/invalid/choice-many-default-duplicate.json +17 -0
  69. package/src/asks/fixtures/invalid/choice-many-default-too-many.json +15 -0
  70. package/src/asks/fixtures/invalid/choice-many-max-above-options.json +14 -0
  71. package/src/asks/fixtures/invalid/choice-many-min-above-choices.json +13 -0
  72. package/src/asks/fixtures/invalid/choice-many-min-above-max.json +15 -0
  73. package/src/asks/fixtures/invalid/choice-missing-required.json +14 -0
  74. package/src/asks/fixtures/invalid/choice-mixed-errors.json +19 -0
  75. package/src/asks/fixtures/invalid/choice-not-an-object.json +5 -0
  76. package/src/asks/fixtures/invalid/choice-one-with-many-fields.json +19 -0
  77. package/src/asks/fixtures/invalid/choice-option-id-format.json +35 -0
  78. package/src/asks/fixtures/invalid/choice-options-not-objects.json +12 -0
  79. package/src/asks/fixtures/invalid/choice-other-label-too-long.json +25 -0
  80. package/src/asks/fixtures/invalid/choice-other-label-without-allow-other.json +14 -0
  81. package/src/asks/fixtures/invalid/choice-prompt-empty.json +12 -0
  82. package/src/asks/fixtures/invalid/choice-prompt-too-long.json +23 -0
  83. package/src/asks/fixtures/invalid/choice-select-unknown.json +12 -0
  84. package/src/asks/fixtures/invalid/choice-submit-label-too-long.json +13 -0
  85. package/src/asks/fixtures/invalid/choice-title-too-long.json +24 -0
  86. package/src/asks/fixtures/invalid/choice-too-few-options.json +9 -0
  87. package/src/asks/fixtures/invalid/choice-too-many-options.json +219 -0
  88. package/src/asks/fixtures/invalid/choice-unknown-key-option.json +15 -0
  89. package/src/asks/fixtures/invalid/choice-unknown-key-root.json +13 -0
  90. package/src/asks/fixtures/invalid/choice-wrong-types.json +16 -0
  91. package/src/asks/fixtures/invalid/confirm-emoji-label-too-long.json +13 -0
  92. package/src/asks/fixtures/invalid/confirm-label-too-long.json +14 -0
  93. package/src/asks/fixtures/invalid/confirm-labels-alike-after-trim.json +14 -0
  94. package/src/asks/fixtures/invalid/confirm-labels-alike.json +13 -0
  95. package/src/asks/fixtures/invalid/confirm-missing-prompt-blank-label.json +16 -0
  96. package/src/asks/fixtures/invalid/confirm-unknown-keys.json +23 -0
  97. package/src/asks/fixtures/invalid/confirm-wrong-types.json +18 -0
  98. package/src/asks/fixtures/invalid/files-accept-duplicate.json +13 -0
  99. package/src/asks/fixtures/invalid/files-accept-empty.json +13 -0
  100. package/src/asks/fixtures/invalid/files-accept-unknown.json +13 -0
  101. package/src/asks/fixtures/invalid/files-counts-out-of-range.json +19 -0
  102. package/src/asks/fixtures/invalid/files-min-above-max.json +15 -0
  103. package/src/asks/fixtures/invalid/files-missing-accept-unknown-key.json +17 -0
  104. package/src/asks/fixtures/invalid/form-default-bad-format.json +40 -0
  105. package/src/asks/fixtures/invalid/form-default-invalid-date.json +40 -0
  106. package/src/asks/fixtures/invalid/form-default-not-in-options.json +58 -0
  107. package/src/asks/fixtures/invalid/form-default-out-of-range.json +33 -0
  108. package/src/asks/fixtures/invalid/form-default-text-bounds.json +42 -0
  109. package/src/asks/fixtures/invalid/form-default-wrong-type.json +30 -0
  110. package/src/asks/fixtures/invalid/form-duplicate-field-ids.json +24 -0
  111. package/src/asks/fixtures/invalid/form-field-id-format.json +19 -0
  112. package/src/asks/fixtures/invalid/form-field-text-limits.json +33 -0
  113. package/src/asks/fixtures/invalid/form-keys-of-another-type.json +39 -0
  114. package/src/asks/fixtures/invalid/form-length-above-cap.json +30 -0
  115. package/src/asks/fixtures/invalid/form-min-above-max.json +32 -0
  116. package/src/asks/fixtures/invalid/form-no-fields.json +13 -0
  117. package/src/asks/fixtures/invalid/form-select-options.json +44 -0
  118. package/src/asks/fixtures/invalid/form-too-many-fields.json +59 -0
  119. package/src/asks/fixtures/invalid/form-unknown-field-type.json +27 -0
  120. package/src/asks/fixtures/invalid/markdown-bounds-out-of-range.json +18 -0
  121. package/src/asks/fixtures/invalid/markdown-initial-above-cap.json +13 -0
  122. package/src/asks/fixtures/invalid/markdown-max-above-cap.json +13 -0
  123. package/src/asks/fixtures/invalid/markdown-min-above-cap.json +13 -0
  124. package/src/asks/fixtures/invalid/markdown-min-above-max.json +14 -0
  125. package/src/asks/fixtures/invalid/markdown-placeholder-too-long.json +13 -0
  126. package/src/asks/fixtures/invalid/markdown-wrong-types-unknown-key.json +27 -0
  127. package/src/asks/fixtures/valid/choice-emoji-label-cut.json +32 -0
  128. package/src/asks/fixtures/valid/choice-labels-collide-after-cut.json +21 -0
  129. package/src/asks/fixtures/valid/choice-long-text-cut.json +41 -0
  130. package/src/asks/fixtures/valid/choice-many-default-below-min.json +24 -0
  131. package/src/asks/fixtures/valid/choice-many-every-choice-with-other.json +24 -0
  132. package/src/asks/fixtures/valid/choice-many-no-default.json +21 -0
  133. package/src/asks/fixtures/valid/choice-many-optional-no-buttons.json +20 -0
  134. package/src/asks/fixtures/valid/choice-many-options-default-label-collides.json +23 -0
  135. package/src/asks/fixtures/valid/choice-many-options-no-buttons.json +21 -0
  136. package/src/asks/fixtures/valid/choice-many-options-no-default.json +22 -0
  137. package/src/asks/fixtures/valid/choice-many-options-with-default.json +30 -0
  138. package/src/asks/fixtures/valid/choice-many-required-with-default.json +29 -0
  139. package/src/asks/fixtures/valid/choice-many-single-pick-or-other.json +29 -0
  140. package/src/asks/fixtures/valid/choice-many-toppings-with-default.json +36 -0
  141. package/src/asks/fixtures/valid/choice-max-limits.json +245 -0
  142. package/src/asks/fixtures/valid/choice-plan-with-default.json +42 -0
  143. package/src/asks/fixtures/valid/choice-two-options-no-decline.json +32 -0
  144. package/src/asks/fixtures/valid/choice-two-options.json +32 -0
  145. package/src/asks/fixtures/valid/confirm-allow-decline.json +40 -0
  146. package/src/asks/fixtures/valid/confirm-default-labels.json +36 -0
  147. package/src/asks/fixtures/valid/confirm-destructive-delete.json +41 -0
  148. package/src/asks/fixtures/valid/confirm-emoji-labels.json +38 -0
  149. package/src/asks/fixtures/valid/confirm-long-text-cut.json +40 -0
  150. package/src/asks/fixtures/valid/files-every-type.json +28 -0
  151. package/src/asks/fixtures/valid/files-one-csv-no-decline.json +16 -0
  152. package/src/asks/fixtures/valid/files-receipt-photos.json +24 -0
  153. package/src/asks/fixtures/valid/form-boolean-newsletter.json +30 -0
  154. package/src/asks/fixtures/valid/form-date-due.json +44 -0
  155. package/src/asks/fixtures/valid/form-datetime-meeting.json +44 -0
  156. package/src/asks/fixtures/valid/form-datetime-minutes-offset.json +44 -0
  157. package/src/asks/fixtures/valid/form-eight-fields.json +71 -0
  158. package/src/asks/fixtures/valid/form-email-contact.json +44 -0
  159. package/src/asks/fixtures/valid/form-invoice-details.json +68 -0
  160. package/src/asks/fixtures/valid/form-multiselect-channels.json +64 -0
  161. package/src/asks/fixtures/valid/form-number-budget.json +45 -0
  162. package/src/asks/fixtures/valid/form-phone-callback.json +44 -0
  163. package/src/asks/fixtures/valid/form-select-shipping.json +58 -0
  164. package/src/asks/fixtures/valid/form-text-required-default.json +47 -0
  165. package/src/asks/fixtures/valid/form-textarea-notes.json +23 -0
  166. package/src/asks/fixtures/valid/form-time-reminder.json +44 -0
  167. package/src/asks/fixtures/valid/form-url-website.json +30 -0
  168. package/src/asks/fixtures/valid/markdown-announcement-draft.json +39 -0
  169. package/src/asks/fixtures/valid/markdown-empty-draft.json +34 -0
  170. package/src/asks/fixtures/valid/markdown-initial-blank-below-min.json +24 -0
  171. package/src/asks/fixtures/valid/markdown-initial-over-max.json +24 -0
  172. package/src/asks/fixtures/valid/markdown-long-text-cut.json +37 -0
  173. package/src/asks/fixtures/valid/markdown-no-decline.json +28 -0
  174. package/src/asks/fixtures/valid/markdown-no-initial-with-min.json +25 -0
  175. package/src/asks/formValues.ts +320 -0
  176. package/src/asks/headless.test.ts +146 -0
  177. package/src/asks/headless.ts +130 -0
  178. package/src/asks/jsonSchema.test.ts +171 -0
  179. package/src/asks/jsonSchema.ts +89 -0
  180. package/src/asks/limits.ts +58 -0
  181. package/src/asks/prompt.test.ts +200 -0
  182. package/src/asks/prompt.ts +173 -0
  183. package/src/asks/schema.test.ts +198 -0
  184. package/src/asks/schema.ts +1203 -0
  185. package/src/asks/simpleCard.test.ts +998 -0
  186. package/src/asks/simpleCard.ts +341 -0
  187. package/src/asks/validateInput.test.ts +875 -0
  188. package/src/asks/validateInput.ts +24 -0
  189. package/src/asks/validateResponse.test.ts +740 -0
  190. package/src/asks/validateResponse.ts +341 -0
  191. package/src/blocksDocParity.test.ts +21 -0
  192. package/src/cli.test.ts +134 -0
  193. package/src/cli.ts +65 -0
  194. package/src/display.test.ts +68 -0
  195. package/src/errors.test.ts +29 -0
  196. package/src/errors.ts +125 -0
  197. package/src/fixtures/invalid/column-not-found.yaml +14 -0
  198. package/src/fixtures/invalid/column-type.yaml +14 -0
  199. package/src/fixtures/invalid/dataset-not-found.yaml +7 -0
  200. package/src/fixtures/invalid/dataset-too-large.yaml +20 -0
  201. package/src/fixtures/invalid/depth-exceeded.yaml +10 -0
  202. package/src/fixtures/invalid/duplicate-id.yaml +6 -0
  203. package/src/fixtures/invalid/invalid-enum.yaml +5 -0
  204. package/src/fixtures/invalid/key-order.yaml +4 -0
  205. package/src/fixtures/invalid/missing-required.yaml +3 -0
  206. package/src/fixtures/invalid/not-a-document.txt +1 -0
  207. package/src/fixtures/invalid/row-arity.yaml +11 -0
  208. package/src/fixtures/invalid/select-target.yaml +12 -0
  209. package/src/fixtures/invalid/table-too-wide.yaml +9 -0
  210. package/src/fixtures/invalid/too-many-points.yaml +8 -0
  211. package/src/fixtures/invalid/unknown-key.yaml +5 -0
  212. package/src/fixtures/invalid/unsupported-version.yaml +4 -0
  213. package/src/fixtures/invalid/yaml-anchor.yaml +6 -0
  214. package/src/fixtures/partial/cut-after-divider.yaml +5 -0
  215. package/src/fixtures/valid/actions.yaml +32 -0
  216. package/src/fixtures/valid/chart.yaml +20 -0
  217. package/src/fixtures/valid/layout.yaml +26 -0
  218. package/src/fixtures/valid/prose.yaml +4 -0
  219. package/src/html.test.ts +52 -0
  220. package/src/index.test.ts +114 -0
  221. package/src/index.ts +175 -0
  222. package/src/jsonSchema.test.ts +33 -0
  223. package/src/jsonSchema.ts +11 -0
  224. package/src/limits.ts +27 -0
  225. package/src/lint.test.ts +289 -0
  226. package/src/lint.ts +484 -0
  227. package/src/parse.test.ts +281 -0
  228. package/src/parse.ts +104 -0
  229. package/src/parsePartial.test.ts +53 -0
  230. package/src/parsePartial.ts +116 -0
  231. package/src/prompt.test.ts +24 -0
  232. package/src/prompt.ts +104 -0
  233. package/src/schema.ts +509 -0
  234. package/src/tests/askFixtures.ts +40 -0
  235. package/src/validate.perf.test.ts +43 -0
  236. package/src/validate.ts +369 -0
  237. package/tsconfig.json +18 -0
package/src/lint.ts ADDED
@@ -0,0 +1,484 @@
1
+ import {BLOCK_ERROR_CODES, BLOCK_WARNING_CODES, type BlockError} from "./errors";
2
+ import {BLOCK_LIMITS} from "./limits";
3
+ import type {Block, BlocksDocument, Dataset, DatasetColumn, InlineDataset} from "./schema";
4
+
5
+ export interface KnownDataset {
6
+ columns: DatasetColumn[];
7
+ }
8
+
9
+ export interface LintBlocksOptions {
10
+ /** When false or omitted, an `html` block fails with `HTML_DISABLED`. */
11
+ allowHtml?: boolean;
12
+ hostActions?: readonly string[];
13
+ /** Hostnames allowed on `https` image `src` values. Empty means no https images. */
14
+ imageHosts?: readonly string[];
15
+ knownDatasets?: Record<string, KnownDataset>;
16
+ }
17
+
18
+ const issue = ({code, fix, message, path}: BlockError): BlockError => ({code, fix, message, path});
19
+
20
+ const imageSourceIssue = (
21
+ src: string,
22
+ hosts: readonly string[] | undefined,
23
+ path: string
24
+ ): BlockError | undefined => {
25
+ if (src.startsWith("file:") && src.slice("file:".length).trim().length > 0) {
26
+ return undefined;
27
+ }
28
+ if (/^data:image\//i.test(src)) {
29
+ return undefined;
30
+ }
31
+ let hostname = "";
32
+ try {
33
+ const url = new URL(src);
34
+ if (url.protocol === "https:") {
35
+ hostname = url.hostname.toLowerCase();
36
+ }
37
+ } catch {
38
+ hostname = "";
39
+ }
40
+ const allowed = (hosts ?? []).some((host) => host.toLowerCase() === hostname);
41
+ if (hostname !== "" && allowed) {
42
+ return undefined;
43
+ }
44
+ return issue({
45
+ code: "IMAGE_HOST_NOT_ALLOWED",
46
+ fix: "Use a data:image URL, a file: ref, or an https URL whose host is in uiBlocks.imageHosts.",
47
+ message: BLOCK_ERROR_CODES.IMAGE_HOST_NOT_ALLOWED,
48
+ path: `${path}.src`,
49
+ });
50
+ };
51
+
52
+ const isInline = (dataset: Dataset): dataset is InlineDataset => dataset.source !== "ref";
53
+
54
+ const columnsFor = (
55
+ dataset: Dataset,
56
+ options: LintBlocksOptions | undefined
57
+ ): DatasetColumn[] | undefined => {
58
+ if (isInline(dataset)) {
59
+ return dataset.columns;
60
+ }
61
+ return options?.knownDatasets?.[dataset.id]?.columns;
62
+ };
63
+
64
+ const cellMatches = (type: DatasetColumn["type"], value: unknown): boolean => {
65
+ if (type === "number") {
66
+ return typeof value === "number" && Number.isFinite(value);
67
+ }
68
+ return typeof value === "string" && value.trim().length > 0;
69
+ };
70
+
71
+ const walk = (blocks: readonly Block[], prefix: string): {block: Block; path: string}[] => {
72
+ const found: {block: Block; path: string}[] = [];
73
+ blocks.forEach((block, index) => {
74
+ const path = `${prefix}[${index}]`;
75
+ found.push({block, path});
76
+ if (block.type === "columns" || block.type === "card") {
77
+ found.push(...walk(block.children, `${path}.children`));
78
+ }
79
+ });
80
+ return found;
81
+ };
82
+
83
+ const renderedCount = (
84
+ dataset: Dataset | undefined,
85
+ points: number | undefined
86
+ ): number | undefined => {
87
+ if (points !== undefined) {
88
+ return points;
89
+ }
90
+ if (dataset === undefined) {
91
+ return undefined;
92
+ }
93
+ if (isInline(dataset)) {
94
+ return dataset.rows.length;
95
+ }
96
+ return dataset.limit;
97
+ };
98
+
99
+ /**
100
+ * Semantic checks for datasets, charts, and tables.
101
+ * Structural schema errors are handled before this runs.
102
+ */
103
+ export const lintDocument = (
104
+ doc: BlocksDocument,
105
+ options?: LintBlocksOptions
106
+ ): {errors: BlockError[]; warnings: BlockError[]} => {
107
+ const errors: BlockError[] = [];
108
+ const warnings: BlockError[] = [];
109
+ const datasets = doc.datasets ?? {};
110
+ const names = Object.keys(datasets);
111
+ if (names.length > BLOCK_LIMITS.maxDatasets) {
112
+ errors.push(
113
+ issue({
114
+ code: "TOO_MANY",
115
+ fix: `Keep ${BLOCK_LIMITS.maxDatasets} datasets or fewer.`,
116
+ message: `The document has more than ${BLOCK_LIMITS.maxDatasets} datasets.`,
117
+ path: "datasets",
118
+ })
119
+ );
120
+ }
121
+
122
+ for (const name of names) {
123
+ const dataset = datasets[name];
124
+ if (dataset === undefined || !isInline(dataset)) {
125
+ if (
126
+ dataset?.source === "ref" &&
127
+ dataset.limit !== undefined &&
128
+ dataset.limit > BLOCK_LIMITS.refLimitMax
129
+ ) {
130
+ errors.push(
131
+ issue({
132
+ code: "TOO_MANY_POINTS",
133
+ fix: `Set grain to a coarser bucket, or set limit to ${BLOCK_LIMITS.refLimitMax} or less.`,
134
+ message: BLOCK_ERROR_CODES.TOO_MANY_POINTS,
135
+ path: `datasets.${name}.limit`,
136
+ })
137
+ );
138
+ }
139
+ continue;
140
+ }
141
+ if (dataset.columns.length > BLOCK_LIMITS.datasetColumnMax) {
142
+ errors.push(
143
+ issue({
144
+ code: "DATASET_TOO_LARGE",
145
+ fix: `Use ${BLOCK_LIMITS.datasetColumnMax} columns or fewer.`,
146
+ message: BLOCK_ERROR_CODES.DATASET_TOO_LARGE,
147
+ path: `datasets.${name}.columns`,
148
+ })
149
+ );
150
+ }
151
+ if (dataset.rows.length > BLOCK_LIMITS.datasetRowMax) {
152
+ errors.push(
153
+ issue({
154
+ code: "DATASET_TOO_LARGE",
155
+ fix: `Use ${BLOCK_LIMITS.datasetRowMax} rows or fewer, or return a ref dataset from a tool.`,
156
+ message: BLOCK_ERROR_CODES.DATASET_TOO_LARGE,
157
+ path: `datasets.${name}.rows`,
158
+ })
159
+ );
160
+ }
161
+ const seenColumns = new Set<string>();
162
+ dataset.columns.forEach((column, index) => {
163
+ if (seenColumns.has(column.name)) {
164
+ errors.push(
165
+ issue({
166
+ code: "DUPLICATE_ID",
167
+ fix: `Rename the duplicate column "${column.name}".`,
168
+ message: BLOCK_ERROR_CODES.DUPLICATE_ID,
169
+ path: `datasets.${name}.columns[${index}].name`,
170
+ })
171
+ );
172
+ }
173
+ seenColumns.add(column.name);
174
+ });
175
+ dataset.rows.forEach((row, rowIndex) => {
176
+ if (row.length !== dataset.columns.length) {
177
+ errors.push(
178
+ issue({
179
+ code: "ROW_ARITY_MISMATCH",
180
+ fix: `Give the row ${dataset.columns.length} values, one per column.`,
181
+ message: BLOCK_ERROR_CODES.ROW_ARITY_MISMATCH,
182
+ path: `datasets.${name}.rows[${rowIndex}]`,
183
+ })
184
+ );
185
+ return;
186
+ }
187
+ row.forEach((value, columnIndex) => {
188
+ const column = dataset.columns[columnIndex];
189
+ if (column === undefined || cellMatches(column.type, value)) {
190
+ return;
191
+ }
192
+ errors.push(
193
+ issue({
194
+ code: "COLUMN_TYPE_MISMATCH",
195
+ fix: `Make datasets.${name}.rows[${rowIndex}][${columnIndex}] a ${column.type}.`,
196
+ message: BLOCK_ERROR_CODES.COLUMN_TYPE_MISMATCH,
197
+ path: `datasets.${name}.rows[${rowIndex}][${columnIndex}]`,
198
+ })
199
+ );
200
+ });
201
+ });
202
+ }
203
+
204
+ const seenIds = new Set<string>();
205
+ const selectableIds = new Set<string>();
206
+ const walked = walk(doc.blocks, "blocks");
207
+ for (const {block} of walked) {
208
+ if ((block.type === "chart" || block.type === "table") && block.id !== undefined) {
209
+ selectableIds.add(block.id);
210
+ }
211
+ }
212
+ for (const {block, path} of walked) {
213
+ if (block.type === "html") {
214
+ if (options?.allowHtml !== true) {
215
+ errors.push(
216
+ issue({
217
+ code: "HTML_DISABLED",
218
+ fix: "Turn on uiBlocks.html before sending an html block, or use a text block.",
219
+ message: BLOCK_ERROR_CODES.HTML_DISABLED,
220
+ path,
221
+ })
222
+ );
223
+ }
224
+ const bytes = new TextEncoder().encode(block.html).length;
225
+ if (bytes > BLOCK_LIMITS.htmlMaxBytes) {
226
+ errors.push(
227
+ issue({
228
+ code: "HTML_TOO_LARGE",
229
+ fix: `Keep the html field at or under ${BLOCK_LIMITS.htmlMaxBytes} bytes.`,
230
+ message: BLOCK_ERROR_CODES.HTML_TOO_LARGE,
231
+ path: `${path}.html`,
232
+ })
233
+ );
234
+ }
235
+ }
236
+ if (block.type === "image") {
237
+ const imageIssue = imageSourceIssue(block.src, options?.imageHosts, path);
238
+ if (imageIssue) {
239
+ errors.push(imageIssue);
240
+ }
241
+ }
242
+ if (block.id !== undefined) {
243
+ if (seenIds.has(block.id)) {
244
+ errors.push(
245
+ issue({
246
+ code: "DUPLICATE_ID",
247
+ fix: `Give ${path} a unique id.`,
248
+ message: BLOCK_ERROR_CODES.DUPLICATE_ID,
249
+ path: `${path}.id`,
250
+ })
251
+ );
252
+ }
253
+ seenIds.add(block.id);
254
+ }
255
+ if (block.type === "chart") {
256
+ const hasPoints = block.points !== undefined;
257
+ const hasData = block.data !== undefined || block.x !== undefined || block.y !== undefined;
258
+ if (hasPoints && hasData) {
259
+ errors.push(
260
+ issue({
261
+ code: "INVALID_TYPE",
262
+ fix: "Use data, x, and y, or use points. Do not set both.",
263
+ message: "A chart binds to a dataset or lists points.",
264
+ path: `${path}.points`,
265
+ })
266
+ );
267
+ } else if (
268
+ !hasPoints &&
269
+ (block.data === undefined || block.x === undefined || block.y === undefined)
270
+ ) {
271
+ errors.push(
272
+ issue({
273
+ code: "MISSING_REQUIRED",
274
+ fix: "Set data, x, and y, or set points.",
275
+ message: BLOCK_ERROR_CODES.MISSING_REQUIRED,
276
+ path: `${path}.data`,
277
+ })
278
+ );
279
+ }
280
+ const dataset = block.data === undefined ? undefined : datasets[block.data];
281
+ if (block.data !== undefined && dataset === undefined) {
282
+ errors.push(
283
+ issue({
284
+ code: "DATASET_NOT_FOUND",
285
+ fix: `Define datasets.${block.data}, or use a dataset name that exists.`,
286
+ message: BLOCK_ERROR_CODES.DATASET_NOT_FOUND,
287
+ path: `${path}.data`,
288
+ })
289
+ );
290
+ }
291
+ const columns = dataset === undefined ? undefined : columnsFor(dataset, options);
292
+ if (
293
+ dataset !== undefined &&
294
+ columns !== undefined &&
295
+ block.x !== undefined &&
296
+ block.y !== undefined
297
+ ) {
298
+ const xColumn = columns.find((column) => column.name === block.x);
299
+ const yColumn = columns.find((column) => column.name === block.y);
300
+ if (xColumn === undefined) {
301
+ errors.push(
302
+ issue({
303
+ code: "COLUMN_NOT_FOUND",
304
+ fix: `Set x to a column of ${block.data}.`,
305
+ message: BLOCK_ERROR_CODES.COLUMN_NOT_FOUND,
306
+ path: `${path}.x`,
307
+ })
308
+ );
309
+ } else if (xColumn.type === "number") {
310
+ errors.push(
311
+ issue({
312
+ code: "COLUMN_TYPE_MISMATCH",
313
+ fix: "Use a string or date column for x.",
314
+ message: BLOCK_ERROR_CODES.COLUMN_TYPE_MISMATCH,
315
+ path: `${path}.x`,
316
+ })
317
+ );
318
+ }
319
+ if (yColumn === undefined) {
320
+ errors.push(
321
+ issue({
322
+ code: "COLUMN_NOT_FOUND",
323
+ fix: `Set y to a column of ${block.data}.`,
324
+ message: BLOCK_ERROR_CODES.COLUMN_NOT_FOUND,
325
+ path: `${path}.y`,
326
+ })
327
+ );
328
+ } else if (yColumn.type !== "number") {
329
+ errors.push(
330
+ issue({
331
+ code: "COLUMN_TYPE_MISMATCH",
332
+ fix: "Use a number column for y.",
333
+ message: BLOCK_ERROR_CODES.COLUMN_TYPE_MISMATCH,
334
+ path: `${path}.y`,
335
+ })
336
+ );
337
+ }
338
+ }
339
+ const count = renderedCount(dataset, block.points?.length);
340
+ if (block.kind === "bar" && count !== undefined && count > BLOCK_LIMITS.barCategoryWarning) {
341
+ warnings.push(
342
+ issue({
343
+ code: "BAR_TOO_MANY_CATEGORIES",
344
+ fix: "Group the categories, or use a line chart.",
345
+ message: BLOCK_WARNING_CODES.BAR_TOO_MANY_CATEGORIES,
346
+ path,
347
+ })
348
+ );
349
+ }
350
+ if (block.kind === "donut" && count !== undefined && count > BLOCK_LIMITS.donutSliceWarning) {
351
+ warnings.push(
352
+ issue({
353
+ code: "DONUT_TOO_MANY_SLICES",
354
+ fix: "Keep 8 slices or fewer.",
355
+ message: BLOCK_WARNING_CODES.DONUT_TOO_MANY_SLICES,
356
+ path,
357
+ })
358
+ );
359
+ }
360
+ if (block.kind === "line" && count === 1) {
361
+ warnings.push(
362
+ issue({
363
+ code: "LINE_SINGLE_POINT",
364
+ fix: "Add another point, or use a metric block.",
365
+ message: BLOCK_WARNING_CODES.LINE_SINGLE_POINT,
366
+ path,
367
+ })
368
+ );
369
+ }
370
+ }
371
+ if (block.type === "actions") {
372
+ block.elements.forEach((element, index) => {
373
+ const elementPath = `${path}.elements[${index}]`;
374
+ if (seenIds.has(element.id)) {
375
+ errors.push(
376
+ issue({
377
+ code: "DUPLICATE_ID",
378
+ fix: `Give ${elementPath} a unique id.`,
379
+ message: BLOCK_ERROR_CODES.DUPLICATE_ID,
380
+ path: `${elementPath}.id`,
381
+ })
382
+ );
383
+ }
384
+ seenIds.add(element.id);
385
+ if (element.type === "segmented" && !selectableIds.has(element.target)) {
386
+ errors.push(
387
+ issue({
388
+ code: "SELECT_TARGET_INVALID",
389
+ fix: "Set target to the id of a chart or table in this document.",
390
+ message: BLOCK_ERROR_CODES.SELECT_TARGET_INVALID,
391
+ path: `${elementPath}.target`,
392
+ })
393
+ );
394
+ }
395
+ if (
396
+ element.type === "button" &&
397
+ element.action.kind === "select" &&
398
+ !selectableIds.has(element.action.target)
399
+ ) {
400
+ errors.push(
401
+ issue({
402
+ code: "SELECT_TARGET_INVALID",
403
+ fix: "Set target to the id of a chart or table in this document.",
404
+ message: BLOCK_ERROR_CODES.SELECT_TARGET_INVALID,
405
+ path: `${elementPath}.action.target`,
406
+ })
407
+ );
408
+ }
409
+ if (
410
+ element.type === "button" &&
411
+ element.action.kind === "callback" &&
412
+ options?.hostActions !== undefined &&
413
+ !options.hostActions.includes(element.action.name)
414
+ ) {
415
+ errors.push(
416
+ issue({
417
+ code: "UNKNOWN_HOST_ACTION",
418
+ fix: `Use one of: ${options.hostActions.join(", ") || "(none registered)"}.`,
419
+ message: BLOCK_ERROR_CODES.UNKNOWN_HOST_ACTION,
420
+ path: `${elementPath}.action.name`,
421
+ })
422
+ );
423
+ }
424
+ if (element.type === "button" && element.action.kind === "open") {
425
+ const hasRoute = element.action.route !== undefined;
426
+ const hasUrl = element.action.url !== undefined;
427
+ if (hasRoute === hasUrl) {
428
+ errors.push(
429
+ issue({
430
+ code: "MISSING_REQUIRED",
431
+ fix: "Set either url or route on an open action.",
432
+ message: BLOCK_ERROR_CODES.MISSING_REQUIRED,
433
+ path: `${elementPath}.action`,
434
+ })
435
+ );
436
+ }
437
+ }
438
+ });
439
+ }
440
+ if (block.type === "table") {
441
+ const dataset = datasets[block.data];
442
+ if (dataset === undefined) {
443
+ errors.push(
444
+ issue({
445
+ code: "DATASET_NOT_FOUND",
446
+ fix: `Define datasets.${block.data}.`,
447
+ message: BLOCK_ERROR_CODES.DATASET_NOT_FOUND,
448
+ path: `${path}.data`,
449
+ })
450
+ );
451
+ continue;
452
+ }
453
+ const columns = columnsFor(dataset, options);
454
+ const listed = block.columns ?? columns?.map((column) => column.name);
455
+ if (listed !== undefined && listed.length > BLOCK_LIMITS.datasetColumnMax) {
456
+ errors.push(
457
+ issue({
458
+ code: "TABLE_TOO_WIDE",
459
+ fix: `List ${BLOCK_LIMITS.datasetColumnMax} columns or fewer.`,
460
+ message: BLOCK_ERROR_CODES.TABLE_TOO_WIDE,
461
+ path: `${path}.columns`,
462
+ })
463
+ );
464
+ }
465
+ if (columns === undefined || block.columns === undefined) {
466
+ continue;
467
+ }
468
+ for (const [index, columnName] of block.columns.entries()) {
469
+ if (!columns.some((column) => column.name === columnName)) {
470
+ errors.push(
471
+ issue({
472
+ code: "COLUMN_NOT_FOUND",
473
+ fix: `Remove "${columnName}" or add it to the dataset.`,
474
+ message: BLOCK_ERROR_CODES.COLUMN_NOT_FOUND,
475
+ path: `${path}.columns[${index}]`,
476
+ })
477
+ );
478
+ }
479
+ }
480
+ }
481
+ }
482
+
483
+ return {errors, warnings};
484
+ };