@azx-pbc/axiom-builder 0.0.0-stage → 0.0.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 (231) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +648 -2
  3. package/dist/cards/selectors.d.ts +70 -0
  4. package/dist/cards/selectors.d.ts.map +1 -0
  5. package/dist/cards/selectors.js +194 -0
  6. package/dist/cards/selectors.js.map +1 -0
  7. package/dist/definition.d.ts +36 -0
  8. package/dist/definition.d.ts.map +1 -0
  9. package/dist/definition.js +48 -0
  10. package/dist/definition.js.map +1 -0
  11. package/dist/evidence/cases.d.ts +44 -0
  12. package/dist/evidence/cases.d.ts.map +1 -0
  13. package/dist/evidence/cases.js +578 -0
  14. package/dist/evidence/cases.js.map +1 -0
  15. package/dist/evidence/repairs.d.ts +99 -0
  16. package/dist/evidence/repairs.d.ts.map +1 -0
  17. package/dist/evidence/repairs.js +181 -0
  18. package/dist/evidence/repairs.js.map +1 -0
  19. package/dist/evidence/summary.d.ts +35 -0
  20. package/dist/evidence/summary.d.ts.map +1 -0
  21. package/dist/evidence/summary.js +38 -0
  22. package/dist/evidence/summary.js.map +1 -0
  23. package/dist/i18n/gaps.d.ts +3 -0
  24. package/dist/i18n/gaps.d.ts.map +1 -0
  25. package/dist/i18n/gaps.js +2 -0
  26. package/dist/i18n/gaps.js.map +1 -0
  27. package/dist/i18n/message-slots.d.ts +19 -0
  28. package/dist/i18n/message-slots.d.ts.map +1 -0
  29. package/dist/i18n/message-slots.js +101 -0
  30. package/dist/i18n/message-slots.js.map +1 -0
  31. package/dist/i18n/text-locale.d.ts +13 -0
  32. package/dist/i18n/text-locale.d.ts.map +1 -0
  33. package/dist/i18n/text-locale.js +14 -0
  34. package/dist/i18n/text-locale.js.map +1 -0
  35. package/dist/i18n/translations.d.ts +56 -0
  36. package/dist/i18n/translations.d.ts.map +1 -0
  37. package/dist/i18n/translations.js +103 -0
  38. package/dist/i18n/translations.js.map +1 -0
  39. package/dist/impact/references.d.ts +143 -0
  40. package/dist/impact/references.d.ts.map +1 -0
  41. package/dist/impact/references.js +429 -0
  42. package/dist/impact/references.js.map +1 -0
  43. package/dist/index.d.ts +86 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +58 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/preview/compile-preview.d.ts +67 -0
  48. package/dist/preview/compile-preview.d.ts.map +1 -0
  49. package/dist/preview/compile-preview.js +286 -0
  50. package/dist/preview/compile-preview.js.map +1 -0
  51. package/dist/preview/handle.d.ts +119 -0
  52. package/dist/preview/handle.d.ts.map +1 -0
  53. package/dist/preview/handle.js +2 -0
  54. package/dist/preview/handle.js.map +1 -0
  55. package/dist/preview/held.d.ts +39 -0
  56. package/dist/preview/held.d.ts.map +1 -0
  57. package/dist/preview/held.js +15 -0
  58. package/dist/preview/held.js.map +1 -0
  59. package/dist/preview/measurement.d.ts +5 -0
  60. package/dist/preview/measurement.d.ts.map +1 -0
  61. package/dist/preview/measurement.js +18 -0
  62. package/dist/preview/measurement.js.map +1 -0
  63. package/dist/publish/build.d.ts +33 -0
  64. package/dist/publish/build.d.ts.map +1 -0
  65. package/dist/publish/build.js +15 -0
  66. package/dist/publish/build.js.map +1 -0
  67. package/dist/publish/diff.d.ts +208 -0
  68. package/dist/publish/diff.d.ts.map +1 -0
  69. package/dist/publish/diff.js +227 -0
  70. package/dist/publish/diff.js.map +1 -0
  71. package/dist/publish/review.d.ts +41 -0
  72. package/dist/publish/review.d.ts.map +1 -0
  73. package/dist/publish/review.js +22 -0
  74. package/dist/publish/review.js.map +1 -0
  75. package/dist/schema/dataset-input.d.ts +30 -0
  76. package/dist/schema/dataset-input.d.ts.map +1 -0
  77. package/dist/schema/dataset-input.js +58 -0
  78. package/dist/schema/dataset-input.js.map +1 -0
  79. package/dist/schema/dataset.d.ts +166 -0
  80. package/dist/schema/dataset.d.ts.map +1 -0
  81. package/dist/schema/dataset.js +281 -0
  82. package/dist/schema/dataset.js.map +1 -0
  83. package/dist/schema/declarations.d.ts +15 -0
  84. package/dist/schema/declarations.d.ts.map +1 -0
  85. package/dist/schema/declarations.js +47 -0
  86. package/dist/schema/declarations.js.map +1 -0
  87. package/dist/schema/document.d.ts +504 -0
  88. package/dist/schema/document.d.ts.map +1 -0
  89. package/dist/schema/document.js +337 -0
  90. package/dist/schema/document.js.map +1 -0
  91. package/dist/schema/error.d.ts +15 -0
  92. package/dist/schema/error.d.ts.map +1 -0
  93. package/dist/schema/error.js +18 -0
  94. package/dist/schema/error.js.map +1 -0
  95. package/dist/schema/field-id.d.ts +8 -0
  96. package/dist/schema/field-id.d.ts.map +1 -0
  97. package/dist/schema/field-id.js +47 -0
  98. package/dist/schema/field-id.js.map +1 -0
  99. package/dist/schema/layout.d.ts +118 -0
  100. package/dist/schema/layout.d.ts.map +1 -0
  101. package/dist/schema/layout.js +101 -0
  102. package/dist/schema/layout.js.map +1 -0
  103. package/dist/schema/palette.d.ts +103 -0
  104. package/dist/schema/palette.d.ts.map +1 -0
  105. package/dist/schema/palette.js +140 -0
  106. package/dist/schema/palette.js.map +1 -0
  107. package/dist/schema/read.d.ts +49 -0
  108. package/dist/schema/read.d.ts.map +1 -0
  109. package/dist/schema/read.js +942 -0
  110. package/dist/schema/read.js.map +1 -0
  111. package/dist/store/apply-dataset.d.ts +14 -0
  112. package/dist/store/apply-dataset.d.ts.map +1 -0
  113. package/dist/store/apply-dataset.js +513 -0
  114. package/dist/store/apply-dataset.js.map +1 -0
  115. package/dist/store/apply.d.ts +37 -0
  116. package/dist/store/apply.d.ts.map +1 -0
  117. package/dist/store/apply.js +2269 -0
  118. package/dist/store/apply.js.map +1 -0
  119. package/dist/store/candidate.d.ts +37 -0
  120. package/dist/store/candidate.d.ts.map +1 -0
  121. package/dist/store/candidate.js +178 -0
  122. package/dist/store/candidate.js.map +1 -0
  123. package/dist/store/commands/coverage.d.ts +58 -0
  124. package/dist/store/commands/coverage.d.ts.map +1 -0
  125. package/dist/store/commands/coverage.js +354 -0
  126. package/dist/store/commands/coverage.js.map +1 -0
  127. package/dist/store/commands/dataset.d.ts +26 -0
  128. package/dist/store/commands/dataset.d.ts.map +1 -0
  129. package/dist/store/commands/dataset.js +368 -0
  130. package/dist/store/commands/dataset.js.map +1 -0
  131. package/dist/store/commands/document.d.ts +21 -0
  132. package/dist/store/commands/document.d.ts.map +1 -0
  133. package/dist/store/commands/document.js +160 -0
  134. package/dist/store/commands/document.js.map +1 -0
  135. package/dist/store/commands/field.d.ts +32 -0
  136. package/dist/store/commands/field.d.ts.map +1 -0
  137. package/dist/store/commands/field.js +572 -0
  138. package/dist/store/commands/field.js.map +1 -0
  139. package/dist/store/commands/layout.d.ts +16 -0
  140. package/dist/store/commands/layout.d.ts.map +1 -0
  141. package/dist/store/commands/layout.js +70 -0
  142. package/dist/store/commands/layout.js.map +1 -0
  143. package/dist/store/commands/parse.d.ts +19 -0
  144. package/dist/store/commands/parse.d.ts.map +1 -0
  145. package/dist/store/commands/parse.js +328 -0
  146. package/dist/store/commands/parse.js.map +1 -0
  147. package/dist/store/commands/readers.d.ts +198 -0
  148. package/dist/store/commands/readers.d.ts.map +1 -0
  149. package/dist/store/commands/readers.js +711 -0
  150. package/dist/store/commands/readers.js.map +1 -0
  151. package/dist/store/commands/rule.d.ts +24 -0
  152. package/dist/store/commands/rule.d.ts.map +1 -0
  153. package/dist/store/commands/rule.js +379 -0
  154. package/dist/store/commands/rule.js.map +1 -0
  155. package/dist/store/commands/shapes.d.ts +900 -0
  156. package/dist/store/commands/shapes.d.ts.map +1 -0
  157. package/dist/store/commands/shapes.js +166 -0
  158. package/dist/store/commands/shapes.js.map +1 -0
  159. package/dist/store/commands/suggestions.d.ts +37 -0
  160. package/dist/store/commands/suggestions.d.ts.map +1 -0
  161. package/dist/store/commands/suggestions.js +177 -0
  162. package/dist/store/commands/suggestions.js.map +1 -0
  163. package/dist/store/commands.d.ts +16 -0
  164. package/dist/store/commands.d.ts.map +1 -0
  165. package/dist/store/commands.js +13 -0
  166. package/dist/store/commands.js.map +1 -0
  167. package/dist/store/dataset-candidate.d.ts +33 -0
  168. package/dist/store/dataset-candidate.d.ts.map +1 -0
  169. package/dist/store/dataset-candidate.js +31 -0
  170. package/dist/store/dataset-candidate.js.map +1 -0
  171. package/dist/store/dataset-pin.d.ts +93 -0
  172. package/dist/store/dataset-pin.d.ts.map +1 -0
  173. package/dist/store/dataset-pin.js +86 -0
  174. package/dist/store/dataset-pin.js.map +1 -0
  175. package/dist/store/dataset-store.d.ts +96 -0
  176. package/dist/store/dataset-store.d.ts.map +1 -0
  177. package/dist/store/dataset-store.js +112 -0
  178. package/dist/store/dataset-store.js.map +1 -0
  179. package/dist/store/history.d.ts +149 -0
  180. package/dist/store/history.d.ts.map +1 -0
  181. package/dist/store/history.js +173 -0
  182. package/dist/store/history.js.map +1 -0
  183. package/dist/store/measurement.d.ts +5 -0
  184. package/dist/store/measurement.d.ts.map +1 -0
  185. package/dist/store/measurement.js +20 -0
  186. package/dist/store/measurement.js.map +1 -0
  187. package/dist/store/notify.d.ts +11 -0
  188. package/dist/store/notify.d.ts.map +1 -0
  189. package/dist/store/notify.js +26 -0
  190. package/dist/store/notify.js.map +1 -0
  191. package/dist/store/published.d.ts +50 -0
  192. package/dist/store/published.d.ts.map +1 -0
  193. package/dist/store/published.js +27 -0
  194. package/dist/store/published.js.map +1 -0
  195. package/dist/store/refusals.d.ts +175 -0
  196. package/dist/store/refusals.d.ts.map +1 -0
  197. package/dist/store/refusals.js +160 -0
  198. package/dist/store/refusals.js.map +1 -0
  199. package/dist/store/steps.d.ts +14 -0
  200. package/dist/store/steps.d.ts.map +1 -0
  201. package/dist/store/steps.js +124 -0
  202. package/dist/store/steps.js.map +1 -0
  203. package/dist/store/store.d.ts +142 -0
  204. package/dist/store/store.d.ts.map +1 -0
  205. package/dist/store/store.js +136 -0
  206. package/dist/store/store.js.map +1 -0
  207. package/dist/store/unchanged.d.ts +12 -0
  208. package/dist/store/unchanged.d.ts.map +1 -0
  209. package/dist/store/unchanged.js +35 -0
  210. package/dist/store/unchanged.js.map +1 -0
  211. package/dist/visual/emit.d.ts +21 -0
  212. package/dist/visual/emit.d.ts.map +1 -0
  213. package/dist/visual/emit.js +133 -0
  214. package/dist/visual/emit.js.map +1 -0
  215. package/dist/visual/parse.d.ts +32 -0
  216. package/dist/visual/parse.d.ts.map +1 -0
  217. package/dist/visual/parse.js +330 -0
  218. package/dist/visual/parse.js.map +1 -0
  219. package/dist/visual/project.d.ts +40 -0
  220. package/dist/visual/project.d.ts.map +1 -0
  221. package/dist/visual/project.js +349 -0
  222. package/dist/visual/project.js.map +1 -0
  223. package/dist/visual/represent.d.ts +50 -0
  224. package/dist/visual/represent.d.ts.map +1 -0
  225. package/dist/visual/represent.js +66 -0
  226. package/dist/visual/represent.js.map +1 -0
  227. package/dist/visual/vocabulary.d.ts +121 -0
  228. package/dist/visual/vocabulary.d.ts.map +1 -0
  229. package/dist/visual/vocabulary.js +32 -0
  230. package/dist/visual/vocabulary.js.map +1 -0
  231. package/package.json +52 -3
@@ -0,0 +1,942 @@
1
+ import { CONTROLS_BY_PRIMITIVE, closedShapeErrors } from '@azx-pbc/axiom-compiler';
2
+ import { AxiomError, CONTROLS, copyRestricted, isItemPrimitive, isRecord, isWireValue, toAccessOverlay, } from '@azx-pbc/axiom-runtime';
3
+ import { localePolicyDefect } from './document.js';
4
+ import { authoringRefusal, closedShapeRefusal } from '../store/refusals.js';
5
+ /**
6
+ * A member the closed metaschema admitted and this walk cannot read.
7
+ *
8
+ * Not an authoring mistake — the metaschema is the gate — but the contract and the types derived
9
+ * from it disagreeing, which no refusal an author reads could repair.
10
+ */
11
+ function impossible(detail) {
12
+ throw new Error(`a document the closed metaschema admits carries ${detail}`);
13
+ }
14
+ /** A validated member as an object. */
15
+ function record(value, detail) {
16
+ if (!isRecord(value))
17
+ return impossible(detail);
18
+ return value;
19
+ }
20
+ /** A required text member. */
21
+ function text(source, key) {
22
+ const value = source[key];
23
+ if (typeof value !== 'string')
24
+ return impossible(`a \`${key}\` that is not text`);
25
+ return value;
26
+ }
27
+ /** An absent-or-text member. */
28
+ function optionalText(source, key) {
29
+ const value = source[key];
30
+ if (value === undefined)
31
+ return undefined;
32
+ if (typeof value !== 'string')
33
+ return impossible(`a \`${key}\` that is not text`);
34
+ return value;
35
+ }
36
+ /** The one duration unit a declaration may state, narrowed from the metaschema's enum. */
37
+ function optionalDurationUnit(source, key) {
38
+ const value = optionalText(source, key);
39
+ if (value === undefined)
40
+ return undefined;
41
+ if (value === 'minutes' || value === 'hours' || value === 'seconds')
42
+ return value;
43
+ return impossible(`a \`${key}\` that is not a duration unit`);
44
+ }
45
+ /** A required whole-number member. */
46
+ function wholeNumber(source, key) {
47
+ const value = source[key];
48
+ if (typeof value !== 'number' || !Number.isSafeInteger(value)) {
49
+ return impossible(`a \`${key}\` that is not a safe integer`);
50
+ }
51
+ return value;
52
+ }
53
+ /** A required boolean member. */
54
+ function booleanMember(source, key) {
55
+ const value = source[key];
56
+ if (typeof value !== 'boolean')
57
+ return impossible(`a \`${key}\` that is not a boolean`);
58
+ return value;
59
+ }
60
+ /** An absent-or-whole-number member. */
61
+ function optionalWholeNumber(source, key) {
62
+ const value = source[key];
63
+ if (value === undefined)
64
+ return undefined;
65
+ if (typeof value !== 'number' || !Number.isSafeInteger(value)) {
66
+ return impossible(`a \`${key}\` that is not a safe integer`);
67
+ }
68
+ return value;
69
+ }
70
+ /** An absent-or-boolean member. */
71
+ function optionalBoolean(source, key) {
72
+ const value = source[key];
73
+ if (value === undefined)
74
+ return undefined;
75
+ return booleanMember(source, key);
76
+ }
77
+ /**
78
+ * An absent value bound: a canonical JSON number, or the text a `date`/`instant` bound states.
79
+ *
80
+ * The metaschema's `oneOf` is the gate; the walk only has to keep the two shapes apart so a
81
+ * `number` never becomes a string on its way through the store.
82
+ */
83
+ function optionalBound(source, key) {
84
+ const value = source[key];
85
+ if (value === undefined)
86
+ return undefined;
87
+ if (typeof value === 'number' && Number.isSafeInteger(value))
88
+ return value;
89
+ if (typeof value === 'string')
90
+ return value;
91
+ return impossible(`a \`${key}\` that is neither a safe integer nor text`);
92
+ }
93
+ /** A validated object member, or `undefined` when the document states none. */
94
+ function objectMember(source, key) {
95
+ const value = source[key];
96
+ return value === undefined ? undefined : record(value, `a \`${key}\` that is not an object`);
97
+ }
98
+ /** The elements of a validated array member, or an empty list when it states none. */
99
+ function members(source, key) {
100
+ const value = source[key];
101
+ if (value === undefined)
102
+ return [];
103
+ if (!Array.isArray(value))
104
+ return impossible(`a \`${key}\` that is not an array`);
105
+ const entries = value;
106
+ return entries;
107
+ }
108
+ /** The text an array member states, element by element. */
109
+ function textMembers(source, key) {
110
+ return members(source, key).map((entry) => typeof entry === 'string' ? entry : impossible(`a \`${key}\` entry that is not text`));
111
+ }
112
+ /** One of a closed set of words, narrowed after the metaschema proved it is one of them. */
113
+ function oneOf(value, vocabulary, detail) {
114
+ if (typeof value !== 'string' || !Object.hasOwn(vocabulary, value))
115
+ return impossible(detail);
116
+ return value;
117
+ }
118
+ /** An item primitive, narrowed by the runtime registry rather than a list restated here. */
119
+ function itemPrimitive(value) {
120
+ return isItemPrimitive(value) ? value : impossible('an item primitive');
121
+ }
122
+ /** The eight rule kinds, as the set the metaschema enumerates. */
123
+ const RULE_KINDS = {
124
+ validate: true,
125
+ visible: true,
126
+ required: true,
127
+ enabled: true,
128
+ compute: true,
129
+ defaultCompute: true,
130
+ gate: true,
131
+ screen: true,
132
+ };
133
+ const RULE_SCOPES = {
134
+ field: true,
135
+ record: true,
136
+ array: true,
137
+ item: true,
138
+ };
139
+ const SEVERITIES = { block: true, warn: true, info: true };
140
+ const TIERS = { client: true, server: true };
141
+ const ON_HIDE = { preserve: true, exclude: true };
142
+ const GATE_POLICIES = { defer: true, warn: true, block: true };
143
+ /**
144
+ * The primitive a declaration states, read from the compiler's control matrix.
145
+ *
146
+ * The matrix is `Record<Primitive, …>`, so its own keys are the primitive vocabulary: one
147
+ * authority states it, and a primitive added to the runtime union cannot be missing from the
148
+ * matrix without failing the matrix's typecheck.
149
+ */
150
+ function isPrimitive(value) {
151
+ return Object.hasOwn(CONTROLS_BY_PRIMITIVE, value);
152
+ }
153
+ function requiredPrimitive(source) {
154
+ const primitive = text(source, 'primitive');
155
+ return isPrimitive(primitive) ? primitive : impossible(`a \`${primitive}\` primitive`);
156
+ }
157
+ function isControl(value) {
158
+ return typeof value === 'string' && CONTROLS.some((control) => control === value);
159
+ }
160
+ function isWireRecord(value) {
161
+ if (!isRecord(value))
162
+ return false;
163
+ return Object.values(value).every(isWireValue);
164
+ }
165
+ /**
166
+ * A value the metaschema checked as a canonical wire value, as the type the runtime states.
167
+ *
168
+ * The metaschema's `wireValue` and the runtime's guard describe one canonical JSON domain, so a
169
+ * value the metaschema admitted and this refuses is the two representations disagreeing.
170
+ */
171
+ function wireValue(value) {
172
+ if (!isWireValue(value))
173
+ return impossible('a `wireValue` the runtime does not read as one');
174
+ return value;
175
+ }
176
+ function wireRecord(value, detail) {
177
+ if (!isWireRecord(value))
178
+ return impossible(detail);
179
+ return value;
180
+ }
181
+ /** One field reference, `schema.v1.json` `fieldRef`. */
182
+ function fieldRef(value, path) {
183
+ const source = record(value, `a \`${path}\` that is not an object`);
184
+ const rowKey = source['rowKey'];
185
+ if (rowKey !== undefined && typeof rowKey !== 'string' && typeof rowKey !== 'number') {
186
+ return impossible(`a \`${path}.rowKey\` that is neither text nor a whole number`);
187
+ }
188
+ return {
189
+ fieldId: text(source, 'fieldId'),
190
+ ...(rowKey === undefined ? {} : { rowKey }),
191
+ };
192
+ }
193
+ /** The item declaration of a scalar or attachment array. */
194
+ function fieldItem(value) {
195
+ const source = record(value, 'an `items` that is not an object');
196
+ const currency = optionalText(source, 'currency');
197
+ const scale = source['scale'];
198
+ const units = textMembers(source, 'units');
199
+ const unit = optionalDurationUnit(source, 'unit');
200
+ return {
201
+ primitive: itemPrimitive(source['primitive']),
202
+ ...(currency === undefined ? {} : { currency }),
203
+ ...(scale === undefined ? {} : { scale: wholeNumber(source, 'scale') }),
204
+ ...(units.length === 0 ? {} : { units }),
205
+ ...(unit === undefined ? {} : { unit }),
206
+ };
207
+ }
208
+ function fieldOption(value, path) {
209
+ const source = record(value, 'an `options` entry that is not an object');
210
+ const code = source['code'];
211
+ if (typeof code !== 'string' && (typeof code !== 'number' || !Number.isSafeInteger(code))) {
212
+ return impossible('an option `code` that is neither text nor a whole number');
213
+ }
214
+ const availableWhenValue = source['availableWhen'];
215
+ const availableWhen = availableWhenValue === undefined
216
+ ? undefined
217
+ : expressionSource(availableWhenValue, `${path}.availableWhen`);
218
+ if (availableWhen !== undefined && !availableWhen.ok)
219
+ return availableWhen;
220
+ const unavailableParamsValue = source['unavailableParams'];
221
+ const unavailableParams = unavailableParamsValue === undefined
222
+ ? undefined
223
+ : expressionParams(unavailableParamsValue, `${path}.unavailableParams`);
224
+ if (unavailableParams !== undefined && !unavailableParams.ok)
225
+ return unavailableParams;
226
+ const suggests = [];
227
+ for (const [index, entry] of members(source, 'suggests').entries()) {
228
+ const read = suggestion(entry, `${path}.suggests[${String(index)}]`);
229
+ if (!read.ok)
230
+ return read;
231
+ suggests.push(read.value);
232
+ }
233
+ const deprecated = source['deprecated'];
234
+ const unavailableMessageKey = optionalText(source, 'unavailableMessageKey');
235
+ return {
236
+ ok: true,
237
+ value: {
238
+ code,
239
+ labelKey: text(source, 'labelKey'),
240
+ ...(deprecated === undefined ? {} : { deprecated: booleanMember(source, 'deprecated') }),
241
+ ...(availableWhen === undefined ? {} : { availableWhen: availableWhen.value }),
242
+ ...(unavailableMessageKey === undefined ? {} : { unavailableMessageKey }),
243
+ ...(unavailableParams === undefined ? {} : { unavailableParams: unavailableParams.value }),
244
+ ...(suggests.length === 0 ? {} : { suggests }),
245
+ },
246
+ };
247
+ }
248
+ /** One alternative, with every expression kept in the authored source form. */
249
+ function suggestion(value, path) {
250
+ const source = record(value, `a \`${path}\` that is not an object`);
251
+ const operations = [];
252
+ for (const [index, entry] of members(source, 'operations').entries()) {
253
+ const operation = suggestionOperation(entry, `${path}.operations[${String(index)}]`);
254
+ if (!operation.ok)
255
+ return operation;
256
+ operations.push(operation.value);
257
+ }
258
+ return { ok: true, value: { labelKey: text(source, 'labelKey'), operations } };
259
+ }
260
+ /** One suggestion operation, narrowed by its kind exactly as the metaschema's union states it. */
261
+ function suggestionOperation(value, path) {
262
+ const source = record(value, `a \`${path}\` that is not an object`);
263
+ const kind = source['kind'];
264
+ switch (kind) {
265
+ case 'value': {
266
+ const expression = expressionSource(source['value'], `${path}.value`);
267
+ if (!expression.ok)
268
+ return expression;
269
+ return {
270
+ ok: true,
271
+ value: {
272
+ kind,
273
+ ref: suggestedRef(source['ref'], `${path}.ref`),
274
+ value: expression.value,
275
+ },
276
+ };
277
+ }
278
+ case 'null':
279
+ case 'remove':
280
+ return { ok: true, value: { kind, ref: suggestedRef(source['ref'], `${path}.ref`) } };
281
+ case 'append': {
282
+ const row = expressionParams(source['row'], `${path}.row`);
283
+ if (!row.ok)
284
+ return row;
285
+ return {
286
+ ok: true,
287
+ value: { kind, fieldId: text(source, 'fieldId'), row: row.value },
288
+ };
289
+ }
290
+ case 'removeRow':
291
+ return {
292
+ ok: true,
293
+ value: {
294
+ kind,
295
+ fieldId: text(source, 'fieldId'),
296
+ rowKey: rowKeyValue(source['rowKey']),
297
+ },
298
+ };
299
+ default:
300
+ return impossible(`an operation \`${String(kind)}\` the metaschema does not enumerate`);
301
+ }
302
+ }
303
+ /** The binding an operation writes, `schema.v1.json` `suggestedRef`. */
304
+ function suggestedRef(value, path) {
305
+ const source = record(value, `a \`${path}\` that is not an object`);
306
+ const rowKey = source['rowKey'];
307
+ return {
308
+ fieldId: text(source, 'fieldId'),
309
+ ...(rowKey === undefined ? {} : { rowKey: rowKeyValue(rowKey) }),
310
+ };
311
+ }
312
+ /** A row address the metaschema admitted: a wire key, or the reserved `current` spelling. */
313
+ function rowKeyValue(value) {
314
+ if (typeof value === 'string' && value !== '')
315
+ return value;
316
+ if (typeof value === 'number' && Number.isSafeInteger(value))
317
+ return value;
318
+ return impossible('a row key that is neither text nor a whole number');
319
+ }
320
+ function optionSource(value) {
321
+ const source = record(value, 'an `optionSource` that is not an object');
322
+ if (source['kind'] === 'field')
323
+ return { kind: 'field', fieldId: text(source, 'fieldId') };
324
+ const dependsOn = textMembers(source, 'dependsOn');
325
+ return {
326
+ kind: 'host',
327
+ sourceId: text(source, 'sourceId'),
328
+ ...(dependsOn.length === 0 ? {} : { dependsOn }),
329
+ };
330
+ }
331
+ function rowKey(value) {
332
+ const source = record(value, 'a `rowKey` that is not an object');
333
+ const mode = source['mode'];
334
+ if (mode !== 'natural' && mode !== 'assigned')
335
+ return impossible('a `rowKey.mode`');
336
+ const fieldId = optionalText(source, 'fieldId');
337
+ return fieldId === undefined ? { mode } : { mode, fieldId };
338
+ }
339
+ function annotations(value) {
340
+ const source = record(value, 'an `annotations` that is not an object');
341
+ const narrowed = [];
342
+ for (const [name, entry] of Object.entries(source)) {
343
+ if (typeof entry !== 'boolean' && typeof entry !== 'string') {
344
+ return impossible(`an annotation \`${name}\` that is neither a boolean nor a code`);
345
+ }
346
+ narrowed.push([name, entry]);
347
+ }
348
+ // Define own properties rather than assigning them, so a `__proto__` annotation stays data.
349
+ return Object.fromEntries(narrowed);
350
+ }
351
+ /** One embedded case list a field declaration carries, read as the cases an artifact embeds. */
352
+ function declarationEvidence(source, path, member) {
353
+ return members(source, member).map((entry, index) => {
354
+ const one = testCase(entry, `${path}.${member}[${String(index)}]`);
355
+ return one.ok ? one.value : impossible(`a \`${member}\` case the reader refuses`);
356
+ });
357
+ }
358
+ /** One declared field at any nesting level, at the document path its evidence members sit on. */
359
+ function authoringField(value, path) {
360
+ const source = record(value, 'a field that is not an object');
361
+ const control = source['control'];
362
+ const options = [];
363
+ for (const [index, entry] of members(source, 'options').entries()) {
364
+ const option = fieldOption(entry, `${path}.options[${String(index)}]`);
365
+ if (!option.ok)
366
+ return option;
367
+ options.push(option.value);
368
+ }
369
+ const fields = [];
370
+ for (const [index, entry] of members(source, 'fields').entries()) {
371
+ const field = authoringField(entry, `${path}.fields[${String(index)}]`);
372
+ if (!field.ok)
373
+ return field;
374
+ fields.push(field.value);
375
+ }
376
+ const items = source['items'] === undefined ? undefined : fieldItem(source['items']);
377
+ const optionSourceValue = source['optionSource'];
378
+ const rowKeyValue = source['rowKey'];
379
+ const annotationsValue = source['annotations'];
380
+ const covers = textMembers(source, 'covers');
381
+ const labelKey = optionalText(source, 'labelKey');
382
+ const descriptionKey = optionalText(source, 'descriptionKey');
383
+ const placeholderKey = optionalText(source, 'placeholderKey');
384
+ const currency = optionalText(source, 'currency');
385
+ const units = textMembers(source, 'units');
386
+ const unit = optionalDurationUnit(source, 'unit');
387
+ const step = optionalBound(source, 'step');
388
+ const onHide = source['onHide'];
389
+ const required = optionalBoolean(source, 'required');
390
+ const requiredMessageKey = optionalText(source, 'requiredMessageKey');
391
+ const requiredWhen = optionalText(source, 'requiredWhen');
392
+ const visibleWhen = optionalText(source, 'visibleWhen');
393
+ const minLength = optionalWholeNumber(source, 'minLength');
394
+ const maxLength = optionalWholeNumber(source, 'maxLength');
395
+ const minLengthMessageKey = optionalText(source, 'minLengthMessageKey');
396
+ const maxLengthMessageKey = optionalText(source, 'maxLengthMessageKey');
397
+ const minItems = optionalWholeNumber(source, 'minItems');
398
+ const maxItems = optionalWholeNumber(source, 'maxItems');
399
+ const maxScale = optionalWholeNumber(source, 'maxScale');
400
+ const maxScaleMessageKey = optionalText(source, 'maxScaleMessageKey');
401
+ const minimum = optionalBound(source, 'minimum');
402
+ const maximum = optionalBound(source, 'maximum');
403
+ const minimumMessageKey = optionalText(source, 'minimumMessageKey');
404
+ const maximumMessageKey = optionalText(source, 'maximumMessageKey');
405
+ const accept = textMembers(source, 'accept');
406
+ const minBytes = optionalWholeNumber(source, 'minBytes');
407
+ const maxBytes = optionalWholeNumber(source, 'maxBytes');
408
+ // The authored slot is the Expr v1 source; the compiled artifact carries the AST beside
409
+ // `rowsFromSource`, which a draft does not hold.
410
+ const rowsFrom = optionalText(source, 'rowsFrom');
411
+ const requiredTestCases = declarationEvidence(source, path, 'requiredTestCases');
412
+ const visibleWhenTestCases = declarationEvidence(source, path, 'visibleWhenTestCases');
413
+ const minimumTestCases = declarationEvidence(source, path, 'minimumTestCases');
414
+ const maximumTestCases = declarationEvidence(source, path, 'maximumTestCases');
415
+ const minLengthTestCases = declarationEvidence(source, path, 'minLengthTestCases');
416
+ const maxLengthTestCases = declarationEvidence(source, path, 'maxLengthTestCases');
417
+ return {
418
+ ok: true,
419
+ value: {
420
+ fieldId: text(source, 'fieldId'),
421
+ primitive: requiredPrimitive(source),
422
+ ...(control === undefined
423
+ ? {}
424
+ : isControl(control)
425
+ ? { control }
426
+ : impossible('a control the metaschema does not enumerate')),
427
+ ...(labelKey === undefined ? {} : { labelKey }),
428
+ ...(descriptionKey === undefined ? {} : { descriptionKey }),
429
+ ...(placeholderKey === undefined ? {} : { placeholderKey }),
430
+ ...(currency === undefined ? {} : { currency }),
431
+ ...(source['scale'] === undefined ? {} : { scale: wholeNumber(source, 'scale') }),
432
+ ...(units.length === 0 ? {} : { units }),
433
+ ...(unit === undefined ? {} : { unit }),
434
+ ...(step === undefined ? {} : { step }),
435
+ ...(options.length === 0 ? {} : { options }),
436
+ ...(optionSourceValue === undefined ? {} : { optionSource: optionSource(optionSourceValue) }),
437
+ ...(items === undefined ? {} : { items }),
438
+ ...(fields.length === 0 ? {} : { fields }),
439
+ ...(rowKeyValue === undefined ? {} : { rowKey: rowKey(rowKeyValue) }),
440
+ ...(annotationsValue === undefined ? {} : { annotations: annotations(annotationsValue) }),
441
+ ...(onHide === undefined ? {} : { onHide: oneOf(onHide, ON_HIDE, 'an `onHide` policy') }),
442
+ ...(covers.length === 0 ? {} : { covers }),
443
+ ...(required === undefined ? {} : { required }),
444
+ ...(requiredMessageKey === undefined ? {} : { requiredMessageKey }),
445
+ ...(requiredWhen === undefined ? {} : { requiredWhen }),
446
+ ...(visibleWhen === undefined ? {} : { visibleWhen }),
447
+ ...(minLength === undefined ? {} : { minLength }),
448
+ ...(maxLength === undefined ? {} : { maxLength }),
449
+ ...(minLengthMessageKey === undefined ? {} : { minLengthMessageKey }),
450
+ ...(maxLengthMessageKey === undefined ? {} : { maxLengthMessageKey }),
451
+ ...(minItems === undefined ? {} : { minItems }),
452
+ ...(maxItems === undefined ? {} : { maxItems }),
453
+ ...(maxScale === undefined ? {} : { maxScale }),
454
+ ...(maxScaleMessageKey === undefined ? {} : { maxScaleMessageKey }),
455
+ ...(minimum === undefined ? {} : { minimum }),
456
+ ...(maximum === undefined ? {} : { maximum }),
457
+ ...(minimumMessageKey === undefined ? {} : { minimumMessageKey }),
458
+ ...(maximumMessageKey === undefined ? {} : { maximumMessageKey }),
459
+ ...(accept.length === 0 ? {} : { accept }),
460
+ ...(minBytes === undefined ? {} : { minBytes }),
461
+ ...(maxBytes === undefined ? {} : { maxBytes }),
462
+ ...(rowsFrom === undefined ? {} : { rowsFrom }),
463
+ ...(requiredTestCases.length === 0 ? {} : { requiredTestCases }),
464
+ ...(visibleWhenTestCases.length === 0 ? {} : { visibleWhenTestCases }),
465
+ ...(minimumTestCases.length === 0 ? {} : { minimumTestCases }),
466
+ ...(maximumTestCases.length === 0 ? {} : { maximumTestCases }),
467
+ ...(minLengthTestCases.length === 0 ? {} : { minLengthTestCases }),
468
+ ...(maxLengthTestCases.length === 0 ? {} : { maxLengthTestCases }),
469
+ },
470
+ };
471
+ }
472
+ /**
473
+ * One expression slot, as the source text an author writes.
474
+ *
475
+ * The metaschema admits the compiler's parsed AST here too, because a compiled artifact uses the
476
+ * same member. A draft is authored, so the parsed form is refused: reading it as a document would
477
+ * produce a rule no editor can show and no printer round-trips.
478
+ */
479
+ function expressionSource(value, path) {
480
+ if (typeof value === 'string')
481
+ return { ok: true, value };
482
+ return {
483
+ ok: false,
484
+ refusal: authoringRefusal('INVALID_STRUCTURE', path, "this document holds the compiler's parsed expression tree; an authoring draft stores the Expr v1 source text, so write the source the tree was parsed from"),
485
+ };
486
+ }
487
+ /** One map of Expr v1 source slots, keyed by message argument or appended-row member. */
488
+ function expressionParams(value, path) {
489
+ const source = record(value, `a \`${path}\` that is not an object`);
490
+ const narrowed = [];
491
+ for (const [name, entry] of Object.entries(source)) {
492
+ const read = expressionSource(entry, `${path}.${name}`);
493
+ if (!read.ok)
494
+ return read;
495
+ narrowed.push([name, read.value]);
496
+ }
497
+ return { ok: true, value: Object.fromEntries(narrowed) };
498
+ }
499
+ function testCase(value, path) {
500
+ const source = record(value, `a \`${path}\` that is not an object`);
501
+ const context = source['context'];
502
+ const redacted = members(source, 'redacted').map((entry) => fieldRef(entry, 'redacted'));
503
+ const sessionWrites = members(source, 'sessionWrites').map((entry) => fieldRef(entry, 'sessionWrites'));
504
+ const access = source['accessOverlay'];
505
+ const overlay = access === undefined ? undefined : authoredOverlay(access, path);
506
+ if (overlay !== undefined && !overlay.ok)
507
+ return overlay;
508
+ const capabilities = textMembers(source, 'capabilities');
509
+ const recordDate = optionalText(source, 'recordDate');
510
+ const tier = source['tier'];
511
+ return {
512
+ ok: true,
513
+ value: {
514
+ caseId: text(source, 'caseId'),
515
+ expected: wireValue(source['expected']),
516
+ // The metaschema requires `values` on every case: a case with no input decides nothing.
517
+ values: wireRecord(source['values'], 'a case without a `values` object'),
518
+ ...(context === undefined
519
+ ? {}
520
+ : { context: wireRecord(context, 'a case `context` that is not a value map') }),
521
+ ...(redacted.length === 0 ? {} : { redacted }),
522
+ ...(sessionWrites.length === 0 ? {} : { sessionWrites }),
523
+ ...(overlay === undefined || !overlay.ok ? {} : { accessOverlay: overlay.value }),
524
+ ...(recordDate === undefined ? {} : { recordDate }),
525
+ ...(capabilities.length === 0 ? {} : { capabilities }),
526
+ ...(tier === undefined ? {} : { tier: oneOf(tier, TIERS, 'a case `tier`') }),
527
+ },
528
+ };
529
+ }
530
+ /**
531
+ * The access overlay a case evaluates under, read by the runtime's own overlay parser.
532
+ *
533
+ * `toAccessOverlay` is the one parser of that shape, so a case's overlay and a host-supplied one
534
+ * are read by one function rather than two. It refuses by throwing, and its refusal becomes the
535
+ * document's: the metaschema admits a few values the runtime will not decide a read from (a `-0`
536
+ * version, say), and those are a document defect rather than a crash.
537
+ *
538
+ * The parser resolves an omitted `defaultAccess` to `read_only`, and the authored slot keeps the
539
+ * omission: a draft that states no default is the fail-closed one at evaluation time, so stating
540
+ * it here would rewrite a stored document on a load-and-save that changed nothing.
541
+ */
542
+ function authoredOverlay(value, path) {
543
+ try {
544
+ const overlay = toAccessOverlay(value, `${path}.accessOverlay`);
545
+ const stated = isRecord(value) && value['defaultAccess'] !== undefined;
546
+ return {
547
+ ok: true,
548
+ value: {
549
+ accessVersion: overlay.accessVersion,
550
+ ...(stated ? { defaultAccess: overlay.defaultAccess } : {}),
551
+ ...(overlay.allowMaskedRead === undefined
552
+ ? {}
553
+ : { allowMaskedRead: overlay.allowMaskedRead }),
554
+ rules: overlay.rules,
555
+ },
556
+ };
557
+ }
558
+ catch (cause) {
559
+ if (!(cause instanceof AxiomError))
560
+ throw cause;
561
+ return {
562
+ ok: false,
563
+ refusal: authoringRefusal('INVALID_STRUCTURE', cause.path === '' ? `${path}.accessOverlay` : cause.path, cause.message),
564
+ };
565
+ }
566
+ }
567
+ function gateItem(value, path) {
568
+ const source = record(value, `a \`${path}\` that is not an object`);
569
+ const expr = expressionSource(source['expr'], `${path}.expr`);
570
+ if (!expr.ok)
571
+ return expr;
572
+ const params = source['params'];
573
+ const readParams = params === undefined ? undefined : expressionParams(params, `${path}.params`);
574
+ if (readParams !== undefined && !readParams.ok)
575
+ return readParams;
576
+ return {
577
+ ok: true,
578
+ value: {
579
+ itemId: text(source, 'itemId'),
580
+ expr: expr.value,
581
+ messageKey: text(source, 'messageKey'),
582
+ ...(readParams === undefined ? {} : { params: readParams.value }),
583
+ },
584
+ };
585
+ }
586
+ function gate(value, path) {
587
+ const source = record(value, `a \`${path}\` that is not an object`);
588
+ const items = [];
589
+ for (const [index, entry] of members(source, 'items').entries()) {
590
+ const item = gateItem(entry, `${path}.items[${String(index)}]`);
591
+ if (!item.ok)
592
+ return item;
593
+ items.push(item.value);
594
+ }
595
+ const activeWhen = source['activeWhen'];
596
+ const readActive = activeWhen === undefined ? undefined : expressionSource(activeWhen, `${path}.activeWhen`);
597
+ if (readActive !== undefined && !readActive.ok)
598
+ return readActive;
599
+ const policy = source['onReadOnlyError'];
600
+ return {
601
+ ok: true,
602
+ value: {
603
+ items,
604
+ hard: booleanMember(source, 'hard'),
605
+ ...(readActive === undefined ? {} : { activeWhen: readActive.value }),
606
+ ...(policy === undefined
607
+ ? {}
608
+ : { onReadOnlyError: oneOf(policy, GATE_POLICIES, 'a read-only gate policy') }),
609
+ },
610
+ };
611
+ }
612
+ function screenReason(value, path) {
613
+ const source = record(value, `a \`${path}\` that is not an object`);
614
+ const when = expressionSource(source['when'], `${path}.when`);
615
+ if (!when.ok)
616
+ return when;
617
+ const params = source['params'];
618
+ const readParams = params === undefined ? undefined : expressionParams(params, `${path}.params`);
619
+ if (readParams !== undefined && !readParams.ok)
620
+ return readParams;
621
+ return {
622
+ ok: true,
623
+ value: {
624
+ code: text(source, 'code'),
625
+ messageKey: text(source, 'messageKey'),
626
+ when: when.value,
627
+ ...(readParams === undefined ? {} : { params: readParams.value }),
628
+ },
629
+ };
630
+ }
631
+ function authoringRule(value, path) {
632
+ const source = record(value, `a \`${path}\` that is not an object`);
633
+ const expr = expressionSource(source['expr'], `${path}.expr`);
634
+ if (!expr.ok)
635
+ return expr;
636
+ const referWhen = source['referWhen'];
637
+ const readRefer = referWhen === undefined ? undefined : expressionSource(referWhen, `${path}.referWhen`);
638
+ if (readRefer !== undefined && !readRefer.ok)
639
+ return readRefer;
640
+ const params = source['params'];
641
+ const readParams = params === undefined ? undefined : expressionParams(params, `${path}.params`);
642
+ if (readParams !== undefined && !readParams.ok)
643
+ return readParams;
644
+ const gateValue = source['gate'];
645
+ const readGate = gateValue === undefined ? undefined : gate(gateValue, `${path}.gate`);
646
+ if (readGate !== undefined && !readGate.ok)
647
+ return readGate;
648
+ const reasons = [];
649
+ for (const [index, entry] of members(source, 'reasons').entries()) {
650
+ const reason = screenReason(entry, `${path}.reasons[${String(index)}]`);
651
+ if (!reason.ok)
652
+ return reason;
653
+ reasons.push(reason.value);
654
+ }
655
+ const cases = [];
656
+ for (const [index, entry] of members(source, 'testCases').entries()) {
657
+ const one = testCase(entry, `${path}.testCases[${String(index)}]`);
658
+ if (!one.ok)
659
+ return one;
660
+ cases.push(one.value);
661
+ }
662
+ const fieldIds = textMembers(source, 'fieldIds');
663
+ const severity = source['severity'];
664
+ const note = optionalText(source, 'note');
665
+ const fieldId = optionalText(source, 'fieldId');
666
+ const targetFieldId = optionalText(source, 'targetFieldId');
667
+ const messageKey = optionalText(source, 'messageKey');
668
+ const overrideFieldId = optionalText(source, 'overrideFieldId');
669
+ const suggests = [];
670
+ for (const [index, entry] of members(source, 'suggests').entries()) {
671
+ const read = suggestion(entry, `${path}.suggests[${String(index)}]`);
672
+ if (!read.ok)
673
+ return read;
674
+ suggests.push(read.value);
675
+ }
676
+ return {
677
+ ok: true,
678
+ value: {
679
+ ruleId: text(source, 'ruleId'),
680
+ kind: oneOf(source['kind'], RULE_KINDS, 'a rule kind'),
681
+ scope: oneOf(source['scope'], RULE_SCOPES, 'a rule scope'),
682
+ evaluateOn: oneOf(source['evaluateOn'], TIERS, 'a rule tier'),
683
+ expr: expr.value,
684
+ ...(note === undefined ? {} : { note }),
685
+ ...(severity === undefined
686
+ ? {}
687
+ : { severity: oneOf(severity, SEVERITIES, 'a rule severity') }),
688
+ ...(fieldId === undefined ? {} : { fieldId }),
689
+ ...(fieldIds.length === 0 ? {} : { fieldIds }),
690
+ ...(targetFieldId === undefined ? {} : { targetFieldId }),
691
+ ...(messageKey === undefined ? {} : { messageKey }),
692
+ ...(readParams === undefined ? {} : { params: readParams.value }),
693
+ ...(readRefer === undefined ? {} : { referWhen: readRefer.value }),
694
+ ...(reasons.length === 0 ? {} : { reasons }),
695
+ ...(overrideFieldId === undefined ? {} : { overrideFieldId }),
696
+ ...(readGate === undefined ? {} : { gate: readGate.value }),
697
+ ...(suggests.length === 0 ? {} : { suggests }),
698
+ ...(cases.length === 0 ? {} : { testCases: cases }),
699
+ },
700
+ };
701
+ }
702
+ function contextRoot(value) {
703
+ const source = record(value, 'a context root that is not an object');
704
+ const nullability = source['nullability'];
705
+ const items = source['items'] === undefined ? undefined : fieldItem(source['items']);
706
+ return {
707
+ path: text(source, 'path'),
708
+ primitive: requiredPrimitive(source),
709
+ ...(nullability === undefined
710
+ ? {}
711
+ : {
712
+ nullability: oneOf(nullability, { required: true, optional: true }, 'a context root nullability'),
713
+ }),
714
+ ...(items === undefined ? {} : { items }),
715
+ };
716
+ }
717
+ function datasetReference(value) {
718
+ const source = record(value, 'a dataset reference that is not an object');
719
+ return {
720
+ tableId: text(source, 'tableId'),
721
+ tableVersion: wholeNumber(source, 'tableVersion'),
722
+ };
723
+ }
724
+ /**
725
+ * The catalogue as authored text.
726
+ *
727
+ * The metaschema admits a compiled message AST in an entry too; a draft states the ICU source an
728
+ * author writes, so a document carrying the compiled form is refused with that repair.
729
+ */
730
+ function catalogue(value) {
731
+ const source = record(value, 'a `messages` that is not an object');
732
+ const narrowed = [];
733
+ for (const [key, entry] of Object.entries(source)) {
734
+ const entryRecord = record(entry, `a message entry \`${key}\` that is not an object`);
735
+ const texts = [];
736
+ for (const [locale, message] of Object.entries(entryRecord)) {
737
+ if (typeof message !== 'string') {
738
+ return {
739
+ ok: false,
740
+ refusal: authoringRefusal('INVALID_STRUCTURE', `$.messages[${JSON.stringify(key)}][${JSON.stringify(locale)}]`, `this document holds the compiler's compiled form of message \`${key}\`; an authoring draft stores the ICU MessageFormat source text, so write the source it was compiled from`),
741
+ };
742
+ }
743
+ texts.push([locale, message]);
744
+ }
745
+ narrowed.push([key, Object.fromEntries(texts)]);
746
+ }
747
+ return { ok: true, value: Object.fromEntries(narrowed) };
748
+ }
749
+ /** One summary readout, with its value kept in the authored source form. */
750
+ function layoutReadout(value, path) {
751
+ const source = record(value, `a \`${path}\` that is not an object`);
752
+ const readValue = expressionSource(source['value'], `${path}.value`);
753
+ if (!readValue.ok)
754
+ return readValue;
755
+ return { ok: true, value: { ...source, value: readValue.value } };
756
+ }
757
+ /** One layout node, with every expression slot kept in the authored source form. */
758
+ function layoutNode(value, path) {
759
+ const source = record(value, 'a layout node that is not an object');
760
+ const visibleWhen = source['visibleWhen'];
761
+ const readVisible = visibleWhen === undefined ? undefined : expressionSource(visibleWhen, `${path}.visibleWhen`);
762
+ if (readVisible !== undefined && !readVisible.ok)
763
+ return readVisible;
764
+ const params = source['params'];
765
+ const readParams = params === undefined ? undefined : expressionParams(params, `${path}.params`);
766
+ if (readParams !== undefined && !readParams.ok)
767
+ return readParams;
768
+ const readouts = [];
769
+ for (const [index, entry] of members(source, 'readouts').entries()) {
770
+ const readout = layoutReadout(entry, `${path}.readouts[${String(index)}]`);
771
+ if (!readout.ok)
772
+ return readout;
773
+ readouts.push(readout.value);
774
+ }
775
+ const children = [];
776
+ for (const [index, entry] of members(source, 'children').entries()) {
777
+ const child = layoutNode(entry, `${path}.children[${String(index)}]`);
778
+ if (!child.ok)
779
+ return child;
780
+ children.push(child.value);
781
+ }
782
+ return {
783
+ ok: true,
784
+ value: {
785
+ ...source,
786
+ ...(readVisible === undefined ? {} : { visibleWhen: readVisible.value }),
787
+ ...(readParams === undefined ? {} : { params: readParams.value }),
788
+ ...(source['readouts'] === undefined ? {} : { readouts }),
789
+ ...(source['children'] === undefined ? {} : { children }),
790
+ },
791
+ };
792
+ }
793
+ /** The layout as the author wrote it; the ten-node grammar is the compiler's walk to judge. */
794
+ function layout(source) {
795
+ const nodes = [];
796
+ for (const [index, entry] of members(source, 'layout').entries()) {
797
+ const node = layoutNode(entry, `$.layout[${String(index)}]`);
798
+ if (!node.ok)
799
+ return node;
800
+ nodes.push(node.value);
801
+ }
802
+ return { ok: true, value: nodes };
803
+ }
804
+ /**
805
+ * Narrows one document the closed metaschema admitted.
806
+ *
807
+ * Members the metaschema makes optional but the store holds unconditionally are stated empty
808
+ * here: an absent `layout` and an absent `messages` are the same document as an empty list and an
809
+ * empty catalogue, and the store's type states them once.
810
+ */
811
+ function narrowDocument(source) {
812
+ const messages = catalogue(source['messages'] ?? {});
813
+ if (!messages.ok)
814
+ return messages;
815
+ const rules = [];
816
+ for (const [index, entry] of members(source, 'rules').entries()) {
817
+ const rule = authoringRule(entry, `$.rules[${String(index)}]`);
818
+ if (!rule.ok)
819
+ return rule;
820
+ rules.push(rule.value);
821
+ }
822
+ const readLayout = layout(source);
823
+ if (!readLayout.ok)
824
+ return readLayout;
825
+ const context = objectMember(source, 'context');
826
+ const contextValue = context === undefined ? undefined : { roots: members(context, 'roots').map(contextRoot) };
827
+ const locales = textMembers(source, 'locales');
828
+ const datasets = members(source, 'datasets').map(datasetReference);
829
+ const titleKey = optionalText(source, 'titleKey');
830
+ const primaryGateId = optionalText(source, 'primaryGateId');
831
+ const primaryScreenId = optionalText(source, 'primaryScreenId');
832
+ const defaultLocale = optionalText(source, 'defaultLocale');
833
+ const onHide = source['onHide'];
834
+ const fields = [];
835
+ for (const [index, entry] of members(source, 'fields').entries()) {
836
+ const field = authoringField(entry, `$.fields[${String(index)}]`);
837
+ if (!field.ok)
838
+ return field;
839
+ fields.push(field.value);
840
+ }
841
+ return {
842
+ ok: true,
843
+ document: {
844
+ schemaId: text(source, 'schemaId'),
845
+ schemaVersion: wholeNumber(source, 'schemaVersion'),
846
+ exprVersion: 1,
847
+ ...(titleKey === undefined ? {} : { titleKey }),
848
+ ...(contextValue === undefined ? {} : { context: contextValue }),
849
+ fields,
850
+ layout: readLayout.value,
851
+ ...(rules.length === 0 ? {} : { rules }),
852
+ ...(primaryGateId === undefined ? {} : { primaryGateId }),
853
+ ...(primaryScreenId === undefined ? {} : { primaryScreenId }),
854
+ messages: messages.value,
855
+ ...(locales.length === 0 ? {} : { locales }),
856
+ ...(defaultLocale === undefined ? {} : { defaultLocale }),
857
+ ...(datasets.length === 0 ? {} : { datasets }),
858
+ ...(onHide === undefined ? {} : { onHide: oneOf(onHide, ON_HIDE, 'an `onHide` policy') }),
859
+ },
860
+ };
861
+ }
862
+ /**
863
+ * Reads a host-loaded value as a draft document, or explains why it is not one.
864
+ *
865
+ * The runtime's restricted-profile reader first copies the host value onto a plain tree without
866
+ * invoking its accessors, refusing what canonical JSON cannot carry (negative zero, an accessor, a
867
+ * cycle or excessive nesting). The closed metaschema then answers over that data, so its refusal is
868
+ * the compiler's own — the failing keyword, the document path and the detail an author repairs.
869
+ * Only that plain tree reaches the narrowing, so a caller holds a document of the declared type
870
+ * rather than a checked `unknown`, which is what keeps every later authoring entry free of casts.
871
+ *
872
+ * One closed-shape reason is admitted rather than refused: the document-level `fields` `minItems`.
873
+ * `createEmptySchema` starts a draft with no declarations — that is the state the store opens on,
874
+ * and its first diagnostics already say it is not a compilable schema — so a draft a host saved
875
+ * before adding its first field has to load back as the document it was saved from. Every other
876
+ * reason, including an empty member list on a record, stays the metaschema's.
877
+ */
878
+ export function readDocument(value) {
879
+ let canonical;
880
+ try {
881
+ canonical = copyRestricted(value);
882
+ }
883
+ catch (cause) {
884
+ if (!(cause instanceof AxiomError))
885
+ throw cause;
886
+ return {
887
+ ok: false,
888
+ refusal: authoringRefusal('INVALID_STRUCTURE', cause.path === '' ? '$' : cause.path, cause.message),
889
+ };
890
+ }
891
+ const closed = closedShapeErrors(canonical);
892
+ const first = closed.find((diagnostic) => !isEmptyFieldList(diagnostic));
893
+ if (first !== undefined)
894
+ return { ok: false, refusal: closedShapeRefusal(first) };
895
+ const source = record(canonical, 'a document that is not an object');
896
+ const locale = storedLocaleDefect(source);
897
+ if (locale !== undefined) {
898
+ return {
899
+ ok: false,
900
+ refusal: authoringRefusal('INVALID_STRUCTURE', locale.path, locale.detail),
901
+ };
902
+ }
903
+ return narrowDocument(source);
904
+ }
905
+ /**
906
+ * Whether a closed-shape reason is the one a draft's starting state earns.
907
+ *
908
+ * The document's own `fields` list is `minItems: 1` in the metaschema because a compilable schema
909
+ * declares a declaration. An authoring draft is not that artifact yet: `createEmptySchema` returns
910
+ * the same empty list and `createAuthoringStore` reports it as `SCHEMA_INVALID` until the first
911
+ * command adds a field. Only a `field` node's own `fields` keeps the metaschema's bound.
912
+ */
913
+ function isEmptyFieldList(diagnostic) {
914
+ return diagnostic.keyword === 'minItems' && diagnostic.path === '$.fields';
915
+ }
916
+ /**
917
+ * The locale policy a stored document states and authoring does not, or `undefined` when it does.
918
+ *
919
+ * The metaschema checks the tags' shape and `uniqueItems`, and it requires `locales` and
920
+ * `defaultLocale` as a pair, but it cannot spell "canonical" or "the default is in the set" in
921
+ * JSON Schema. Those clauses are `localePolicyDefect`, the same one `createEmptySchema` starts a
922
+ * document under, so a document the constructor would have refused cannot enter the store.
923
+ */
924
+ function storedLocaleDefect(source) {
925
+ const defaultLocale = source['defaultLocale'];
926
+ const locales = source['locales'];
927
+ if (defaultLocale === undefined || locales === undefined)
928
+ return undefined;
929
+ if (typeof defaultLocale !== 'string') {
930
+ return impossible('a `defaultLocale` that is not text');
931
+ }
932
+ if (!Array.isArray(locales))
933
+ return impossible('a `locales` that is not an array');
934
+ const declared = [];
935
+ for (const entry of locales) {
936
+ if (typeof entry !== 'string')
937
+ return impossible('a `locales` entry that is not text');
938
+ declared.push(entry);
939
+ }
940
+ return localePolicyDefect(defaultLocale, declared);
941
+ }
942
+ //# sourceMappingURL=read.js.map