@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
@@ -0,0 +1,369 @@
1
+ import type {z} from "zod";
2
+
3
+ import {
4
+ BLOCK_ERROR_CODES,
5
+ type BlockError,
6
+ type BlockErrorCode,
7
+ formatBlockPath,
8
+ sortBlockErrors,
9
+ } from "./errors";
10
+ import {BLOCK_LIMITS} from "./limits";
11
+ import {type LintBlocksOptions, lintDocument} from "./lint";
12
+ import {type BlocksDocument, blocksSchema, LAYOUT_BLOCK_TYPES} from "./schema";
13
+
14
+ const TOP_LEVEL_ORDER = ["v", "datasets", "blocks"] as const;
15
+
16
+ const layoutTypes: ReadonlySet<string> = new Set(LAYOUT_BLOCK_TYPES);
17
+
18
+ export type ValidateBlocksResult =
19
+ | {doc: BlocksDocument; ok: true; warnings: BlockError[]}
20
+ | {errors: BlockError[]; ok: false; warnings: BlockError[]};
21
+
22
+ const error = ({
23
+ code,
24
+ fix,
25
+ message,
26
+ path,
27
+ }: {
28
+ code: BlockErrorCode;
29
+ fix: string;
30
+ message: string;
31
+ path: string;
32
+ }): BlockError => ({code, fix, message, path});
33
+
34
+ const isPlainMapping = (value: unknown): value is Record<string, unknown> => {
35
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
36
+ return false;
37
+ }
38
+ return Object.getPrototypeOf(value) === Object.prototype;
39
+ };
40
+
41
+ const keyOrderError = (value: Record<string, unknown>): BlockError | undefined => {
42
+ const present = Object.keys(value).filter((key) =>
43
+ TOP_LEVEL_ORDER.some((ordered) => ordered === key)
44
+ );
45
+ const expected = TOP_LEVEL_ORDER.filter((key) => present.includes(key));
46
+ for (let index = 0; index < present.length; index += 1) {
47
+ if (present[index] !== expected[index]) {
48
+ return error({
49
+ code: "KEY_ORDER",
50
+ fix: "Put the keys in this order: v, datasets, blocks.",
51
+ message: BLOCK_ERROR_CODES.KEY_ORDER,
52
+ path: present[index] ?? "",
53
+ });
54
+ }
55
+ }
56
+ return undefined;
57
+ };
58
+
59
+ const versionError = (value: Record<string, unknown>): BlockError | undefined => {
60
+ if (!Object.hasOwn(value, "v")) {
61
+ return undefined;
62
+ }
63
+ if (value.v === 1) {
64
+ return undefined;
65
+ }
66
+ return error({
67
+ code: "UNSUPPORTED_VERSION",
68
+ fix: "Set v to 1.",
69
+ message: BLOCK_ERROR_CODES.UNSUPPORTED_VERSION,
70
+ path: "v",
71
+ });
72
+ };
73
+
74
+ const countBlocks = (value: unknown): number => {
75
+ if (!isPlainMapping(value) || typeof value.type !== "string") {
76
+ return 0;
77
+ }
78
+ let count = 1;
79
+ if (Array.isArray(value.children)) {
80
+ for (const child of value.children) {
81
+ count += countBlocks(child);
82
+ }
83
+ }
84
+ return count;
85
+ };
86
+
87
+ const blockCountError = (value: Record<string, unknown>): BlockError | undefined => {
88
+ if (!Array.isArray(value.blocks)) {
89
+ return undefined;
90
+ }
91
+ let count = 0;
92
+ for (const block of value.blocks) {
93
+ count += countBlocks(block);
94
+ }
95
+ if (count <= BLOCK_LIMITS.maxBlocks) {
96
+ return undefined;
97
+ }
98
+ return error({
99
+ code: "TOO_MANY_BLOCKS",
100
+ fix: `Use ${BLOCK_LIMITS.maxBlocks} blocks or fewer.`,
101
+ message: BLOCK_ERROR_CODES.TOO_MANY_BLOCKS,
102
+ path: "blocks",
103
+ });
104
+ };
105
+
106
+ const depthPath = (value: unknown, depth: number, path: string): string | undefined => {
107
+ if (!isPlainMapping(value)) {
108
+ return undefined;
109
+ }
110
+ const type = value.type;
111
+ const isLayout = typeof type === "string" && layoutTypes.has(type);
112
+ if (isLayout && depth >= BLOCK_LIMITS.maxDepth) {
113
+ return path;
114
+ }
115
+ if (!Array.isArray(value.children)) {
116
+ return undefined;
117
+ }
118
+ const childDepth = isLayout ? depth + 1 : depth;
119
+ for (let index = 0; index < value.children.length; index += 1) {
120
+ const found = depthPath(value.children[index], childDepth, `${path}.children[${index}]`);
121
+ if (found !== undefined) {
122
+ return found;
123
+ }
124
+ }
125
+ return undefined;
126
+ };
127
+
128
+ const depthError = (value: Record<string, unknown>): BlockError | undefined => {
129
+ if (!Array.isArray(value.blocks)) {
130
+ return undefined;
131
+ }
132
+ for (let index = 0; index < value.blocks.length; index += 1) {
133
+ const path = depthPath(value.blocks[index], 1, `blocks[${index}]`);
134
+ if (path === undefined) {
135
+ continue;
136
+ }
137
+ return error({
138
+ code: "DEPTH_EXCEEDED",
139
+ fix: "Keep columns and card at the top level. Their children must be leaf blocks.",
140
+ message: BLOCK_ERROR_CODES.DEPTH_EXCEEDED,
141
+ path,
142
+ });
143
+ }
144
+ return undefined;
145
+ };
146
+
147
+ const lookup = (root: unknown, path: readonly PropertyKey[]): unknown => {
148
+ let current = root;
149
+ for (const segment of path) {
150
+ if (Array.isArray(current) && typeof segment === "number") {
151
+ current = current[segment];
152
+ continue;
153
+ }
154
+ if (isPlainMapping(current) && (typeof segment === "string" || typeof segment === "number")) {
155
+ current = current[String(segment)];
156
+ continue;
157
+ }
158
+ return undefined;
159
+ }
160
+ return current;
161
+ };
162
+
163
+ const subject = (path: string): string => (path === "" ? "The value" : path);
164
+
165
+ const mapIssue = (issue: z.core.$ZodIssue, root: unknown): BlockError[] => {
166
+ const path = formatBlockPath(issue.path);
167
+ if (issue.code === "unrecognized_keys") {
168
+ return issue.keys.map((key) =>
169
+ error({
170
+ code: "UNKNOWN_KEY",
171
+ fix: `Remove "${key}".`,
172
+ message: `"${key}" is not a field of ${path === "" ? "this object" : path}.`,
173
+ path: path === "" ? key : `${path}.${key}`,
174
+ })
175
+ );
176
+ }
177
+ if (issue.code === "invalid_value") {
178
+ const received = lookup(root, issue.path);
179
+ return [
180
+ error({
181
+ code: "INVALID_ENUM",
182
+ fix: `Use one of: ${issue.values.map((value) => JSON.stringify(value)).join(", ")}.`,
183
+ message: `${subject(path)} must be one of the allowed values, not ${JSON.stringify(received)}.`,
184
+ path,
185
+ }),
186
+ ];
187
+ }
188
+ if (issue.code === "invalid_type") {
189
+ const received = lookup(root, issue.path);
190
+ if (received === undefined) {
191
+ return [
192
+ error({
193
+ code: "MISSING_REQUIRED",
194
+ fix: `Add ${path === "" ? "the missing field" : path}.`,
195
+ message: BLOCK_ERROR_CODES.MISSING_REQUIRED,
196
+ path,
197
+ }),
198
+ ];
199
+ }
200
+ return [
201
+ error({
202
+ code: "INVALID_TYPE",
203
+ fix: `Make ${subject(path)} a ${issue.expected}.`,
204
+ message: `${subject(path)} has the wrong type.`,
205
+ path,
206
+ }),
207
+ ];
208
+ }
209
+ if (issue.code === "too_big") {
210
+ const maximum = String(issue.maximum);
211
+ if (issue.origin === "array" && path === "blocks") {
212
+ return [
213
+ error({
214
+ code: "TOO_MANY_BLOCKS",
215
+ fix: `Use ${maximum} blocks or fewer.`,
216
+ message: BLOCK_ERROR_CODES.TOO_MANY_BLOCKS,
217
+ path,
218
+ }),
219
+ ];
220
+ }
221
+ if (issue.origin === "array") {
222
+ return [
223
+ error({
224
+ code: "TOO_MANY",
225
+ fix: `Remove items from ${subject(path)} until it has ${maximum} or fewer.`,
226
+ message: `${subject(path)} has more than ${maximum} items.`,
227
+ path,
228
+ }),
229
+ ];
230
+ }
231
+ return [
232
+ error({
233
+ code: "TOO_LONG",
234
+ fix: `Shorten ${subject(path)} to ${maximum} characters or fewer.`,
235
+ message: `${subject(path)} is longer than ${maximum} characters.`,
236
+ path,
237
+ }),
238
+ ];
239
+ }
240
+ if (issue.code === "too_small") {
241
+ if (issue.origin === "number" || issue.origin === "int") {
242
+ return [
243
+ error({
244
+ code: "INVALID_TYPE",
245
+ fix: `Set ${subject(path)} to an integer of at least ${String(issue.minimum)}.`,
246
+ message: `${subject(path)} must be an integer of at least ${String(issue.minimum)}.`,
247
+ path,
248
+ }),
249
+ ];
250
+ }
251
+ if (issue.origin === "array") {
252
+ return [
253
+ error({
254
+ code: "TOO_FEW",
255
+ fix: `Add items to ${subject(path)} until it has at least ${String(issue.minimum)}.`,
256
+ message: `${subject(path)} has fewer items than allowed.`,
257
+ path,
258
+ }),
259
+ ];
260
+ }
261
+ return [
262
+ error({
263
+ code: "TOO_SHORT",
264
+ fix: `Write visible text for ${subject(path)}.`,
265
+ message: BLOCK_ERROR_CODES.TOO_SHORT,
266
+ path,
267
+ }),
268
+ ];
269
+ }
270
+ if (issue.code === "invalid_format") {
271
+ const isRefId = /^datasets\.[^.]+\.id$/.test(path);
272
+ return [
273
+ error({
274
+ code: "INVALID_FORMAT",
275
+ fix: isRefId
276
+ ? "Use 1 to 80 letters, digits, underscores, or hyphens."
277
+ : "Use a lowercase id starting with a letter, then letters, digits, or underscores.",
278
+ message: `${subject(path)} does not match the required format.`,
279
+ path,
280
+ }),
281
+ ];
282
+ }
283
+ if (issue.code === "custom" && issue.params?.blockCode === "TOO_SHORT") {
284
+ return [
285
+ error({
286
+ code: "TOO_SHORT",
287
+ fix: `Write visible text for ${subject(path)}.`,
288
+ message: BLOCK_ERROR_CODES.TOO_SHORT,
289
+ path,
290
+ }),
291
+ ];
292
+ }
293
+ if (issue.code === "invalid_union") {
294
+ const nested = "errors" in issue && Array.isArray(issue.errors) ? issue.errors.flat() : [];
295
+ if (nested.length > 0) {
296
+ return nested.flatMap((nestedIssue) => mapIssue(nestedIssue, root));
297
+ }
298
+ return [
299
+ error({
300
+ code: "INVALID_ENUM",
301
+ fix: "Set type to heading, text, metric, badge, divider, context, chart, table, actions, columns, card, callout, image, details, or html.",
302
+ message: `${subject(path)} is not a supported block.`,
303
+ path: path === "" ? "type" : `${path}.type`,
304
+ }),
305
+ ];
306
+ }
307
+ return [
308
+ error({
309
+ code: "INVALID_TYPE",
310
+ fix: `Check ${subject(path)}.`,
311
+ message: issue.message,
312
+ path,
313
+ }),
314
+ ];
315
+ };
316
+
317
+ const dropVersionIssues = (issues: readonly z.core.$ZodIssue[]): z.core.$ZodIssue[] =>
318
+ issues.filter((issue) => !(issue.path.length === 1 && issue.path[0] === "v"));
319
+
320
+ /**
321
+ * Checks structure, then dataset, chart, and table lint.
322
+ * Returns every error at once. Warnings do not block a valid document.
323
+ */
324
+ export const validateBlocks = (doc: unknown, options?: LintBlocksOptions): ValidateBlocksResult => {
325
+ const structural: BlockError[] = [];
326
+ if (isPlainMapping(doc)) {
327
+ const order = keyOrderError(doc);
328
+ if (order) {
329
+ structural.push(order);
330
+ }
331
+ const version = versionError(doc);
332
+ if (version) {
333
+ structural.push(version);
334
+ }
335
+ const depth = depthError(doc);
336
+ if (depth) {
337
+ structural.push(depth);
338
+ }
339
+ const count = blockCountError(doc);
340
+ if (count) {
341
+ structural.push(count);
342
+ }
343
+ }
344
+ const parsed = blocksSchema.safeParse(doc);
345
+ if (!parsed.success) {
346
+ const issues = structural.some((item) => item.code === "UNSUPPORTED_VERSION")
347
+ ? dropVersionIssues(parsed.error.issues)
348
+ : parsed.error.issues;
349
+ const skipBlockCount = structural.some((item) => item.code === "TOO_MANY_BLOCKS");
350
+ for (const issue of issues) {
351
+ if (skipBlockCount && issue.code === "too_big" && formatBlockPath(issue.path) === "blocks") {
352
+ continue;
353
+ }
354
+ structural.push(...mapIssue(issue, doc));
355
+ }
356
+ }
357
+ if (structural.length > 0 || !parsed.success) {
358
+ return {errors: sortBlockErrors(structural), ok: false, warnings: []};
359
+ }
360
+ const linted = lintDocument(parsed.data, options);
361
+ if (linted.errors.length > 0) {
362
+ return {
363
+ errors: sortBlockErrors(linted.errors),
364
+ ok: false,
365
+ warnings: sortBlockErrors(linted.warnings),
366
+ };
367
+ }
368
+ return {doc: parsed.data, ok: true, warnings: sortBlockErrors(linted.warnings)};
369
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "isolatedModules": true,
5
+ "lib": ["es2023"],
6
+ "module": "nodenext",
7
+ "noImplicitAny": false,
8
+ "noImplicitReturns": true,
9
+ "outDir": "./dist",
10
+ "rootDir": "./src",
11
+ "skipLibCheck": true,
12
+ "strict": true,
13
+ "target": "es2023",
14
+ "types": ["node"]
15
+ },
16
+ "exclude": ["node_modules", "src/**/*.test.ts", "src/tests/**"],
17
+ "include": ["src"]
18
+ }