@composable-os/sdk 0.0.0-stage → 0.1.0-beta.8
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 +109 -0
- package/dist/automation-check.d.ts +38 -0
- package/dist/automation-check.js +454 -0
- package/dist/automation.d.ts +767 -0
- package/dist/automation.js +303 -0
- package/dist/behaviour.d.ts +3 -0
- package/dist/behaviour.js +153 -0
- package/dist/capacity.d.ts +51 -0
- package/dist/capacity.js +68 -0
- package/dist/contract.d.ts +706 -0
- package/dist/contract.js +42 -0
- package/dist/document.d.ts +165 -0
- package/dist/document.js +636 -0
- package/dist/field-types.d.ts +11 -0
- package/dist/field-types.js +93 -0
- package/dist/formula/ast.d.ts +67 -0
- package/dist/formula/ast.js +22 -0
- package/dist/formula/compile.d.ts +37 -0
- package/dist/formula/compile.js +379 -0
- package/dist/formula/index.d.ts +3 -0
- package/dist/formula/index.js +4 -0
- package/dist/formula/parser.d.ts +13 -0
- package/dist/formula/parser.js +365 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +21 -0
- package/dist/manifest.d.ts +1131 -0
- package/dist/manifest.js +1229 -0
- package/dist/media.d.ts +9 -0
- package/dist/media.js +43 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +44 -0
- package/dist/renamed.d.ts +16 -0
- package/dist/renamed.js +59 -0
- package/dist/rich-text-fixture.d.ts +121 -0
- package/dist/rich-text-fixture.js +67 -0
- package/dist/rich-text.d.ts +34 -0
- package/dist/rich-text.js +138 -0
- package/dist/runtime.d.ts +95 -0
- package/dist/runtime.js +312 -0
- package/dist/sellable.d.ts +103 -0
- package/dist/sellable.js +258 -0
- package/dist/slug.d.ts +11 -0
- package/dist/slug.js +23 -0
- package/dist/states.d.ts +64 -0
- package/dist/states.js +101 -0
- package/dist/testing.d.ts +151 -0
- package/dist/testing.js +670 -0
- package/package.json +34 -4
- package/src/automation-check.ts +534 -0
- package/src/automation.ts +743 -0
- package/src/behaviour.ts +215 -0
- package/src/capacity.ts +112 -0
- package/src/contract.ts +766 -0
- package/src/document.ts +788 -0
- package/src/field-types.ts +106 -0
- package/src/formula/ast.ts +72 -0
- package/src/formula/compile.ts +454 -0
- package/src/formula/index.ts +4 -0
- package/src/formula/parser.ts +417 -0
- package/src/index.ts +33 -0
- package/src/manifest.ts +1484 -0
- package/src/media.ts +46 -0
- package/src/permissions.ts +60 -0
- package/src/renamed.ts +63 -0
- package/src/rich-text-fixture.ts +68 -0
- package/src/rich-text.ts +173 -0
- package/src/runtime.ts +410 -0
- package/src/sellable.ts +396 -0
- package/src/slug.ts +25 -0
- package/src/states.ts +162 -0
- package/src/testing.ts +836 -0
- package/README.md +0 -3
package/dist/document.js
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
// The structured document field (stage 41, ADR 0445): a field whose value is one JSON document,
|
|
2
|
+
// checked against a schema the object declares. A program's layout is one; stage 28's `richText`
|
|
3
|
+
// is one fixed schema of this kind.
|
|
4
|
+
//
|
|
5
|
+
// layout: {
|
|
6
|
+
// type: "document",
|
|
7
|
+
// label: "Layout",
|
|
8
|
+
// schema: { root: { type: "object", fields: { weeks: { type: "array", max: 52, of: ... } } } },
|
|
9
|
+
// maxBytes: 262_144,
|
|
10
|
+
// paths: { weekCount: { label: "Weeks", path: "weeks", count: true } },
|
|
11
|
+
// }
|
|
12
|
+
//
|
|
13
|
+
// The schema language is plain data, so a manifest carries it and the host, the check and the
|
|
14
|
+
// test host read the same declaration. It is small on purpose: objects with named fields (every
|
|
15
|
+
// key known, nothing else accepted), lists with a maximum, text with a maximum, numbers with
|
|
16
|
+
// bounds, true or false, one of a fixed set of words, the id of a record of a named object, a
|
|
17
|
+
// tagged union of objects, and named definitions a schema may use again (for nesting, as rich
|
|
18
|
+
// text's lists do). There is no free pattern: a text node may name the characters it allows.
|
|
19
|
+
//
|
|
20
|
+
// Validation never repairs a value. It returns every fault (up to a limit) with the path of the
|
|
21
|
+
// fault, as a list of keys and indexes from the document's root.
|
|
22
|
+
//
|
|
23
|
+
// An object node may be `private`: removed from what a customer or the public reads
|
|
24
|
+
// (`redactDocument`), unless its `unless` key is true (a coach's note marked as shown to
|
|
25
|
+
// members). The host does that at the visibility seam; staff read the whole document.
|
|
26
|
+
/** The most a document may be, in bytes of its JSON, whatever a field declares. */
|
|
27
|
+
export const DOCUMENT_MAX_BYTES = 524_288;
|
|
28
|
+
/** A field that declares no `maxBytes`. */
|
|
29
|
+
export const DOCUMENT_DEFAULT_BYTES = 65_536;
|
|
30
|
+
/** The deepest a value may nest (objects and lists), and a schema. */
|
|
31
|
+
export const DOCUMENT_MAX_DEPTH = 24;
|
|
32
|
+
/** The most entries any one list may declare. */
|
|
33
|
+
export const DOCUMENT_MAX_LIST = 1000;
|
|
34
|
+
/** The longest a text node may declare, and the default when it declares none. */
|
|
35
|
+
export const DOCUMENT_MAX_TEXT = 20_000;
|
|
36
|
+
export const DOCUMENT_DEFAULT_TEXT = 1000;
|
|
37
|
+
/** Faults reported for one value, at most. */
|
|
38
|
+
export const DOCUMENT_MAX_ISSUES = 20;
|
|
39
|
+
/** Declared paths (view columns) per field, at most. */
|
|
40
|
+
export const DOCUMENT_MAX_PATHS = 12;
|
|
41
|
+
const ID_RE = /^[a-z][a-z0-9]{0,9}_[A-Za-z0-9]{1,60}$/;
|
|
42
|
+
const KEY_RE = /^[a-z][a-zA-Z0-9]{0,39}$/;
|
|
43
|
+
const OBJECT_RE = /^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)?$/;
|
|
44
|
+
// Control characters other than tab and newline (and a lone carriage return) are refused.
|
|
45
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what it finds.
|
|
46
|
+
const CONTROL_RE = /[\u0000-\u0008\u000b-\u001f\u007f]/;
|
|
47
|
+
// Markup a text node with `noMarkup` refuses (ADR 0440): the start of a tag, closing tag, comment
|
|
48
|
+
// or processing instruction, and any character reference, named or numeric.
|
|
49
|
+
const MARKUP_RE = /<[A-Za-z!/?]|&(?:#[0-9]+|#[xX][0-9A-Fa-f]+|[A-Za-z][A-Za-z0-9]*);/;
|
|
50
|
+
// What a link may be. No quote, angle bracket, backslash, backtick or space anywhere.
|
|
51
|
+
const URL_TAIL = "[^\\s<>\"'`\\\\]*";
|
|
52
|
+
const WEB_RE = new RegExp(`^https?://[A-Za-z0-9](?:[A-Za-z0-9.-]*[A-Za-z0-9])?(?::[0-9]{1,5})?(?:[/?#]${URL_TAIL})?$`, "i");
|
|
53
|
+
const MAILTO_RE = /^mailto:[^\s<>"'`\\:/][^\s<>"'`\\]*$/i;
|
|
54
|
+
const TEL_RE = /^tel:[+0-9 ().-]{3,40}$/i;
|
|
55
|
+
const PATH_RE = new RegExp(`^/(?![/\\\\])${URL_TAIL}$`);
|
|
56
|
+
const ANCHOR_RE = /^#[A-Za-z0-9_-]{1,100}$/;
|
|
57
|
+
/** Whether `value` is a link of the given kind (a text node's `href`). */
|
|
58
|
+
export function isDocumentHref(value, kind) {
|
|
59
|
+
if (WEB_RE.test(value))
|
|
60
|
+
return true;
|
|
61
|
+
if (kind === "web")
|
|
62
|
+
return false;
|
|
63
|
+
return (MAILTO_RE.test(value) || TEL_RE.test(value) || PATH_RE.test(value) || ANCHOR_RE.test(value));
|
|
64
|
+
}
|
|
65
|
+
/** Whether `value` holds markup a `noMarkup` text node refuses. */
|
|
66
|
+
export function hasMarkup(value) {
|
|
67
|
+
return MARKUP_RE.test(value);
|
|
68
|
+
}
|
|
69
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
70
|
+
/** Bytes of a value's JSON, as stored. */
|
|
71
|
+
export function documentBytes(value) {
|
|
72
|
+
// UTF-8 length by hand: the SDK is type-checked with no DOM or runtime library.
|
|
73
|
+
const text = JSON.stringify(value) ?? "";
|
|
74
|
+
let bytes = 0;
|
|
75
|
+
for (const char of text) {
|
|
76
|
+
const code = char.codePointAt(0) ?? 0;
|
|
77
|
+
bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
|
|
78
|
+
}
|
|
79
|
+
return bytes;
|
|
80
|
+
}
|
|
81
|
+
// --- Checking a schema ---------------------------------------------------------------------------
|
|
82
|
+
/**
|
|
83
|
+
* What is wrong with a declared schema, as `[path, message]` pairs (the path from the schema's
|
|
84
|
+
* own root: `root.fields.weeks.of`). Empty when nothing is. `knownObjects`, when given, holds the
|
|
85
|
+
* objects a `ref` may name.
|
|
86
|
+
*/
|
|
87
|
+
export function checkDocumentSchema(schema, knownObjects) {
|
|
88
|
+
const problems = [];
|
|
89
|
+
const add = (path, message) => {
|
|
90
|
+
if (problems.length < 50)
|
|
91
|
+
problems.push([path, message]);
|
|
92
|
+
};
|
|
93
|
+
if (!isObject(schema)) {
|
|
94
|
+
add("", "A document schema is an object with a `root` object node");
|
|
95
|
+
return problems;
|
|
96
|
+
}
|
|
97
|
+
for (const key of Object.keys(schema)) {
|
|
98
|
+
if (key !== "root" && key !== "defs")
|
|
99
|
+
add(key, `Unknown key "${key}" (root, defs)`);
|
|
100
|
+
}
|
|
101
|
+
const defs = isObject(schema.defs) ? schema.defs : {};
|
|
102
|
+
if (schema.defs !== undefined && !isObject(schema.defs))
|
|
103
|
+
add("defs", "defs is an object");
|
|
104
|
+
for (const name of Object.keys(defs)) {
|
|
105
|
+
if (!KEY_RE.test(name))
|
|
106
|
+
add(`defs.${name}`, "A definition's name is camelCase");
|
|
107
|
+
}
|
|
108
|
+
const node = (path, value, depth, inFields) => {
|
|
109
|
+
if (depth > DOCUMENT_MAX_DEPTH) {
|
|
110
|
+
add(path, `The schema nests deeper than ${DOCUMENT_MAX_DEPTH}`);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (!isObject(value)) {
|
|
114
|
+
add(path, "A node is an object with a `type`");
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const allowed = new Set(["type", "description", "optional"]);
|
|
118
|
+
const expect = (...keys) => {
|
|
119
|
+
for (const key of keys)
|
|
120
|
+
allowed.add(key);
|
|
121
|
+
};
|
|
122
|
+
if (value.optional !== undefined && (typeof value.optional !== "boolean" || !inFields)) {
|
|
123
|
+
add(`${path}.optional`, "optional is true or false, on a field of an object");
|
|
124
|
+
}
|
|
125
|
+
if (value.description !== undefined && typeof value.description !== "string") {
|
|
126
|
+
add(`${path}.description`, "description is text");
|
|
127
|
+
}
|
|
128
|
+
const num = (key, opts = {}) => {
|
|
129
|
+
const v = value[key];
|
|
130
|
+
if (v === undefined)
|
|
131
|
+
return undefined;
|
|
132
|
+
if (typeof v !== "number" || !Number.isFinite(v) || (opts.int && !Number.isInteger(v))) {
|
|
133
|
+
add(`${path}.${key}`, `${key} is ${opts.int ? "a whole number" : "a number"}`);
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
if (opts.min !== undefined && v < opts.min)
|
|
137
|
+
add(`${path}.${key}`, `${key} is at least ${opts.min}`);
|
|
138
|
+
if (opts.max !== undefined && v > opts.max)
|
|
139
|
+
add(`${path}.${key}`, `${key} is at most ${opts.max}`);
|
|
140
|
+
return v;
|
|
141
|
+
};
|
|
142
|
+
switch (value.type) {
|
|
143
|
+
case "object": {
|
|
144
|
+
expect("fields", "private");
|
|
145
|
+
object(path, value, depth);
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
case "array": {
|
|
149
|
+
expect("of", "max", "min", "unique");
|
|
150
|
+
const max = num("max", { int: true, min: 1, max: DOCUMENT_MAX_LIST });
|
|
151
|
+
if (value.max === undefined)
|
|
152
|
+
add(`${path}.max`, "A list declares its most entries (max)");
|
|
153
|
+
const min = num("min", { int: true, min: 0 });
|
|
154
|
+
if (min !== undefined && max !== undefined && min > max)
|
|
155
|
+
add(`${path}.min`, "min is above max");
|
|
156
|
+
node(`${path}.of`, value.of, depth + 1, false);
|
|
157
|
+
if (value.unique !== undefined) {
|
|
158
|
+
const of = resolve(value.of);
|
|
159
|
+
// The key of an object, or of every variant of a union.
|
|
160
|
+
const shapes = of?.type === "object"
|
|
161
|
+
? [of]
|
|
162
|
+
: of?.type === "union"
|
|
163
|
+
? Object.values(of.variants ?? {})
|
|
164
|
+
: [];
|
|
165
|
+
const keyNodes = shapes.map((shape) => resolve(shape.fields?.[String(value.unique)]));
|
|
166
|
+
if (typeof value.unique !== "string" ||
|
|
167
|
+
!keyNodes.length ||
|
|
168
|
+
keyNodes.some((n) => !n || !["text", "number", "choice", "ref"].includes(n.type))) {
|
|
169
|
+
add(`${path}.unique`, "unique names a text, number, choice or ref key of the listed objects");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
case "text": {
|
|
175
|
+
expect("max", "min", "multiline", "chars", "noMarkup", "href");
|
|
176
|
+
if (value.noMarkup !== undefined && typeof value.noMarkup !== "boolean") {
|
|
177
|
+
add(`${path}.noMarkup`, "noMarkup is true or false");
|
|
178
|
+
}
|
|
179
|
+
if (value.href !== undefined && value.href !== "web" && value.href !== "link") {
|
|
180
|
+
add(`${path}.href`, "href is web or link");
|
|
181
|
+
}
|
|
182
|
+
const max = num("max", { int: true, min: 1, max: DOCUMENT_MAX_TEXT });
|
|
183
|
+
const min = num("min", { int: true, min: 0 });
|
|
184
|
+
if (min !== undefined && (max ?? DOCUMENT_DEFAULT_TEXT) < min)
|
|
185
|
+
add(`${path}.min`, "min is above max");
|
|
186
|
+
if (value.multiline !== undefined && typeof value.multiline !== "boolean") {
|
|
187
|
+
add(`${path}.multiline`, "multiline is true or false");
|
|
188
|
+
}
|
|
189
|
+
if (value.chars !== undefined &&
|
|
190
|
+
(typeof value.chars !== "string" || !value.chars.length || value.chars.length > 200)) {
|
|
191
|
+
add(`${path}.chars`, "chars is the allowed characters, 1 to 200 of them");
|
|
192
|
+
}
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
case "number": {
|
|
196
|
+
expect("min", "max", "integer", "step");
|
|
197
|
+
const min = num("min");
|
|
198
|
+
const max = num("max");
|
|
199
|
+
if (min !== undefined && max !== undefined && min > max)
|
|
200
|
+
add(`${path}.min`, "min is above max");
|
|
201
|
+
if (value.integer !== undefined && typeof value.integer !== "boolean") {
|
|
202
|
+
add(`${path}.integer`, "integer is true or false");
|
|
203
|
+
}
|
|
204
|
+
const step = num("step");
|
|
205
|
+
if (step !== undefined && step <= 0)
|
|
206
|
+
add(`${path}.step`, "step is above 0");
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
case "boolean":
|
|
210
|
+
break;
|
|
211
|
+
case "choice": {
|
|
212
|
+
expect("options");
|
|
213
|
+
const options = value.options;
|
|
214
|
+
if (!Array.isArray(options) ||
|
|
215
|
+
!options.length ||
|
|
216
|
+
options.length > 100 ||
|
|
217
|
+
options.some((o) => typeof o !== "string" || !o.length || o.length > 100)) {
|
|
218
|
+
add(`${path}.options`, "A choice lists 1 to 100 words");
|
|
219
|
+
}
|
|
220
|
+
else if (new Set(options).size !== options.length) {
|
|
221
|
+
add(`${path}.options`, "A choice lists each word once");
|
|
222
|
+
}
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
case "ref": {
|
|
226
|
+
expect("to");
|
|
227
|
+
if (typeof value.to !== "string" || !OBJECT_RE.test(value.to)) {
|
|
228
|
+
add(`${path}.to`, "A ref names an object: `<extension>.<object>` or a core object");
|
|
229
|
+
}
|
|
230
|
+
else if (knownObjects && !knownObjects.has(value.to)) {
|
|
231
|
+
add(`${path}.to`, `Unknown object "${value.to}"`);
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
case "union": {
|
|
236
|
+
expect("tag", "variants");
|
|
237
|
+
if (typeof value.tag !== "string" || !KEY_RE.test(value.tag)) {
|
|
238
|
+
add(`${path}.tag`, "A union's tag is a camelCase key");
|
|
239
|
+
}
|
|
240
|
+
if (!isObject(value.variants) || !Object.keys(value.variants).length) {
|
|
241
|
+
add(`${path}.variants`, "A union has at least one variant");
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
for (const [name, variant] of Object.entries(value.variants)) {
|
|
245
|
+
const vp = `${path}.variants.${name}`;
|
|
246
|
+
if (!name.length || name.length > 100)
|
|
247
|
+
add(vp, "A variant's name is 1 to 100 characters");
|
|
248
|
+
if (!isObject(variant) || variant.type !== "object") {
|
|
249
|
+
add(vp, "A variant is an object node");
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (isObject(variant.fields) &&
|
|
253
|
+
typeof value.tag === "string" &&
|
|
254
|
+
value.tag in variant.fields) {
|
|
255
|
+
add(`${vp}.fields.${value.tag}`, "A variant does not declare its union's tag: the host does");
|
|
256
|
+
}
|
|
257
|
+
node(vp, variant, depth + 1, false);
|
|
258
|
+
}
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
case "use": {
|
|
262
|
+
expect("def");
|
|
263
|
+
if (typeof value.def !== "string" || !Object.hasOwn(defs, value.def)) {
|
|
264
|
+
add(`${path}.def`, `"${String(value.def)}" is not one of the schema's defs`);
|
|
265
|
+
}
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
default:
|
|
269
|
+
add(`${path}.type`, "type is one of object, array, text, number, boolean, choice, ref, union, use");
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
for (const key of Object.keys(value)) {
|
|
273
|
+
if (!allowed.has(key))
|
|
274
|
+
add(`${path}.${key}`, `Unknown key "${key}" on a ${String(value.type)} node`);
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
const object = (path, value, depth) => {
|
|
278
|
+
if (!isObject(value.fields)) {
|
|
279
|
+
add(`${path}.fields`, "An object node lists its fields");
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const keys = Object.keys(value.fields);
|
|
283
|
+
if (keys.length > 100)
|
|
284
|
+
add(`${path}.fields`, "An object has at most 100 fields");
|
|
285
|
+
for (const key of keys) {
|
|
286
|
+
if (!KEY_RE.test(key))
|
|
287
|
+
add(`${path}.fields.${key}`, "A field's key is camelCase");
|
|
288
|
+
node(`${path}.fields.${key}`, value.fields[key], depth + 1, true);
|
|
289
|
+
}
|
|
290
|
+
const priv = value.private;
|
|
291
|
+
if (priv !== undefined && priv !== true) {
|
|
292
|
+
const unless = isObject(priv) ? priv.unless : undefined;
|
|
293
|
+
const flag = typeof unless === "string" ? value.fields[unless] : undefined;
|
|
294
|
+
if (!isObject(flag) || flag.type !== "boolean") {
|
|
295
|
+
add(`${path}.private`, "private is true, or { unless: <a boolean key of this object> }");
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
const resolve = (value, seen = new Set()) => {
|
|
300
|
+
if (!isObject(value))
|
|
301
|
+
return undefined;
|
|
302
|
+
if (value.type !== "use")
|
|
303
|
+
return value;
|
|
304
|
+
const name = String(value.def);
|
|
305
|
+
if (seen.has(name) || !Object.hasOwn(defs, name))
|
|
306
|
+
return undefined;
|
|
307
|
+
seen.add(name);
|
|
308
|
+
return resolve(defs[name], seen);
|
|
309
|
+
};
|
|
310
|
+
if (!isObject(schema.root) || schema.root.type !== "object") {
|
|
311
|
+
add("root", "root is an object node");
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
node("root", schema.root, 0, false);
|
|
315
|
+
}
|
|
316
|
+
for (const [name, def] of Object.entries(defs))
|
|
317
|
+
node(`defs.${name}`, def, 0, false);
|
|
318
|
+
// A definition that only names itself (a: use a) would never reach a value.
|
|
319
|
+
for (const name of Object.keys(defs)) {
|
|
320
|
+
if (!resolve({ type: "use", def: name }))
|
|
321
|
+
add(`defs.${name}`, "A definition must reach a node that is not `use`");
|
|
322
|
+
}
|
|
323
|
+
return problems;
|
|
324
|
+
}
|
|
325
|
+
/** The node a declared path reaches, or why it reaches none. */
|
|
326
|
+
export function documentPathNode(schema, spec) {
|
|
327
|
+
const keys = spec.path.split(".");
|
|
328
|
+
if (!keys.length || keys.some((key) => !KEY_RE.test(key))) {
|
|
329
|
+
return "A path is camelCase keys joined by dots, from the document's root";
|
|
330
|
+
}
|
|
331
|
+
let current = schema.root;
|
|
332
|
+
for (const key of keys) {
|
|
333
|
+
const at = useOf(schema, current);
|
|
334
|
+
if (at?.type !== "object")
|
|
335
|
+
return `"${spec.path}" passes through something that is not an object`;
|
|
336
|
+
current = at.fields[key];
|
|
337
|
+
if (!current)
|
|
338
|
+
return `"${spec.path}" is not in the schema (no "${key}")`;
|
|
339
|
+
}
|
|
340
|
+
const end = useOf(schema, current);
|
|
341
|
+
if (spec.count) {
|
|
342
|
+
return end?.type === "array"
|
|
343
|
+
? { type: "number" }
|
|
344
|
+
: `"${spec.path}" is not a list, so it has no count`;
|
|
345
|
+
}
|
|
346
|
+
switch (end?.type) {
|
|
347
|
+
case "text":
|
|
348
|
+
case "choice":
|
|
349
|
+
return { type: "text" };
|
|
350
|
+
case "number":
|
|
351
|
+
return { type: "number" };
|
|
352
|
+
case "boolean":
|
|
353
|
+
return { type: "boolean" };
|
|
354
|
+
case "ref":
|
|
355
|
+
return { type: "id" };
|
|
356
|
+
default:
|
|
357
|
+
return `"${spec.path}" is not a text, number, boolean, choice or ref (use count for a list)`;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
/** The JSON path SQLite reads a declared path with: `$.meta.level`. */
|
|
361
|
+
export function documentJsonPath(path) {
|
|
362
|
+
if (!path.split(".").every((key) => KEY_RE.test(key)))
|
|
363
|
+
throw new Error(`Bad document path ${path}`);
|
|
364
|
+
return `$.${path}`;
|
|
365
|
+
}
|
|
366
|
+
function useOf(schema, node) {
|
|
367
|
+
let current = node;
|
|
368
|
+
for (let hops = 0; current?.type === "use" && hops < 20; hops += 1) {
|
|
369
|
+
current = schema.defs?.[current.def];
|
|
370
|
+
}
|
|
371
|
+
return current?.type === "use" ? undefined : current;
|
|
372
|
+
}
|
|
373
|
+
// --- Validating a value ----------------------------------------------------------------------------
|
|
374
|
+
/**
|
|
375
|
+
* Checks `value` against `schema`: every fault, with its path, or the value and its size. A value
|
|
376
|
+
* over `maxBytes` (capped at DOCUMENT_MAX_BYTES) is refused before it is walked.
|
|
377
|
+
*/
|
|
378
|
+
export function validateDocument(schema, value, options = {}) {
|
|
379
|
+
const limit = Math.min(options.maxBytes ?? DOCUMENT_DEFAULT_BYTES, DOCUMENT_MAX_BYTES);
|
|
380
|
+
let bytes;
|
|
381
|
+
try {
|
|
382
|
+
bytes = documentBytes(value);
|
|
383
|
+
}
|
|
384
|
+
catch {
|
|
385
|
+
return { ok: false, issues: [{ path: [], message: "The document is not JSON" }] };
|
|
386
|
+
}
|
|
387
|
+
if (bytes > limit) {
|
|
388
|
+
return {
|
|
389
|
+
ok: false,
|
|
390
|
+
issues: [{ path: [], message: `The document is ${bytes} bytes; at most ${limit}` }],
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
const issues = [];
|
|
394
|
+
const fault = (path, message) => {
|
|
395
|
+
if (issues.length < DOCUMENT_MAX_ISSUES)
|
|
396
|
+
issues.push({ path: [...path], message });
|
|
397
|
+
};
|
|
398
|
+
const walk = (node, v, path, depth) => {
|
|
399
|
+
if (issues.length >= DOCUMENT_MAX_ISSUES)
|
|
400
|
+
return;
|
|
401
|
+
if (depth > DOCUMENT_MAX_DEPTH) {
|
|
402
|
+
fault(path, `Nested deeper than ${DOCUMENT_MAX_DEPTH}`);
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
switch (node.type) {
|
|
406
|
+
case "use": {
|
|
407
|
+
const target = useOf(schema, node);
|
|
408
|
+
if (!target)
|
|
409
|
+
fault(path, `The schema's definition "${node.def}" is missing`);
|
|
410
|
+
else
|
|
411
|
+
walk(target, v, path, depth);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
case "object":
|
|
415
|
+
objectValue(node, v, path, depth, null);
|
|
416
|
+
return;
|
|
417
|
+
case "union": {
|
|
418
|
+
if (!isObject(v)) {
|
|
419
|
+
fault(path, `Expected an object with "${node.tag}"`);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
const tag = v[node.tag];
|
|
423
|
+
const variant = typeof tag === "string" && Object.hasOwn(node.variants, tag)
|
|
424
|
+
? node.variants[tag]
|
|
425
|
+
: undefined;
|
|
426
|
+
if (!variant) {
|
|
427
|
+
fault([...path, node.tag], `Expected one of ${Object.keys(node.variants).join(", ")}`);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
objectValue(variant, v, path, depth, node.tag);
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
case "array": {
|
|
434
|
+
if (!Array.isArray(v)) {
|
|
435
|
+
fault(path, "Expected a list");
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (v.length > node.max)
|
|
439
|
+
fault(path, `At most ${node.max} entries (this has ${v.length})`);
|
|
440
|
+
if (node.min !== undefined && v.length < node.min) {
|
|
441
|
+
fault(path, `At least ${node.min} ${node.min === 1 ? "entry" : "entries"}`);
|
|
442
|
+
}
|
|
443
|
+
const seen = new Map();
|
|
444
|
+
v.slice(0, node.max).forEach((item, i) => {
|
|
445
|
+
walk(node.of, item, [...path, i], depth + 1);
|
|
446
|
+
if (node.unique && isObject(item) && item[node.unique] !== undefined) {
|
|
447
|
+
const key = JSON.stringify(item[node.unique]);
|
|
448
|
+
const first = seen.get(key);
|
|
449
|
+
if (first !== undefined) {
|
|
450
|
+
fault([...path, i, node.unique], `${JSON.stringify(item[node.unique])} is there twice (also entry ${first})`);
|
|
451
|
+
}
|
|
452
|
+
else
|
|
453
|
+
seen.set(key, i);
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
case "text": {
|
|
459
|
+
if (typeof v !== "string") {
|
|
460
|
+
fault(path, "Expected text");
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const max = node.max ?? DOCUMENT_DEFAULT_TEXT;
|
|
464
|
+
if (v.length > max)
|
|
465
|
+
fault(path, `At most ${max} characters (this has ${v.length})`);
|
|
466
|
+
if (node.min !== undefined && v.length < node.min)
|
|
467
|
+
fault(path, `At least ${node.min} characters`);
|
|
468
|
+
if (CONTROL_RE.test(v))
|
|
469
|
+
fault(path, "Control characters are not allowed");
|
|
470
|
+
else if (!node.multiline && /[\r\n]/.test(v))
|
|
471
|
+
fault(path, "One line only");
|
|
472
|
+
if (node.chars) {
|
|
473
|
+
const allowed = new Set(node.chars);
|
|
474
|
+
const bad = [...v].find((c) => !allowed.has(c));
|
|
475
|
+
if (bad !== undefined)
|
|
476
|
+
fault(path, `"${bad}" is not allowed here (only ${node.chars})`);
|
|
477
|
+
}
|
|
478
|
+
if (node.noMarkup && MARKUP_RE.test(v)) {
|
|
479
|
+
fault(path, "Markup is not allowed: rich text is data, not HTML");
|
|
480
|
+
}
|
|
481
|
+
if (node.href && !isDocumentHref(v, node.href)) {
|
|
482
|
+
fault(path, node.href === "web"
|
|
483
|
+
? "Expected an http or https address"
|
|
484
|
+
: "Expected an http, https, mailto or tel address, a path on the site or an anchor");
|
|
485
|
+
}
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
case "number": {
|
|
489
|
+
if (typeof v !== "number" || !Number.isFinite(v)) {
|
|
490
|
+
fault(path, "Expected a number");
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (node.integer && !Number.isInteger(v))
|
|
494
|
+
fault(path, "Expected a whole number");
|
|
495
|
+
if (node.min !== undefined && v < node.min)
|
|
496
|
+
fault(path, `At least ${node.min}`);
|
|
497
|
+
if (node.max !== undefined && v > node.max)
|
|
498
|
+
fault(path, `At most ${node.max}`);
|
|
499
|
+
if (node.step !== undefined) {
|
|
500
|
+
const steps = (v - (node.min ?? 0)) / node.step;
|
|
501
|
+
if (Math.abs(steps - Math.round(steps)) > 1e-9)
|
|
502
|
+
fault(path, `In steps of ${node.step}`);
|
|
503
|
+
}
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
case "boolean":
|
|
507
|
+
if (typeof v !== "boolean")
|
|
508
|
+
fault(path, "Expected true or false");
|
|
509
|
+
return;
|
|
510
|
+
case "choice":
|
|
511
|
+
if (typeof v !== "string" || !node.options.includes(v)) {
|
|
512
|
+
fault(path, `Expected one of ${node.options.join(", ")}`);
|
|
513
|
+
}
|
|
514
|
+
return;
|
|
515
|
+
case "ref":
|
|
516
|
+
if (typeof v !== "string" || !ID_RE.test(v))
|
|
517
|
+
fault(path, `Expected the id of a ${node.to}`);
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
const objectValue = (node, v, path, depth, tag) => {
|
|
522
|
+
if (!isObject(v)) {
|
|
523
|
+
fault(path, "Expected an object");
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
for (const key of Object.keys(v)) {
|
|
527
|
+
if (key === tag)
|
|
528
|
+
continue;
|
|
529
|
+
if (!Object.hasOwn(node.fields, key)) {
|
|
530
|
+
fault([...path, key], `Unknown key "${key}"`);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
for (const [key, child] of Object.entries(node.fields)) {
|
|
534
|
+
if (!Object.hasOwn(v, key) || v[key] === undefined) {
|
|
535
|
+
if (!child.optional)
|
|
536
|
+
fault([...path, key], "Required");
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
walk(child, v[key], [...path, key], depth + 1);
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
walk(schema.root, value, [], 0);
|
|
543
|
+
return issues.length ? { ok: false, issues } : { ok: true, value, bytes };
|
|
544
|
+
}
|
|
545
|
+
/** "weeks.0.sessions.2: Required", for messages. */
|
|
546
|
+
export function formatDocumentIssue(issue) {
|
|
547
|
+
return issue.path.length ? `${issue.path.join(".")}: ${issue.message}` : issue.message;
|
|
548
|
+
}
|
|
549
|
+
// --- Reading a value -----------------------------------------------------------------------------
|
|
550
|
+
/** Every `ref` in a valid value: the object it names, the id, and where it is. */
|
|
551
|
+
export function documentRefs(schema, value) {
|
|
552
|
+
const refs = [];
|
|
553
|
+
visit(schema, value, (node, v, path) => {
|
|
554
|
+
if (node.type === "ref" && typeof v === "string")
|
|
555
|
+
refs.push({ to: node.to, id: v, path });
|
|
556
|
+
});
|
|
557
|
+
return refs;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* The document as someone outside the org may read it: every `private` object removed (from a
|
|
561
|
+
* list, its entry; from an object, its key), unless its `unless` key is true. A value that does
|
|
562
|
+
* not fit the schema is returned as null: an outsider is never shown what was not checked.
|
|
563
|
+
*/
|
|
564
|
+
export function redactDocument(schema, value) {
|
|
565
|
+
if (value === null || value === undefined)
|
|
566
|
+
return value ?? null;
|
|
567
|
+
const strip = (node, v, depth) => {
|
|
568
|
+
const at = useOf(schema, node);
|
|
569
|
+
if (!at || depth > DOCUMENT_MAX_DEPTH)
|
|
570
|
+
return { keep: false, value: null };
|
|
571
|
+
switch (at.type) {
|
|
572
|
+
case "object":
|
|
573
|
+
case "union": {
|
|
574
|
+
if (!isObject(v))
|
|
575
|
+
return { keep: false, value: null };
|
|
576
|
+
const shape = at.type === "object" ? at : at.variants[String(v[at.tag])];
|
|
577
|
+
if (!shape)
|
|
578
|
+
return { keep: false, value: null };
|
|
579
|
+
if (shape.private) {
|
|
580
|
+
const unless = shape.private === true ? null : shape.private.unless;
|
|
581
|
+
if (!unless || v[unless] !== true)
|
|
582
|
+
return { keep: false, value: null };
|
|
583
|
+
}
|
|
584
|
+
const out = {};
|
|
585
|
+
if (at.type === "union")
|
|
586
|
+
out[at.tag] = v[at.tag];
|
|
587
|
+
for (const [key, child] of Object.entries(shape.fields)) {
|
|
588
|
+
if (!Object.hasOwn(v, key))
|
|
589
|
+
continue;
|
|
590
|
+
const kept = strip(child, v[key], depth + 1);
|
|
591
|
+
if (kept.keep)
|
|
592
|
+
out[key] = kept.value;
|
|
593
|
+
}
|
|
594
|
+
return { keep: true, value: out };
|
|
595
|
+
}
|
|
596
|
+
case "array": {
|
|
597
|
+
if (!Array.isArray(v))
|
|
598
|
+
return { keep: false, value: null };
|
|
599
|
+
return {
|
|
600
|
+
keep: true,
|
|
601
|
+
value: v
|
|
602
|
+
.map((item) => strip(at.of, item, depth + 1))
|
|
603
|
+
.filter((r) => r.keep)
|
|
604
|
+
.map((r) => r.value),
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
default:
|
|
608
|
+
return { keep: true, value: v };
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
if (!validateDocument(schema, value, { maxBytes: DOCUMENT_MAX_BYTES }).ok)
|
|
612
|
+
return null;
|
|
613
|
+
const result = strip(schema.root, value, 0);
|
|
614
|
+
return result.keep ? result.value : null;
|
|
615
|
+
}
|
|
616
|
+
function visit(schema, value, each) {
|
|
617
|
+
const walk = (node, v, path, depth) => {
|
|
618
|
+
const at = useOf(schema, node);
|
|
619
|
+
if (!at || depth > DOCUMENT_MAX_DEPTH)
|
|
620
|
+
return;
|
|
621
|
+
each(at, v, path);
|
|
622
|
+
if (at.type === "array" && Array.isArray(v)) {
|
|
623
|
+
v.forEach((item, i) => {
|
|
624
|
+
walk(at.of, item, [...path, i], depth + 1);
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
else if ((at.type === "object" || at.type === "union") && isObject(v)) {
|
|
628
|
+
const shape = at.type === "object" ? at : at.variants[String(v[at.tag])];
|
|
629
|
+
for (const [key, child] of Object.entries(shape?.fields ?? {})) {
|
|
630
|
+
if (Object.hasOwn(v, key))
|
|
631
|
+
walk(child, v[key], [...path, key], depth + 1);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
walk(schema.root, value, [], 0);
|
|
636
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { FormulaType } from "./formula/ast.ts";
|
|
2
|
+
export declare const FIELD_TYPES: readonly ["text", "longText", "number", "currency", "percent", "date", "datetime", "boolean", "select", "multiSelect", "relation", "user", "email", "phone", "url", "formula", "document", "timezone", "richText", "slug", "image"];
|
|
3
|
+
export type FieldTypeName = (typeof FIELD_TYPES)[number];
|
|
4
|
+
/** What a formula sees when it reads a field of this type (null: not readable from formulas). */
|
|
5
|
+
export declare function formulaTypeOf(type: FieldTypeName, many?: boolean): Exclude<FormulaType, "null"> | null;
|
|
6
|
+
/**
|
|
7
|
+
* Type changes the reconciler accepts: the stored values stay valid and keep their meaning.
|
|
8
|
+
* Anything else is refused with a suggestion to add a new field and migrate.
|
|
9
|
+
*/
|
|
10
|
+
export declare const LOSSLESS_TYPE_CHANGES: Readonly<Record<FieldTypeName, readonly FieldTypeName[]>>;
|
|
11
|
+
export declare function isLosslessChange(from: FieldTypeName, to: FieldTypeName): boolean;
|