@azx-pbc/axiom-builder 0.0.0-stage → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +648 -2
- package/dist/cards/selectors.d.ts +70 -0
- package/dist/cards/selectors.d.ts.map +1 -0
- package/dist/cards/selectors.js +194 -0
- package/dist/cards/selectors.js.map +1 -0
- package/dist/definition.d.ts +36 -0
- package/dist/definition.d.ts.map +1 -0
- package/dist/definition.js +48 -0
- package/dist/definition.js.map +1 -0
- package/dist/evidence/cases.d.ts +44 -0
- package/dist/evidence/cases.d.ts.map +1 -0
- package/dist/evidence/cases.js +578 -0
- package/dist/evidence/cases.js.map +1 -0
- package/dist/evidence/repairs.d.ts +99 -0
- package/dist/evidence/repairs.d.ts.map +1 -0
- package/dist/evidence/repairs.js +181 -0
- package/dist/evidence/repairs.js.map +1 -0
- package/dist/evidence/summary.d.ts +35 -0
- package/dist/evidence/summary.d.ts.map +1 -0
- package/dist/evidence/summary.js +38 -0
- package/dist/evidence/summary.js.map +1 -0
- package/dist/i18n/gaps.d.ts +3 -0
- package/dist/i18n/gaps.d.ts.map +1 -0
- package/dist/i18n/gaps.js +2 -0
- package/dist/i18n/gaps.js.map +1 -0
- package/dist/i18n/message-slots.d.ts +19 -0
- package/dist/i18n/message-slots.d.ts.map +1 -0
- package/dist/i18n/message-slots.js +101 -0
- package/dist/i18n/message-slots.js.map +1 -0
- package/dist/i18n/text-locale.d.ts +13 -0
- package/dist/i18n/text-locale.d.ts.map +1 -0
- package/dist/i18n/text-locale.js +14 -0
- package/dist/i18n/text-locale.js.map +1 -0
- package/dist/i18n/translations.d.ts +56 -0
- package/dist/i18n/translations.d.ts.map +1 -0
- package/dist/i18n/translations.js +103 -0
- package/dist/i18n/translations.js.map +1 -0
- package/dist/impact/references.d.ts +143 -0
- package/dist/impact/references.d.ts.map +1 -0
- package/dist/impact/references.js +429 -0
- package/dist/impact/references.js.map +1 -0
- package/dist/index.d.ts +86 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +58 -0
- package/dist/index.js.map +1 -0
- package/dist/preview/compile-preview.d.ts +67 -0
- package/dist/preview/compile-preview.d.ts.map +1 -0
- package/dist/preview/compile-preview.js +286 -0
- package/dist/preview/compile-preview.js.map +1 -0
- package/dist/preview/handle.d.ts +119 -0
- package/dist/preview/handle.d.ts.map +1 -0
- package/dist/preview/handle.js +2 -0
- package/dist/preview/handle.js.map +1 -0
- package/dist/preview/held.d.ts +39 -0
- package/dist/preview/held.d.ts.map +1 -0
- package/dist/preview/held.js +15 -0
- package/dist/preview/held.js.map +1 -0
- package/dist/preview/measurement.d.ts +5 -0
- package/dist/preview/measurement.d.ts.map +1 -0
- package/dist/preview/measurement.js +18 -0
- package/dist/preview/measurement.js.map +1 -0
- package/dist/publish/build.d.ts +33 -0
- package/dist/publish/build.d.ts.map +1 -0
- package/dist/publish/build.js +15 -0
- package/dist/publish/build.js.map +1 -0
- package/dist/publish/diff.d.ts +208 -0
- package/dist/publish/diff.d.ts.map +1 -0
- package/dist/publish/diff.js +227 -0
- package/dist/publish/diff.js.map +1 -0
- package/dist/publish/review.d.ts +41 -0
- package/dist/publish/review.d.ts.map +1 -0
- package/dist/publish/review.js +22 -0
- package/dist/publish/review.js.map +1 -0
- package/dist/schema/dataset-input.d.ts +30 -0
- package/dist/schema/dataset-input.d.ts.map +1 -0
- package/dist/schema/dataset-input.js +58 -0
- package/dist/schema/dataset-input.js.map +1 -0
- package/dist/schema/dataset.d.ts +166 -0
- package/dist/schema/dataset.d.ts.map +1 -0
- package/dist/schema/dataset.js +281 -0
- package/dist/schema/dataset.js.map +1 -0
- package/dist/schema/declarations.d.ts +15 -0
- package/dist/schema/declarations.d.ts.map +1 -0
- package/dist/schema/declarations.js +47 -0
- package/dist/schema/declarations.js.map +1 -0
- package/dist/schema/document.d.ts +504 -0
- package/dist/schema/document.d.ts.map +1 -0
- package/dist/schema/document.js +337 -0
- package/dist/schema/document.js.map +1 -0
- package/dist/schema/error.d.ts +15 -0
- package/dist/schema/error.d.ts.map +1 -0
- package/dist/schema/error.js +18 -0
- package/dist/schema/error.js.map +1 -0
- package/dist/schema/field-id.d.ts +8 -0
- package/dist/schema/field-id.d.ts.map +1 -0
- package/dist/schema/field-id.js +47 -0
- package/dist/schema/field-id.js.map +1 -0
- package/dist/schema/layout.d.ts +118 -0
- package/dist/schema/layout.d.ts.map +1 -0
- package/dist/schema/layout.js +101 -0
- package/dist/schema/layout.js.map +1 -0
- package/dist/schema/palette.d.ts +103 -0
- package/dist/schema/palette.d.ts.map +1 -0
- package/dist/schema/palette.js +140 -0
- package/dist/schema/palette.js.map +1 -0
- package/dist/schema/read.d.ts +49 -0
- package/dist/schema/read.d.ts.map +1 -0
- package/dist/schema/read.js +942 -0
- package/dist/schema/read.js.map +1 -0
- package/dist/store/apply-dataset.d.ts +14 -0
- package/dist/store/apply-dataset.d.ts.map +1 -0
- package/dist/store/apply-dataset.js +513 -0
- package/dist/store/apply-dataset.js.map +1 -0
- package/dist/store/apply.d.ts +37 -0
- package/dist/store/apply.d.ts.map +1 -0
- package/dist/store/apply.js +2269 -0
- package/dist/store/apply.js.map +1 -0
- package/dist/store/candidate.d.ts +37 -0
- package/dist/store/candidate.d.ts.map +1 -0
- package/dist/store/candidate.js +178 -0
- package/dist/store/candidate.js.map +1 -0
- package/dist/store/commands/coverage.d.ts +58 -0
- package/dist/store/commands/coverage.d.ts.map +1 -0
- package/dist/store/commands/coverage.js +354 -0
- package/dist/store/commands/coverage.js.map +1 -0
- package/dist/store/commands/dataset.d.ts +26 -0
- package/dist/store/commands/dataset.d.ts.map +1 -0
- package/dist/store/commands/dataset.js +368 -0
- package/dist/store/commands/dataset.js.map +1 -0
- package/dist/store/commands/document.d.ts +21 -0
- package/dist/store/commands/document.d.ts.map +1 -0
- package/dist/store/commands/document.js +160 -0
- package/dist/store/commands/document.js.map +1 -0
- package/dist/store/commands/field.d.ts +32 -0
- package/dist/store/commands/field.d.ts.map +1 -0
- package/dist/store/commands/field.js +572 -0
- package/dist/store/commands/field.js.map +1 -0
- package/dist/store/commands/layout.d.ts +16 -0
- package/dist/store/commands/layout.d.ts.map +1 -0
- package/dist/store/commands/layout.js +70 -0
- package/dist/store/commands/layout.js.map +1 -0
- package/dist/store/commands/parse.d.ts +19 -0
- package/dist/store/commands/parse.d.ts.map +1 -0
- package/dist/store/commands/parse.js +328 -0
- package/dist/store/commands/parse.js.map +1 -0
- package/dist/store/commands/readers.d.ts +198 -0
- package/dist/store/commands/readers.d.ts.map +1 -0
- package/dist/store/commands/readers.js +711 -0
- package/dist/store/commands/readers.js.map +1 -0
- package/dist/store/commands/rule.d.ts +24 -0
- package/dist/store/commands/rule.d.ts.map +1 -0
- package/dist/store/commands/rule.js +379 -0
- package/dist/store/commands/rule.js.map +1 -0
- package/dist/store/commands/shapes.d.ts +900 -0
- package/dist/store/commands/shapes.d.ts.map +1 -0
- package/dist/store/commands/shapes.js +166 -0
- package/dist/store/commands/shapes.js.map +1 -0
- package/dist/store/commands/suggestions.d.ts +37 -0
- package/dist/store/commands/suggestions.d.ts.map +1 -0
- package/dist/store/commands/suggestions.js +177 -0
- package/dist/store/commands/suggestions.js.map +1 -0
- package/dist/store/commands.d.ts +16 -0
- package/dist/store/commands.d.ts.map +1 -0
- package/dist/store/commands.js +13 -0
- package/dist/store/commands.js.map +1 -0
- package/dist/store/dataset-candidate.d.ts +33 -0
- package/dist/store/dataset-candidate.d.ts.map +1 -0
- package/dist/store/dataset-candidate.js +31 -0
- package/dist/store/dataset-candidate.js.map +1 -0
- package/dist/store/dataset-pin.d.ts +93 -0
- package/dist/store/dataset-pin.d.ts.map +1 -0
- package/dist/store/dataset-pin.js +86 -0
- package/dist/store/dataset-pin.js.map +1 -0
- package/dist/store/dataset-store.d.ts +96 -0
- package/dist/store/dataset-store.d.ts.map +1 -0
- package/dist/store/dataset-store.js +112 -0
- package/dist/store/dataset-store.js.map +1 -0
- package/dist/store/history.d.ts +149 -0
- package/dist/store/history.d.ts.map +1 -0
- package/dist/store/history.js +173 -0
- package/dist/store/history.js.map +1 -0
- package/dist/store/measurement.d.ts +5 -0
- package/dist/store/measurement.d.ts.map +1 -0
- package/dist/store/measurement.js +20 -0
- package/dist/store/measurement.js.map +1 -0
- package/dist/store/notify.d.ts +11 -0
- package/dist/store/notify.d.ts.map +1 -0
- package/dist/store/notify.js +26 -0
- package/dist/store/notify.js.map +1 -0
- package/dist/store/published.d.ts +50 -0
- package/dist/store/published.d.ts.map +1 -0
- package/dist/store/published.js +27 -0
- package/dist/store/published.js.map +1 -0
- package/dist/store/refusals.d.ts +175 -0
- package/dist/store/refusals.d.ts.map +1 -0
- package/dist/store/refusals.js +160 -0
- package/dist/store/refusals.js.map +1 -0
- package/dist/store/steps.d.ts +14 -0
- package/dist/store/steps.d.ts.map +1 -0
- package/dist/store/steps.js +124 -0
- package/dist/store/steps.js.map +1 -0
- package/dist/store/store.d.ts +142 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/store.js +136 -0
- package/dist/store/store.js.map +1 -0
- package/dist/store/unchanged.d.ts +12 -0
- package/dist/store/unchanged.d.ts.map +1 -0
- package/dist/store/unchanged.js +35 -0
- package/dist/store/unchanged.js.map +1 -0
- package/dist/visual/emit.d.ts +21 -0
- package/dist/visual/emit.d.ts.map +1 -0
- package/dist/visual/emit.js +133 -0
- package/dist/visual/emit.js.map +1 -0
- package/dist/visual/parse.d.ts +32 -0
- package/dist/visual/parse.d.ts.map +1 -0
- package/dist/visual/parse.js +330 -0
- package/dist/visual/parse.js.map +1 -0
- package/dist/visual/project.d.ts +40 -0
- package/dist/visual/project.d.ts.map +1 -0
- package/dist/visual/project.js +349 -0
- package/dist/visual/project.js.map +1 -0
- package/dist/visual/represent.d.ts +50 -0
- package/dist/visual/represent.d.ts.map +1 -0
- package/dist/visual/represent.js +66 -0
- package/dist/visual/represent.js.map +1 -0
- package/dist/visual/vocabulary.d.ts +121 -0
- package/dist/visual/vocabulary.d.ts.map +1 -0
- package/dist/visual/vocabulary.js +32 -0
- package/dist/visual/vocabulary.js.map +1 -0
- package/package.json +52 -3
|
@@ -0,0 +1,711 @@
|
|
|
1
|
+
import { canonicalLocale, isLocaleTag } from '@azx-pbc/axiom-compiler';
|
|
2
|
+
import { AxiomError, CURRENCY_CODE_PATTERN, IDENTIFIER_PATTERN, ITEM_PRIMITIVES as RUNTIME_ITEM_PRIMITIVES, UNIT_CODE_PATTERN, isItemPrimitive, isRecord, isWireValue, parsePlainDate, toAccessOverlay, } from '@azx-pbc/axiom-runtime';
|
|
3
|
+
import { isCanonicalLocale } from '../../schema/document.js';
|
|
4
|
+
import { OPTION_ARRAY_ITEMS, fieldsControls } from '../../schema/palette.js';
|
|
5
|
+
import { parseVisual } from '../../visual/parse.js';
|
|
6
|
+
import { authoringRefusal } from '../refusals.js';
|
|
7
|
+
export function draft() {
|
|
8
|
+
return { refusal: undefined };
|
|
9
|
+
}
|
|
10
|
+
export function fail(draftIn, path, detail) {
|
|
11
|
+
if (draftIn.refusal === undefined) {
|
|
12
|
+
draftIn.refusal = authoringRefusal('MALFORMED_COMMAND', path, detail);
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
export function describe(value) {
|
|
17
|
+
if (value === undefined)
|
|
18
|
+
return 'nothing';
|
|
19
|
+
if (value === null)
|
|
20
|
+
return 'null';
|
|
21
|
+
if (Array.isArray(value))
|
|
22
|
+
return 'a list';
|
|
23
|
+
return `a ${typeof value}`;
|
|
24
|
+
}
|
|
25
|
+
function recordAt(value) {
|
|
26
|
+
return isRecord(value) ? value : undefined;
|
|
27
|
+
}
|
|
28
|
+
export function readRecord(draftIn, value, path, what) {
|
|
29
|
+
const record = recordAt(value);
|
|
30
|
+
if (record === undefined) {
|
|
31
|
+
return fail(draftIn, path, `${what} is an object; received ${describe(value)}`);
|
|
32
|
+
}
|
|
33
|
+
return record;
|
|
34
|
+
}
|
|
35
|
+
/** Refuses the first member the contract does not list, so an unknown member cannot ride along. */
|
|
36
|
+
export function onlyMembers(draftIn, record, allowed, path) {
|
|
37
|
+
for (const member of Object.keys(record)) {
|
|
38
|
+
if (allowed.includes(member))
|
|
39
|
+
continue;
|
|
40
|
+
fail(draftIn, path, `\`${member}\` is not a member this command states; the members are ${allowed.join(', ')}`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function readText(draftIn, record, key, path) {
|
|
45
|
+
const value = record[key];
|
|
46
|
+
if (typeof value !== 'string') {
|
|
47
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` must be text; received ${describe(value)}`);
|
|
48
|
+
}
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The closed metaschema's `messageKey` pattern: a dotted lower-case identifier.
|
|
53
|
+
*
|
|
54
|
+
* A catalogue key is an identity a declaration names, so a key the metaschema refuses is not one
|
|
55
|
+
* a command may store: the candidate gate would refuse the whole document after the store had
|
|
56
|
+
* already committed the edit. The parse refuses it at the member instead, with the spelling the
|
|
57
|
+
* contract admits.
|
|
58
|
+
*/
|
|
59
|
+
const MESSAGE_KEY_PATTERN = /^[a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)*$/u;
|
|
60
|
+
/** One message key, as the metaschema states it. */
|
|
61
|
+
export function readMessageKeyValue(draftIn, value, path) {
|
|
62
|
+
if (typeof value !== 'string') {
|
|
63
|
+
return fail(draftIn, path, `a message key is text; received ${describe(value)}`);
|
|
64
|
+
}
|
|
65
|
+
if (MESSAGE_KEY_PATTERN.test(value))
|
|
66
|
+
return value;
|
|
67
|
+
return fail(draftIn, path, `a message key is a dotted lower-case identifier, as \`field.f_note.required\`; received ${describe(value)}`);
|
|
68
|
+
}
|
|
69
|
+
/** One keyed message key member. */
|
|
70
|
+
export function readMessageKey(draftIn, record, key, path) {
|
|
71
|
+
if (!Object.hasOwn(record, key))
|
|
72
|
+
return undefined;
|
|
73
|
+
const value = record[key];
|
|
74
|
+
if (typeof value !== 'string') {
|
|
75
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is a message key; received ${describe(value)}`);
|
|
76
|
+
}
|
|
77
|
+
if (MESSAGE_KEY_PATTERN.test(value))
|
|
78
|
+
return value;
|
|
79
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is a dotted lower-case identifier, as \`field.f_note.required\`; received ${describe(value)}`);
|
|
80
|
+
}
|
|
81
|
+
export function readOptionalText(draftIn, record, key, path) {
|
|
82
|
+
if (!Object.hasOwn(record, key))
|
|
83
|
+
return undefined;
|
|
84
|
+
return readText(draftIn, record, key, path);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* A rule's author-facing `note`: plain text of 1 to 500 characters after trimming.
|
|
88
|
+
*
|
|
89
|
+
* A note documents why a rule exists, so an empty or whitespace-only one is refused at the
|
|
90
|
+
* member rather than stored as an empty explanation. The bound is counted in code points, the
|
|
91
|
+
* way the model counts string length, and the reader states the repair with the value's length.
|
|
92
|
+
*/
|
|
93
|
+
export function readRuleNote(draftIn, value, path) {
|
|
94
|
+
if (typeof value !== 'string') {
|
|
95
|
+
return fail(draftIn, path, `\`note\` must be text; it is author-facing documentation a respondent never sees; received ${describe(value)}`);
|
|
96
|
+
}
|
|
97
|
+
const trimmed = value.trim();
|
|
98
|
+
if (trimmed === '') {
|
|
99
|
+
return fail(draftIn, path, '`note` is blank after trimming, so it documents nothing; write the reason the rule exists in 1 to 500 characters, or omit `note`');
|
|
100
|
+
}
|
|
101
|
+
const length = Array.from(trimmed).length;
|
|
102
|
+
if (length > 500) {
|
|
103
|
+
return fail(draftIn, path, `\`note\` is ${String(length)} characters after trimming; a note is 1 to 500 characters, so shorten it to the reason the rule exists`);
|
|
104
|
+
}
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
export function readIdentifier(draftIn, record, key, path) {
|
|
108
|
+
const value = readText(draftIn, record, key, path);
|
|
109
|
+
if (value === undefined)
|
|
110
|
+
return undefined;
|
|
111
|
+
if (IDENTIFIER_PATTERN.test(value))
|
|
112
|
+
return value;
|
|
113
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is \`${value}\`; write a lower-case identifier: a letter first, then up to 63 lower-case letters, digits or underscores`);
|
|
114
|
+
}
|
|
115
|
+
export function readOptionalIdentifier(draftIn, record, key, path) {
|
|
116
|
+
if (!Object.hasOwn(record, key))
|
|
117
|
+
return undefined;
|
|
118
|
+
return readIdentifier(draftIn, record, key, path);
|
|
119
|
+
}
|
|
120
|
+
export function readOptionalLiteral(draftIn, record, key, path) {
|
|
121
|
+
if (!Object.hasOwn(record, key))
|
|
122
|
+
return undefined;
|
|
123
|
+
return readLiteral(draftIn, record, key, path);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The locale a text is written in, narrowed to a canonical tag.
|
|
127
|
+
*
|
|
128
|
+
* A catalogue key is compared as an exact string against `locales`, so `PT-br` and `pt-BR` are two
|
|
129
|
+
* different keys and one of them can never be selected. The refusal names the canonical form when
|
|
130
|
+
* the value is a tag at all, and the tag's shape when it is not — the same two repairs
|
|
131
|
+
* `createEmptySchema` states.
|
|
132
|
+
*/
|
|
133
|
+
export function readLocale(draftIn, record, key, path) {
|
|
134
|
+
return narrowLocale(draftIn, record[key], `${path}.${key}`);
|
|
135
|
+
}
|
|
136
|
+
/** One locale tag, narrowed to the canonical spelling the catalogue is keyed by. */
|
|
137
|
+
export function narrowLocale(draftIn, value, path) {
|
|
138
|
+
if (typeof value !== 'string') {
|
|
139
|
+
return fail(draftIn, path, `a locale tag is text; received ${describe(value)}`);
|
|
140
|
+
}
|
|
141
|
+
if (isCanonicalLocale(value))
|
|
142
|
+
return value;
|
|
143
|
+
const canonical = isLocaleTag(value) ? canonicalLocale(value) : undefined;
|
|
144
|
+
return fail(draftIn, path, canonical === undefined
|
|
145
|
+
? `\`${value}\` is not a locale tag; write a locale tag such as \`en\`, \`pt-BR\` or \`es-419\``
|
|
146
|
+
: `\`${value}\` is not the canonical form; write \`${canonical}\``);
|
|
147
|
+
}
|
|
148
|
+
export function readLiteral(draftIn, record, key, path) {
|
|
149
|
+
const value = readText(draftIn, record, key, path);
|
|
150
|
+
if (value === undefined)
|
|
151
|
+
return undefined;
|
|
152
|
+
if (value.trim() !== '')
|
|
153
|
+
return value;
|
|
154
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is the text a reader sees, so it is not empty`);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* One expression slot written as source text, as a declaration member or a suggestion states it.
|
|
158
|
+
*
|
|
159
|
+
* The text is stored as it was written: a half-typed expression remains draft source for the
|
|
160
|
+
* compiler to judge, so this refuses only what no expression could ever mean — an empty slot.
|
|
161
|
+
*/
|
|
162
|
+
export function readExpressionSource(draftIn, value, path, member) {
|
|
163
|
+
if (typeof value !== 'string') {
|
|
164
|
+
return fail(draftIn, path, `\`${member}\` is an Expr v1 expression written as source text; received ${describe(value)}`);
|
|
165
|
+
}
|
|
166
|
+
if (value.trim() === '') {
|
|
167
|
+
return fail(draftIn, path, `\`${member}\` is an Expr v1 expression, so it is not empty`);
|
|
168
|
+
}
|
|
169
|
+
return value;
|
|
170
|
+
}
|
|
171
|
+
export function isCanonicalInteger(value) {
|
|
172
|
+
return typeof value === 'number' && isWireValue(value);
|
|
173
|
+
}
|
|
174
|
+
export function readInteger(draftIn, record, key, path) {
|
|
175
|
+
const value = record[key];
|
|
176
|
+
if (isCanonicalInteger(value))
|
|
177
|
+
return value;
|
|
178
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is a whole number; received ${describe(value)}`);
|
|
179
|
+
}
|
|
180
|
+
export function readOptionalInteger(draftIn, record, key, path) {
|
|
181
|
+
if (!Object.hasOwn(record, key))
|
|
182
|
+
return undefined;
|
|
183
|
+
return readInteger(draftIn, record, key, path);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* A value bound: a canonical JSON number, or the text a `date`/`instant` bound states.
|
|
187
|
+
*
|
|
188
|
+
* The member's domain is the field's, which the compiler decides; the command's job is only to
|
|
189
|
+
* keep a number and a date string from being confused, so it accepts the two shapes the
|
|
190
|
+
* metaschema's `oneOf` admits and lets `SCHEMA_INVALID` name a bound the field cannot hold.
|
|
191
|
+
*/
|
|
192
|
+
export function readBound(draftIn, record, key, path) {
|
|
193
|
+
const value = record[key];
|
|
194
|
+
if (isCanonicalInteger(value))
|
|
195
|
+
return value;
|
|
196
|
+
if (typeof value === 'string' && value.trim() !== '')
|
|
197
|
+
return value;
|
|
198
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is a whole number or a canonical date/instant text; received ${describe(value)}`);
|
|
199
|
+
}
|
|
200
|
+
export function readBoolean(draftIn, record, key, path) {
|
|
201
|
+
const value = record[key];
|
|
202
|
+
if (typeof value === 'boolean')
|
|
203
|
+
return value;
|
|
204
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is \`true\` or \`false\`; received ${describe(value)}`);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* The one unit a `duration` declaration may name, `MODEL-2.1.24`.
|
|
208
|
+
*
|
|
209
|
+
* The unit is optional on the declaration — an absent one means `minutes` — but a stated one is
|
|
210
|
+
* one of the three the metaschema enumerates, because v1 defines no conversion and the unit is
|
|
211
|
+
* part of the value rather than a display choice.
|
|
212
|
+
*/
|
|
213
|
+
export function readOptionalDurationUnit(draftIn, record, key, path) {
|
|
214
|
+
if (!Object.hasOwn(record, key))
|
|
215
|
+
return undefined;
|
|
216
|
+
const value = readText(draftIn, record, key, path);
|
|
217
|
+
if (value === undefined)
|
|
218
|
+
return undefined;
|
|
219
|
+
if (value === 'minutes' || value === 'hours' || value === 'seconds')
|
|
220
|
+
return value;
|
|
221
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is \`${value}\`; a duration unit is \`minutes\`, \`hours\` or \`seconds\``);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* A `quantity` declaration's unit domain, `MODEL-2.1.21`.
|
|
225
|
+
*
|
|
226
|
+
* A unit code is identifier-like and a domain is a set: the list names at least one unit, each
|
|
227
|
+
* entry is a lower-case unit code, and no code appears twice. An absent member is `undefined`
|
|
228
|
+
* here; whether the declaration requires one is the metaschema's `then.required`, which the
|
|
229
|
+
* candidate gate reports for a quantity field that states none.
|
|
230
|
+
*/
|
|
231
|
+
export function readUnitList(draftIn, value, path, what) {
|
|
232
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
233
|
+
return fail(draftIn, path, `${what} states at least one unit code`);
|
|
234
|
+
}
|
|
235
|
+
const narrowed = [];
|
|
236
|
+
for (const [index, entry] of value.entries()) {
|
|
237
|
+
if (typeof entry !== 'string' || !UNIT_CODE_PATTERN.test(entry)) {
|
|
238
|
+
return fail(draftIn, `${path}[${String(index)}]`, `${what} holds lower-case unit codes such as \`sqm\`, \`each\` or \`m3\`; received ${describe(entry)}`);
|
|
239
|
+
}
|
|
240
|
+
if (narrowed.includes(entry)) {
|
|
241
|
+
return fail(draftIn, `${path}[${String(index)}]`, `${what} names \`${entry}\` twice`);
|
|
242
|
+
}
|
|
243
|
+
narrowed.push(entry);
|
|
244
|
+
}
|
|
245
|
+
return narrowed;
|
|
246
|
+
}
|
|
247
|
+
/** A non-empty list of non-empty text, for the media types an `accept` list states. */
|
|
248
|
+
export function readTextList(draftIn, value, path, what) {
|
|
249
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
250
|
+
return fail(draftIn, path, `${what} states at least one entry`);
|
|
251
|
+
}
|
|
252
|
+
const narrowed = [];
|
|
253
|
+
for (const [index, entry] of value.entries()) {
|
|
254
|
+
if (typeof entry !== 'string' || entry === '') {
|
|
255
|
+
return fail(draftIn, `${path}[${String(index)}]`, `${what} holds non-empty text; received ${describe(entry)}`);
|
|
256
|
+
}
|
|
257
|
+
narrowed.push(entry);
|
|
258
|
+
}
|
|
259
|
+
return narrowed;
|
|
260
|
+
}
|
|
261
|
+
/** A positive whole-number member, as a byte bound states it, `MODEL-2.1.20`. */
|
|
262
|
+
export function readPositiveInteger(draftIn, record, key, path) {
|
|
263
|
+
const value = readInteger(draftIn, record, key, path);
|
|
264
|
+
if (value === undefined)
|
|
265
|
+
return undefined;
|
|
266
|
+
if (value > 0)
|
|
267
|
+
return value;
|
|
268
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` counts at least one; received ${String(value)}`);
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* The `control` a field of this shape may state, narrowed to the matrix's own vocabulary.
|
|
272
|
+
*
|
|
273
|
+
* The pairing is `CONTROLS_BY_PRIMITIVE`'s answer, not a second table: a control the matrix does
|
|
274
|
+
* not pair with this primitive is refused here, because a hint a renderer ignores is worse than
|
|
275
|
+
* no hint at all.
|
|
276
|
+
*/
|
|
277
|
+
export function readControl(draftIn, record, primitive, items, path) {
|
|
278
|
+
const value = readOptionalText(draftIn, record, 'control', path);
|
|
279
|
+
if (value === undefined)
|
|
280
|
+
return undefined;
|
|
281
|
+
const admitted = fieldsControls(primitive, items);
|
|
282
|
+
const matched = admitted.find((control) => control === value);
|
|
283
|
+
if (matched !== undefined)
|
|
284
|
+
return matched;
|
|
285
|
+
return fail(draftIn, `${path}.control`, admitted.length === 0
|
|
286
|
+
? `a \`${primitive}\` field admits no control`
|
|
287
|
+
: `\`${value}\` is not a \`${primitive}\` control; the matrix pairs ${admitted.join(', ')}`);
|
|
288
|
+
}
|
|
289
|
+
export function readChoice(draftIn, record, key, path, choices) {
|
|
290
|
+
const value = readText(draftIn, record, key, path);
|
|
291
|
+
if (value === undefined)
|
|
292
|
+
return undefined;
|
|
293
|
+
const matched = choices.find((choice) => choice === value);
|
|
294
|
+
if (matched !== undefined)
|
|
295
|
+
return matched;
|
|
296
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is \`${value}\`; the vocabulary is ${choices.join(', ')}`);
|
|
297
|
+
}
|
|
298
|
+
export function readOptionalChoice(draftIn, record, key, path, choices) {
|
|
299
|
+
if (!Object.hasOwn(record, key))
|
|
300
|
+
return undefined;
|
|
301
|
+
return readChoice(draftIn, record, key, path, choices);
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* A member an edit leaves alone (`undefined`), clears (`null`) or sets.
|
|
305
|
+
*
|
|
306
|
+
* The reader is passed rather than the key's vocabulary because each member has its own reader:
|
|
307
|
+
* a control is the matrix's choice, a message key is text, and the annotations are a block. The
|
|
308
|
+
* three-state member is what makes one command express a change, a clearing and no opinion.
|
|
309
|
+
*/
|
|
310
|
+
export function readEditable(record, key, read) {
|
|
311
|
+
if (!Object.hasOwn(record, key))
|
|
312
|
+
return undefined;
|
|
313
|
+
if (record[key] === null)
|
|
314
|
+
return null;
|
|
315
|
+
return read();
|
|
316
|
+
}
|
|
317
|
+
export function readPath(draftIn, record, key, path) {
|
|
318
|
+
if (!Object.hasOwn(record, key))
|
|
319
|
+
return undefined;
|
|
320
|
+
const value = record[key];
|
|
321
|
+
if (!Array.isArray(value)) {
|
|
322
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is a list of child indexes`);
|
|
323
|
+
}
|
|
324
|
+
const steps = [];
|
|
325
|
+
for (const step of value) {
|
|
326
|
+
if (typeof step !== 'number' || !Number.isSafeInteger(step) || step < 0) {
|
|
327
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` holds non-negative whole child indexes; received ${describe(step)}`);
|
|
328
|
+
}
|
|
329
|
+
steps.push(step);
|
|
330
|
+
}
|
|
331
|
+
return steps;
|
|
332
|
+
}
|
|
333
|
+
/** A path that names one node inside `layout`; the root is the list, not a node. */
|
|
334
|
+
export function readNodePath(draftIn, record, key, path) {
|
|
335
|
+
const read = readPath(draftIn, record, key, path);
|
|
336
|
+
if (read === undefined) {
|
|
337
|
+
if (draftIn.refusal === undefined) {
|
|
338
|
+
fail(draftIn, `${path}.${key}`, `\`${key}\` is a list of child indexes naming one layout node`);
|
|
339
|
+
}
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
if (read.length === 0) {
|
|
343
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` names a node inside \`layout\`; pass at least one index, as \`[0]\` for the first node`);
|
|
344
|
+
}
|
|
345
|
+
return read;
|
|
346
|
+
}
|
|
347
|
+
export function readAnnotations(draftIn, record, path) {
|
|
348
|
+
if (!Object.hasOwn(record, 'annotations'))
|
|
349
|
+
return undefined;
|
|
350
|
+
const value = record['annotations'];
|
|
351
|
+
const annotations = readRecord(draftIn, value, `${path}.annotations`, '`annotations`');
|
|
352
|
+
if (annotations === undefined)
|
|
353
|
+
return undefined;
|
|
354
|
+
const narrowed = [];
|
|
355
|
+
for (const [name, member] of Object.entries(annotations)) {
|
|
356
|
+
if (typeof member !== 'boolean' && typeof member !== 'string') {
|
|
357
|
+
return fail(draftIn, `${path}.annotations.${name}`, `annotation \`${name}\` is a boolean or a code; received ${describe(member)}`);
|
|
358
|
+
}
|
|
359
|
+
narrowed.push([name, member]);
|
|
360
|
+
}
|
|
361
|
+
// Define own properties rather than assigning them: `__proto__` must reach the closed validator.
|
|
362
|
+
return Object.fromEntries(narrowed);
|
|
363
|
+
}
|
|
364
|
+
export function readRowKey(draftIn, record, path) {
|
|
365
|
+
if (!Object.hasOwn(record, 'rowKey'))
|
|
366
|
+
return undefined;
|
|
367
|
+
const value = record['rowKey'];
|
|
368
|
+
const rowKey = readRecord(draftIn, value, `${path}.rowKey`, '`rowKey`');
|
|
369
|
+
if (rowKey === undefined)
|
|
370
|
+
return undefined;
|
|
371
|
+
onlyMembers(draftIn, rowKey, ['mode', 'fieldId'], `${path}.rowKey`);
|
|
372
|
+
const mode = readChoice(draftIn, rowKey, 'mode', `${path}.rowKey`, [
|
|
373
|
+
'natural',
|
|
374
|
+
'assigned',
|
|
375
|
+
]);
|
|
376
|
+
const fieldId = readOptionalText(draftIn, rowKey, 'fieldId', `${path}.rowKey`);
|
|
377
|
+
if (mode === 'natural' && fieldId === undefined) {
|
|
378
|
+
fail(draftIn, `${path}.rowKey`, 'a `natural` row key names the member that identifies a row in `fieldId`');
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
if (mode === 'assigned' && fieldId !== undefined) {
|
|
382
|
+
fail(draftIn, `${path}.rowKey`, 'an `assigned` row key is minted by the host, so it declares no `fieldId`');
|
|
383
|
+
return undefined;
|
|
384
|
+
}
|
|
385
|
+
if (mode === undefined)
|
|
386
|
+
return undefined;
|
|
387
|
+
return fieldId === undefined ? { mode } : { mode, fieldId };
|
|
388
|
+
}
|
|
389
|
+
/** One option code, as every option-bearing command states it. */
|
|
390
|
+
export function readOptionCode(draftIn, value, path) {
|
|
391
|
+
if (typeof value === 'string' && value !== '')
|
|
392
|
+
return value;
|
|
393
|
+
if (isCanonicalInteger(value))
|
|
394
|
+
return value;
|
|
395
|
+
return fail(draftIn, path, `an option \`code\` is non-empty text or a whole number; received ${describe(value)}`);
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* One rule expression, as the editor holds it.
|
|
399
|
+
*
|
|
400
|
+
* The visual arm is narrowed by the vocabulary's own reader, so a value that is not one of its
|
|
401
|
+
* forms is refused at the bound the reader names rather than reaching the document. The source arm
|
|
402
|
+
* is text and stays text: an unfinished expression is a draft state the store accepts.
|
|
403
|
+
*/
|
|
404
|
+
export function readRuleExpression(draftIn, value, path) {
|
|
405
|
+
const expression = readRecord(draftIn, value, path, 'an expression');
|
|
406
|
+
if (expression === undefined)
|
|
407
|
+
return undefined;
|
|
408
|
+
const mode = readText(draftIn, expression, 'mode', path);
|
|
409
|
+
if (mode === undefined)
|
|
410
|
+
return undefined;
|
|
411
|
+
if (mode === 'advanced') {
|
|
412
|
+
onlyMembers(draftIn, expression, ['mode', 'source'], path);
|
|
413
|
+
const source = readLiteral(draftIn, expression, 'source', path);
|
|
414
|
+
return source === undefined ? undefined : { mode, source };
|
|
415
|
+
}
|
|
416
|
+
if (mode !== 'visual') {
|
|
417
|
+
return fail(draftIn, `${path}.mode`, '`mode` is `visual` or `advanced`');
|
|
418
|
+
}
|
|
419
|
+
onlyMembers(draftIn, expression, ['mode', 'visual'], path);
|
|
420
|
+
const parsed = parseVisual(expression['visual'], `${path}.visual`);
|
|
421
|
+
if (!parsed.ok)
|
|
422
|
+
return fail(draftIn, parsed.path, parsed.detail);
|
|
423
|
+
return { mode, visual: parsed.visual };
|
|
424
|
+
}
|
|
425
|
+
/** One optional rule expression: absent leaves the member alone rather than refusing it. */
|
|
426
|
+
export function readOptionalRuleExpression(draftIn, record, key, path) {
|
|
427
|
+
if (!Object.hasOwn(record, key))
|
|
428
|
+
return undefined;
|
|
429
|
+
return readRuleExpression(draftIn, record[key], `${path}.${key}`);
|
|
430
|
+
}
|
|
431
|
+
/** The `params` block of a rule, gate item, screen reason or option availability. */
|
|
432
|
+
export function readParams(draftIn, record, path) {
|
|
433
|
+
if (!Object.hasOwn(record, 'params'))
|
|
434
|
+
return undefined;
|
|
435
|
+
const value = record['params'];
|
|
436
|
+
const params = readRecord(draftIn, value, `${path}.params`, '`params`');
|
|
437
|
+
if (params === undefined)
|
|
438
|
+
return undefined;
|
|
439
|
+
const narrowed = [];
|
|
440
|
+
for (const [name, source] of Object.entries(params)) {
|
|
441
|
+
if (typeof source !== 'string' || source.trim() === '') {
|
|
442
|
+
return fail(draftIn, `${path}.params.${name}`, `parameter \`${name}\` is an expression; received ${describe(source)}`);
|
|
443
|
+
}
|
|
444
|
+
narrowed.push([name, source]);
|
|
445
|
+
}
|
|
446
|
+
return Object.fromEntries(narrowed);
|
|
447
|
+
}
|
|
448
|
+
/** One canonical wire value. */
|
|
449
|
+
export function readWireValue(draftIn, value, path) {
|
|
450
|
+
if (isWireValue(value))
|
|
451
|
+
return value;
|
|
452
|
+
return fail(draftIn, path, 'a value is a canonical wire value: a string, a whole number, a boolean, `null`, money, a timeslot, a list or a record of those');
|
|
453
|
+
}
|
|
454
|
+
/** A map keyed by an identifier, holding canonical wire values. */
|
|
455
|
+
export function readWireRecord(draftIn, value, path, what) {
|
|
456
|
+
const record = readRecord(draftIn, value, path, what);
|
|
457
|
+
if (record === undefined)
|
|
458
|
+
return undefined;
|
|
459
|
+
const narrowed = [];
|
|
460
|
+
for (const [key, member] of Object.entries(record)) {
|
|
461
|
+
const read = readWireValue(draftIn, member, `${path}.${key}`);
|
|
462
|
+
if (read === undefined)
|
|
463
|
+
return undefined;
|
|
464
|
+
narrowed.push([key, read]);
|
|
465
|
+
}
|
|
466
|
+
return Object.fromEntries(narrowed);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* One row key: a non-empty string or a safe whole number.
|
|
470
|
+
*
|
|
471
|
+
* A key is the record's own data, so its spelling is the record's: a text key is kept exactly as it
|
|
472
|
+
* was written, and `1` and `"1"` stay two rows. The reserved spelling `current` is a string like any
|
|
473
|
+
* other here; the engine resolves it as the row an item-scoped rule judged, which is the one place a
|
|
474
|
+
* suggestion's row address is read.
|
|
475
|
+
*/
|
|
476
|
+
export function readRowKeyValue(draftIn, value, path) {
|
|
477
|
+
if (typeof value === 'string' && value !== '')
|
|
478
|
+
return value;
|
|
479
|
+
if (isCanonicalInteger(value))
|
|
480
|
+
return value;
|
|
481
|
+
return fail(draftIn, path, '`rowKey` is a non-empty string or a safe whole number');
|
|
482
|
+
}
|
|
483
|
+
/** One field reference, `schema.v1.json` `fieldRef`. */
|
|
484
|
+
function readFieldRef(draftIn, value, path) {
|
|
485
|
+
const record = readRecord(draftIn, value, path, 'a field reference');
|
|
486
|
+
if (record === undefined)
|
|
487
|
+
return undefined;
|
|
488
|
+
onlyMembers(draftIn, record, ['fieldId', 'rowKey'], path);
|
|
489
|
+
const fieldId = readIdentifier(draftIn, record, 'fieldId', path);
|
|
490
|
+
const rowKey = record['rowKey'];
|
|
491
|
+
const key = rowKey === undefined ? undefined : readRowKeyValue(draftIn, rowKey, `${path}.rowKey`);
|
|
492
|
+
if (fieldId === undefined || draftIn.refusal !== undefined)
|
|
493
|
+
return undefined;
|
|
494
|
+
return key === undefined ? { fieldId } : { fieldId, rowKey: key };
|
|
495
|
+
}
|
|
496
|
+
/** A list of field references, or `undefined` when the value is not one. */
|
|
497
|
+
export function readFieldRefs(draftIn, value, path, what) {
|
|
498
|
+
if (!Array.isArray(value)) {
|
|
499
|
+
return fail(draftIn, path, `${what} is a list of field references`);
|
|
500
|
+
}
|
|
501
|
+
const narrowed = [];
|
|
502
|
+
for (const [index, entry] of value.entries()) {
|
|
503
|
+
const read = readFieldRef(draftIn, entry, `${path}[${String(index)}]`);
|
|
504
|
+
if (read === undefined)
|
|
505
|
+
return undefined;
|
|
506
|
+
narrowed.push(read);
|
|
507
|
+
}
|
|
508
|
+
return narrowed;
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* The access overlay a case evaluates under, read by the runtime's own overlay parser.
|
|
512
|
+
*
|
|
513
|
+
* The authored slot keeps an omitted `defaultAccess` omitted: the runtime resolves that omission
|
|
514
|
+
* as the fail-closed `read_only`, and stating it here would rewrite a stored case on a
|
|
515
|
+
* load-and-save that changed nothing.
|
|
516
|
+
*/
|
|
517
|
+
export function readAccessOverlay(draftIn, value, path) {
|
|
518
|
+
try {
|
|
519
|
+
const overlay = toAccessOverlay(value, path);
|
|
520
|
+
const stated = isRecord(value) && value['defaultAccess'] !== undefined;
|
|
521
|
+
return {
|
|
522
|
+
accessVersion: overlay.accessVersion,
|
|
523
|
+
...(stated ? { defaultAccess: overlay.defaultAccess } : {}),
|
|
524
|
+
...(overlay.allowMaskedRead === undefined
|
|
525
|
+
? {}
|
|
526
|
+
: { allowMaskedRead: overlay.allowMaskedRead }),
|
|
527
|
+
rules: overlay.rules,
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
catch (cause) {
|
|
531
|
+
if (!(cause instanceof AxiomError))
|
|
532
|
+
throw cause;
|
|
533
|
+
return fail(draftIn, cause.path === '' ? path : cause.path, cause.message);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
/** A civil date text, read by the runtime's own calendar check. */
|
|
537
|
+
export function readPlainDate(draftIn, value, path) {
|
|
538
|
+
if (typeof value !== 'string') {
|
|
539
|
+
return fail(draftIn, path, `\`recordDate\` is a civil date; received ${describe(value)}`);
|
|
540
|
+
}
|
|
541
|
+
try {
|
|
542
|
+
parsePlainDate(value, path);
|
|
543
|
+
return value;
|
|
544
|
+
}
|
|
545
|
+
catch (cause) {
|
|
546
|
+
if (!(cause instanceof AxiomError))
|
|
547
|
+
throw cause;
|
|
548
|
+
return fail(draftIn, path, cause.message);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
/** A list of non-empty capability names. */
|
|
552
|
+
export function readCapabilities(draftIn, value, path) {
|
|
553
|
+
if (!Array.isArray(value)) {
|
|
554
|
+
return fail(draftIn, path, '`capabilities` is a list of capability names');
|
|
555
|
+
}
|
|
556
|
+
const narrowed = [];
|
|
557
|
+
for (const [index, entry] of value.entries()) {
|
|
558
|
+
if (typeof entry !== 'string' || entry === '') {
|
|
559
|
+
return fail(draftIn, `${path}[${String(index)}]`, `a capability is a non-empty name; received ${describe(entry)}`);
|
|
560
|
+
}
|
|
561
|
+
narrowed.push(entry);
|
|
562
|
+
}
|
|
563
|
+
return narrowed;
|
|
564
|
+
}
|
|
565
|
+
/** One `diagnosticCode`: the upper-case problem grammar a screen reason states. */
|
|
566
|
+
export function readDiagnosticCode(draftIn, record, key, path) {
|
|
567
|
+
const value = readText(draftIn, record, key, path);
|
|
568
|
+
if (value === undefined)
|
|
569
|
+
return undefined;
|
|
570
|
+
if (/^[A-Z][A-Z0-9_]{2,63}$/u.test(value))
|
|
571
|
+
return value;
|
|
572
|
+
return fail(draftIn, `${path}.${key}`, `\`${key}\` is \`${value}\`; write an upper-case problem code, as \`AMOUNT_REVIEW\``);
|
|
573
|
+
}
|
|
574
|
+
/** One list of identifiers, duplicate-free, for `covers` and `fieldIds`. */
|
|
575
|
+
export function readIdentifierList(draftIn, value, path, what) {
|
|
576
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
577
|
+
return fail(draftIn, path, `${what} states at least one identifier`);
|
|
578
|
+
}
|
|
579
|
+
const narrowed = [];
|
|
580
|
+
for (const [index, entry] of value.entries()) {
|
|
581
|
+
if (typeof entry !== 'string' || !IDENTIFIER_PATTERN.test(entry)) {
|
|
582
|
+
return fail(draftIn, `${path}[${String(index)}]`, `${what} holds lower-case identifiers; received ${describe(entry)}`);
|
|
583
|
+
}
|
|
584
|
+
if (narrowed.includes(entry)) {
|
|
585
|
+
return fail(draftIn, `${path}[${String(index)}]`, `${what} names \`${entry}\` twice`);
|
|
586
|
+
}
|
|
587
|
+
narrowed.push(entry);
|
|
588
|
+
}
|
|
589
|
+
return narrowed;
|
|
590
|
+
}
|
|
591
|
+
/** One `itemDeclaration`: a scalar or attachment array's element domain. */
|
|
592
|
+
/**
|
|
593
|
+
* The four members that narrow a declaration's value domain, read from a record.
|
|
594
|
+
*
|
|
595
|
+
* A `money`, `quantity` or `duration` declaration states its currency, its scale, its unit domain
|
|
596
|
+
* or its unit; every other primitive states none of them, and the metaschema says so through an
|
|
597
|
+
* `if`/`then` per primitive. Field items and context roots spell the domain the same way, so they
|
|
598
|
+
* read it through this one function and cannot disagree about which member belongs to which
|
|
599
|
+
* primitive.
|
|
600
|
+
*/
|
|
601
|
+
export function readDeclarationDomain(draftIn, record, path, primitive) {
|
|
602
|
+
const currency = readOptionalText(draftIn, record, 'currency', path);
|
|
603
|
+
const scale = readOptionalInteger(draftIn, record, 'scale', path);
|
|
604
|
+
const units = Object.hasOwn(record, 'units')
|
|
605
|
+
? readIdentifierList(draftIn, record['units'], `${path}.units`, '`units`')
|
|
606
|
+
: undefined;
|
|
607
|
+
const unitText = readOptionalText(draftIn, record, 'unit', path);
|
|
608
|
+
const unit = unitText === undefined
|
|
609
|
+
? undefined
|
|
610
|
+
: unitText === 'minutes' || unitText === 'hours' || unitText === 'seconds'
|
|
611
|
+
? unitText
|
|
612
|
+
: fail(draftIn, `${path}.unit`, 'a duration unit is `minutes`, `hours` or `seconds`');
|
|
613
|
+
if (primitive === undefined)
|
|
614
|
+
return undefined;
|
|
615
|
+
if (primitive !== 'money' && currency !== undefined) {
|
|
616
|
+
fail(draftIn, `${path}.currency`, 'only a `money` declaration states a currency');
|
|
617
|
+
}
|
|
618
|
+
// A quantity states a scale too: money carries its own scale in the value, a quantity's is the
|
|
619
|
+
// declaration's (`MODEL-2.1.21`). Every other primitive holds a value whose digits are fixed.
|
|
620
|
+
if (primitive !== 'money' && primitive !== 'quantity' && scale !== undefined) {
|
|
621
|
+
fail(draftIn, `${path}.scale`, 'only a `money` or `quantity` declaration states a scale');
|
|
622
|
+
}
|
|
623
|
+
if (primitive !== 'quantity' && units !== undefined) {
|
|
624
|
+
fail(draftIn, `${path}.units`, 'only a `quantity` declaration states its unit domain');
|
|
625
|
+
}
|
|
626
|
+
if (primitive !== 'duration' && unit !== undefined) {
|
|
627
|
+
fail(draftIn, `${path}.unit`, 'only a `duration` declaration states a unit');
|
|
628
|
+
}
|
|
629
|
+
if (primitive === 'money') {
|
|
630
|
+
if (currency === undefined) {
|
|
631
|
+
fail(draftIn, `${path}.currency`, 'a `money` declaration states its ISO 4217 currency');
|
|
632
|
+
}
|
|
633
|
+
else if (!CURRENCY_CODE_PATTERN.test(currency)) {
|
|
634
|
+
fail(draftIn, `${path}.currency`, `\`currency\` is \`${currency}\`; write three upper-case letters, as \`USD\``);
|
|
635
|
+
}
|
|
636
|
+
if (scale === undefined) {
|
|
637
|
+
fail(draftIn, `${path}.scale`, 'a `money` declaration states its `scale`, the minor-unit digits');
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
if (draftIn.refusal !== undefined)
|
|
641
|
+
return undefined;
|
|
642
|
+
return {
|
|
643
|
+
...(currency === undefined ? {} : { currency }),
|
|
644
|
+
...(scale === undefined ? {} : { scale }),
|
|
645
|
+
...(units === undefined || units.length === 0 ? {} : { units }),
|
|
646
|
+
...(unit === undefined ? {} : { unit }),
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
/** One `itemDeclaration`: the element domain of a scalar or attachment array. */
|
|
650
|
+
export function readItemDeclaration(draftIn, value, path) {
|
|
651
|
+
const record = readRecord(draftIn, value, path, 'an item declaration');
|
|
652
|
+
if (record === undefined)
|
|
653
|
+
return undefined;
|
|
654
|
+
onlyMembers(draftIn, record, ['primitive', 'currency', 'scale', 'units', 'unit'], path);
|
|
655
|
+
const primitive = readChoice(draftIn, record, 'primitive', path, ITEM_PRIMITIVES);
|
|
656
|
+
const domain = readDeclarationDomain(draftIn, record, path, primitive);
|
|
657
|
+
if (primitive === undefined || domain === undefined)
|
|
658
|
+
return undefined;
|
|
659
|
+
return { primitive, ...domain };
|
|
660
|
+
}
|
|
661
|
+
/** The rule kinds the metaschema's `rule.kind` enumerates, as this module reads them. */
|
|
662
|
+
export const RULE_KINDS = [
|
|
663
|
+
'validate',
|
|
664
|
+
'visible',
|
|
665
|
+
'required',
|
|
666
|
+
'enabled',
|
|
667
|
+
'compute',
|
|
668
|
+
'defaultCompute',
|
|
669
|
+
'gate',
|
|
670
|
+
'screen',
|
|
671
|
+
];
|
|
672
|
+
export const RULE_SCOPES = [
|
|
673
|
+
'field',
|
|
674
|
+
'record',
|
|
675
|
+
'array',
|
|
676
|
+
'item',
|
|
677
|
+
];
|
|
678
|
+
/** The primitives whose values an option domain can name, `schema.v1.json` `field` `allOf`. */
|
|
679
|
+
const OPTION_PRIMITIVES = new Set([
|
|
680
|
+
'string',
|
|
681
|
+
'integer',
|
|
682
|
+
'boolean',
|
|
683
|
+
'duration',
|
|
684
|
+
]);
|
|
685
|
+
/** Whether a field of this shape may declare an option domain at all. */
|
|
686
|
+
export function admitsOptions(primitive, items) {
|
|
687
|
+
if (primitive === undefined)
|
|
688
|
+
return false;
|
|
689
|
+
if (OPTION_PRIMITIVES.has(primitive))
|
|
690
|
+
return true;
|
|
691
|
+
return (primitive === 'array' &&
|
|
692
|
+
items !== undefined &&
|
|
693
|
+
isItemPrimitive(items) &&
|
|
694
|
+
OPTION_ARRAY_ITEMS.has(items));
|
|
695
|
+
}
|
|
696
|
+
export const RULE_SEVERITIES = ['block', 'warn', 'info'];
|
|
697
|
+
export const EVALUATE_ON = ['client', 'server'];
|
|
698
|
+
export const ON_HIDE_POLICIES = ['preserve', 'exclude'];
|
|
699
|
+
export const GATE_POLICIES = ['defer', 'warn', 'block'];
|
|
700
|
+
/** The item primitives a scalar or attachment array states, from the runtime registry. */
|
|
701
|
+
export const ITEM_PRIMITIVES = RUNTIME_ITEM_PRIMITIVES;
|
|
702
|
+
/**
|
|
703
|
+
* The deepest a container member tree may nest.
|
|
704
|
+
*
|
|
705
|
+
* A record inside a record is legal in the format, and a builder authors a handful of levels at
|
|
706
|
+
* most. The bound is stated because the parse is recursive: without it a stored draft nested
|
|
707
|
+
* thousands deep overflows the stack instead of being refused, and a refusal is the whole
|
|
708
|
+
* contract of this door.
|
|
709
|
+
*/
|
|
710
|
+
export const MAX_MEMBER_NESTING = 16;
|
|
711
|
+
//# sourceMappingURL=readers.js.map
|