viewkitjs 0.19.0 → 0.21.0
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/CHANGELOG.md +66 -0
- package/MIGRATION.md +117 -0
- package/README.md +7 -3
- package/fields/README.md +3 -3
- package/fields/dist/derived.d.ts +4 -0
- package/fields/dist/derived.js +39 -0
- package/fields/dist/entities.js +30 -30
- package/fields/dist/formula.js +2 -2
- package/fields/dist/query/evaluate.js +5 -5
- package/fields/dist/query/figures.js +5 -5
- package/fields/dist/query/filter.d.ts +15 -0
- package/fields/dist/query/filter.js +85 -0
- package/fields/dist/query/hold.js +2 -2
- package/fields/dist/query/option-counts.js +2 -2
- package/fields/dist/query/read-rule.js +2 -2
- package/fields/dist/query/search.js +2 -2
- package/fields/dist/query/set-aside.js +4 -4
- package/fields/dist/query/settings.d.ts +2 -15
- package/fields/dist/query/settings.js +3 -81
- package/fields/dist/query/sort.js +4 -4
- package/fields/dist/registry.d.ts +19 -5
- package/fields/dist/registry.js +371 -50
- package/fields/dist/types.d.ts +1 -1
- package/fields/docs/getting-started.md +2 -2
- package/fields/docs/guides/checking-definitions.md +17 -4
- package/fields/docs/guides/derived-fields.md +5 -5
- package/fields/docs/guides/queries.md +1 -1
- package/fields/docs/reference/design-category-colors.md +1 -1
- package/fields/docs/reference/design-status-slots.md +1 -1
- package/fields/docs/reference/fields.md +3 -3
- package/fields/docs/reference/query.md +1 -0
- package/fields/skills/viewkit/SKILL.md +2 -2
- package/package.json +17 -6
- package/postgres/README.md +2 -2
- package/postgres/dist/access.d.ts +12 -4
- package/postgres/dist/access.js +3 -0
- package/postgres/dist/calendar.js +3 -2
- package/postgres/dist/check.d.ts +6 -4
- package/postgres/dist/check.js +27 -16
- package/postgres/dist/counts.js +3 -2
- package/postgres/dist/definitions/build.js +7 -0
- package/postgres/dist/definitions/formula.d.ts +1 -1
- package/postgres/dist/definitions/formula.js +5 -3
- package/postgres/dist/definitions/lookup.js +5 -1
- package/postgres/dist/definitions/money.js +6 -1
- package/postgres/dist/definitions/place.d.ts +7 -0
- package/postgres/dist/definitions/place.js +8 -0
- package/postgres/dist/definitions/relations.js +36 -3
- package/postgres/dist/definitions/rollup.d.ts +1 -1
- package/postgres/dist/definitions/rollup.js +11 -6
- package/postgres/dist/field-values.js +11 -8
- package/postgres/dist/figures.js +4 -2
- package/postgres/dist/filled.js +2 -2
- package/postgres/dist/groups.d.ts +2 -1
- package/postgres/dist/groups.js +5 -3
- package/postgres/dist/index.d.ts +1 -0
- package/postgres/dist/index.js +1 -0
- package/postgres/dist/kept.js +8 -4
- package/postgres/dist/lanes.js +5 -3
- package/postgres/dist/phone.js +2 -2
- package/postgres/dist/predicates.js +2 -2
- package/postgres/dist/readable.d.ts +40 -0
- package/postgres/dist/readable.js +84 -0
- package/postgres/dist/records.js +7 -4
- package/postgres/dist/refusals.d.ts +8 -1
- package/postgres/dist/refusals.js +7 -0
- package/postgres/dist/relations.js +5 -4
- package/postgres/dist/search-indexes.js +2 -1
- package/postgres/dist/search.js +6 -4
- package/postgres/dist/several.js +8 -8
- package/postgres/dist/sort.d.ts +5 -3
- package/postgres/dist/sort.js +13 -8
- package/postgres/dist/value-rows.js +2 -2
- package/postgres/dist/values.js +4 -2
- package/postgres/docs/guides/definitions.md +2 -2
- package/postgres/docs/guides/field-map.md +43 -18
- package/postgres/docs/guides/test-kit.md +2 -0
- package/postgres/docs/reference/postgres.md +10 -4
- package/views/README.md +4 -2
- package/views/dist/atoms/StatusBadge.js +1 -1
- package/views/dist/atoms/tones.js +7 -7
- package/views/dist/atoms/types.d.ts +3 -2
- package/views/dist/contract.d.ts +7 -2
- package/views/dist/contract.js +33 -0
- package/views/dist/display/fit-values.d.ts +3 -2
- package/views/dist/display/link.d.ts +1 -1
- package/views/dist/display/location.js +2 -1
- package/views/dist/display/multi-value.js +1 -1
- package/views/dist/display/name.js +3 -1
- package/views/dist/display/select.js +1 -1
- package/views/dist/display/value-list.d.ts +1 -1
- package/views/dist/grid/chrome/GridStructure.js +2 -1
- package/views/dist/grid/looks.d.ts +45 -0
- package/views/dist/grid/looks.js +74 -0
- package/views/dist/looks/index.d.ts +7 -0
- package/views/dist/looks/index.js +3 -0
- package/views/dist/looks/looks.d.ts +46 -0
- package/views/dist/looks/looks.js +25 -0
- package/views/dist/looks/pieces.d.ts +3 -0
- package/views/dist/looks/pieces.js +2 -0
- package/views/dist/provider/ViewsProvider.d.ts +3 -3
- package/views/dist/provider/ViewsProvider.js +1 -1
- package/views/dist/provider/context.d.ts +1 -1
- package/views/dist/styles.css +3277 -0
- package/views/dist/styles.unlayered.css +3276 -0
- package/views/docs/contract.md +61 -13
- package/views/docs/getting-started.md +1 -1
- package/views/docs/guides/display.md +11 -7
- package/views/docs/guides/looks.md +29 -0
- package/views/docs/guides/the-grid.md +1 -1
- package/views/docs/guides/the-provider.md +1 -1
- package/views/docs/index.md +2 -0
- package/views/docs/install.md +36 -9
- package/views/docs/reference/display.md +2 -2
- package/views/docs/reference/looks.md +31 -0
- package/views/docs/reference/react.md +2 -2
- package/views/docs/reference/views.md +1 -1
- package/views/skills/views/SKILL.md +2 -2
package/fields/dist/registry.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
1
2
|
import { DEFAULT_CONVENTIONS } from "./conventions.js";
|
|
2
3
|
import { FieldDefinitionError } from "./errors.js";
|
|
3
|
-
import { checkFormula, formulaReads } from "./formula.js";
|
|
4
|
-
import {
|
|
4
|
+
import { checkFormula, FORMULA_MAX_NODES, formulaReads } from "./formula.js";
|
|
5
|
+
import { filterTreeSchema } from "./query/filter.js";
|
|
6
|
+
import { isDerived, isGroupedRollup, ownField, presentedDef, valueTypeOf } from "./derived.js";
|
|
5
7
|
import { NORMALIZE_STEPS } from "./normalize.js";
|
|
6
8
|
import { isDecimalText } from "./parse-number.js";
|
|
7
9
|
import { LOCATION_PARTS } from "./location.js";
|
|
@@ -54,13 +56,14 @@ export const CATEGORY_COLORS = [
|
|
|
54
56
|
];
|
|
55
57
|
export { FieldDefinitionError };
|
|
56
58
|
export function defineFields(input) {
|
|
57
|
-
const { entity, identity, rename, subtitle = [], avatar, fields, allowSql =
|
|
59
|
+
const { entity, identity, rename, subtitle = [], avatar, fields, allowSql = false } = input;
|
|
58
60
|
if (!entity)
|
|
59
61
|
throw new FieldDefinitionError(identity, "a registry names the entity it describes");
|
|
60
|
-
|
|
62
|
+
checkFieldShapes(identity, fields);
|
|
63
|
+
if (ownField(fields, identity) === undefined) {
|
|
61
64
|
throw new FieldDefinitionError(identity, "identity is not a key of fields");
|
|
62
65
|
}
|
|
63
|
-
if (fields
|
|
66
|
+
if (ownField(fields, identity)?.type === "lookup") {
|
|
64
67
|
throw new FieldDefinitionError(identity, "a lookup is another record's field — it cannot be this record's identity");
|
|
65
68
|
}
|
|
66
69
|
checkLineEntries(identity, subtitle, fields);
|
|
@@ -74,13 +77,19 @@ export function defineFields(input) {
|
|
|
74
77
|
checkAvatar(identity, avatar, fields);
|
|
75
78
|
return Object.freeze(stampFields(entity, fields, identity, subtitle, rename, avatar, allowSql));
|
|
76
79
|
}
|
|
80
|
+
function checkFieldShapes(owner, fields) {
|
|
81
|
+
if (!fields || typeof fields !== "object" || Array.isArray(fields))
|
|
82
|
+
throw new FieldDefinitionError(owner, "fields is an object of definitions");
|
|
83
|
+
for (const [key, spec] of Object.entries(fields))
|
|
84
|
+
checkShape(key, spec);
|
|
85
|
+
}
|
|
77
86
|
export function checkLineEntries(identity, keys, fields) {
|
|
78
87
|
const seen = new Set();
|
|
79
88
|
for (const key of keys) {
|
|
80
89
|
if (key.includes("."))
|
|
81
90
|
checkSubtitlePath(key, fields);
|
|
82
91
|
else {
|
|
83
|
-
const spec = fields
|
|
92
|
+
const spec = ownField(fields, key);
|
|
84
93
|
if (!spec)
|
|
85
94
|
throw new FieldDefinitionError(key, `subtitle names no field "${key}"`);
|
|
86
95
|
if (key === identity)
|
|
@@ -100,7 +109,7 @@ function checkSubtitlePath(key, fields) {
|
|
|
100
109
|
const field = key.slice(dot + 1);
|
|
101
110
|
if (!field || field.includes("."))
|
|
102
111
|
throw new FieldDefinitionError(key, `subtitle reads one hop: "${key}"`);
|
|
103
|
-
const spec = fields
|
|
112
|
+
const spec = ownField(fields, via);
|
|
104
113
|
if (!spec)
|
|
105
114
|
throw new FieldDefinitionError(key, `subtitle names no relation "${via}"`);
|
|
106
115
|
if (spec.type !== "relation")
|
|
@@ -109,7 +118,7 @@ function checkSubtitlePath(key, fields) {
|
|
|
109
118
|
throw new FieldDefinitionError(key, "a many relation cannot be the line under the name");
|
|
110
119
|
}
|
|
111
120
|
function checkAvatar(identity, avatar, fields) {
|
|
112
|
-
const spec = fields
|
|
121
|
+
const spec = ownField(fields, avatar);
|
|
113
122
|
if (!spec)
|
|
114
123
|
throw new FieldDefinitionError(avatar, `avatar names no field "${avatar}"`);
|
|
115
124
|
if (avatar === identity)
|
|
@@ -127,7 +136,7 @@ function checkRename(identity, rename, fields) {
|
|
|
127
136
|
});
|
|
128
137
|
}
|
|
129
138
|
function checkEditThrough(key, through, fields, identity) {
|
|
130
|
-
if (fields
|
|
139
|
+
if (ownField(fields, key).type !== "formula") {
|
|
131
140
|
throw new FieldDefinitionError(key, "only a worked-out field (a formula) is edited through another field");
|
|
132
141
|
}
|
|
133
142
|
if (key === identity)
|
|
@@ -140,8 +149,8 @@ function checkEditThrough(key, through, fields, identity) {
|
|
|
140
149
|
});
|
|
141
150
|
}
|
|
142
151
|
function checkWrittenThrough(key, through, fields, words) {
|
|
143
|
-
const owner = fields
|
|
144
|
-
const target = fields
|
|
152
|
+
const owner = ownField(fields, key);
|
|
153
|
+
const target = ownField(fields, through);
|
|
145
154
|
const fail = (why) => {
|
|
146
155
|
throw new FieldDefinitionError(key, why);
|
|
147
156
|
};
|
|
@@ -162,13 +171,14 @@ function checkWrittenThrough(key, through, fields, words) {
|
|
|
162
171
|
if (!(rule ? formulaReads(rule) : []).includes(through))
|
|
163
172
|
fail(`${words.owner} never reads "${through}"`);
|
|
164
173
|
}
|
|
165
|
-
function stampFields(entity, fields, identity, subtitle = [], rename, avatar, allowSql =
|
|
174
|
+
function stampFields(entity, fields, identity, subtitle = [], rename, avatar, allowSql = false) {
|
|
166
175
|
for (const [key, spec] of Object.entries(fields))
|
|
167
176
|
checkFieldSpec(key, spec, fields, { allowSql });
|
|
168
177
|
checkFormulaCycles(fields);
|
|
169
178
|
const through = Object.fromEntries(subtitle.flatMap((key, place) => (key.includes(".") ? [[key, place]] : [])));
|
|
170
179
|
const out = {};
|
|
171
|
-
for (const [key,
|
|
180
|
+
for (const [key, given] of Object.entries(fields)) {
|
|
181
|
+
const { identity: _identity, avatar: _avatar, rename: _rename, subtitleThrough: _through, ...spec } = given;
|
|
172
182
|
out[key] = Object.freeze({
|
|
173
183
|
...spec,
|
|
174
184
|
...(spec.type === "rollup" ||
|
|
@@ -189,11 +199,12 @@ function stampFields(entity, fields, identity, subtitle = [], rename, avatar, al
|
|
|
189
199
|
return out;
|
|
190
200
|
}
|
|
191
201
|
export function contributeFields(input) {
|
|
192
|
-
const { entity, from, fields, allowSql =
|
|
202
|
+
const { entity, from, fields, allowSql = false } = input;
|
|
193
203
|
if (!from)
|
|
194
204
|
throw new FieldDefinitionError(String(entity ?? "?"), "a contribution names who adds it");
|
|
195
205
|
if (!entity)
|
|
196
206
|
throw new FieldDefinitionError(from, "a contribution names the entity it adds to");
|
|
207
|
+
checkFieldShapes(from, fields);
|
|
197
208
|
for (const [key, spec] of Object.entries(fields)) {
|
|
198
209
|
if (spec.editThrough !== undefined) {
|
|
199
210
|
throw new FieldDefinitionError(key, "a contributed field is never edited through another field");
|
|
@@ -207,7 +218,7 @@ export function contributeFields(input) {
|
|
|
207
218
|
}
|
|
208
219
|
function checkFormulaCycles(fields) {
|
|
209
220
|
const readsOf = (key) => {
|
|
210
|
-
const f = fields
|
|
221
|
+
const f = ownField(fields, key);
|
|
211
222
|
if (!f || f.type !== "formula")
|
|
212
223
|
return [];
|
|
213
224
|
return [
|
|
@@ -234,7 +245,7 @@ export function checkInverse(registries) {
|
|
|
234
245
|
for (const [key, f] of Object.entries(fields)) {
|
|
235
246
|
if (f.type !== "relation" || !f.inverse)
|
|
236
247
|
continue;
|
|
237
|
-
const far = registries
|
|
248
|
+
const far = ownField(ownField(registries, f.inverse.kind), f.inverse.field);
|
|
238
249
|
const back = far &&
|
|
239
250
|
far.type === "relation" &&
|
|
240
251
|
far.inverse &&
|
|
@@ -276,15 +287,17 @@ export function checkRegistries(registries) {
|
|
|
276
287
|
continue;
|
|
277
288
|
if (typeof f.over !== "string" || f.fn === "countDistinct" || f.fn === "first")
|
|
278
289
|
continue;
|
|
279
|
-
const over = fields
|
|
290
|
+
const over = ownField(fields, f.over);
|
|
280
291
|
if (!over || over.type !== "relation")
|
|
281
292
|
continue;
|
|
293
|
+
if (!isObject(over.relation))
|
|
294
|
+
continue;
|
|
282
295
|
const farKinds = "kinds" in over.relation ? over.relation.kinds : [over.relation.kind];
|
|
283
296
|
for (const farKind of farKinds) {
|
|
284
|
-
const far = registries
|
|
297
|
+
const far = ownField(registries, farKind);
|
|
285
298
|
if (!far)
|
|
286
299
|
continue;
|
|
287
|
-
const target = far
|
|
300
|
+
const target = ownField(far, f.of);
|
|
288
301
|
if (!target) {
|
|
289
302
|
throw new FieldDefinitionError(`${kind}.${key}`, `of "${f.of}" is not a field of ${farKind}`);
|
|
290
303
|
}
|
|
@@ -316,33 +329,38 @@ const ROLLUP_FNS = [
|
|
|
316
329
|
"none",
|
|
317
330
|
"first",
|
|
318
331
|
];
|
|
319
|
-
|
|
332
|
+
function isObject(value) {
|
|
333
|
+
return value !== null && typeof value === "object";
|
|
334
|
+
}
|
|
335
|
+
function wordOf(value) {
|
|
336
|
+
try {
|
|
337
|
+
return String(value);
|
|
338
|
+
}
|
|
339
|
+
catch {
|
|
340
|
+
return "an object";
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
function closedWord(key) {
|
|
344
|
+
return (value, allowed, what) => {
|
|
345
|
+
if (typeof value !== "string" || !allowed.includes(value))
|
|
346
|
+
throw new FieldDefinitionError(key, `${what} "${wordOf(value)}" is not one of: ${allowed.join(", ")}`);
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
export function checkShape(key, spec) {
|
|
350
|
+
checkRawSpec(key, spec);
|
|
351
|
+
checkClosedWords(spec, closedWord(key));
|
|
352
|
+
checkStrictShape(key, spec);
|
|
353
|
+
}
|
|
354
|
+
export function checkFieldSpec(key, spec, all = {}, context = {}) {
|
|
320
355
|
const fail = (why) => {
|
|
321
356
|
throw new FieldDefinitionError(key, why);
|
|
322
357
|
};
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
return path;
|
|
330
|
-
for (const [k, v] of Object.entries(value)) {
|
|
331
|
-
const hit = notData(v, `${path}.${k}`);
|
|
332
|
-
if (hit)
|
|
333
|
-
return hit;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
return null;
|
|
337
|
-
};
|
|
338
|
-
const hit = notData(f, key);
|
|
339
|
-
if (hit)
|
|
340
|
-
fail(`${hit} is not plain data — a definition must be something a row can hold`);
|
|
341
|
-
const oneOf = (value, allowed, what) => {
|
|
342
|
-
if (typeof value !== "string" || !allowed.includes(value))
|
|
343
|
-
fail(`${what} "${String(value)}" is not one of: ${allowed.join(", ")}`);
|
|
344
|
-
};
|
|
345
|
-
checkClosedWords(f, oneOf);
|
|
358
|
+
if (context.inResult)
|
|
359
|
+
checkClosedWords(spec, closedWord(key));
|
|
360
|
+
else
|
|
361
|
+
checkShape(key, spec);
|
|
362
|
+
const f = spec;
|
|
363
|
+
const oneOf = closedWord(key);
|
|
346
364
|
const options = "options" in f ? f.options : undefined;
|
|
347
365
|
if (options) {
|
|
348
366
|
const seen = new Set();
|
|
@@ -408,7 +426,7 @@ export function checkFieldSpec(key, f, all = {}, context = {}) {
|
|
|
408
426
|
fail("a required field cannot also be emptiable");
|
|
409
427
|
if (f.type === "phone" || f.type === "domain") {
|
|
410
428
|
const v = f.validation;
|
|
411
|
-
const onlyMaxItems = f.multiple === true && v
|
|
429
|
+
const onlyMaxItems = f.multiple === true && isObject(v) && Object.keys(v).every((k) => k === "maxItems");
|
|
412
430
|
if (v !== undefined && !onlyMaxItems)
|
|
413
431
|
fail(f.type === "phone"
|
|
414
432
|
? "a phone takes no validation: the phone library checks it"
|
|
@@ -617,14 +635,14 @@ export function checkFieldSpec(key, f, all = {}, context = {}) {
|
|
|
617
635
|
}
|
|
618
636
|
}
|
|
619
637
|
else if ("field" in f.code) {
|
|
620
|
-
const sibling = all
|
|
638
|
+
const sibling = ownField(all, f.code.field);
|
|
621
639
|
if (!sibling || sibling.type !== "select")
|
|
622
640
|
fail(`code.field "${f.code.field}" is not a sibling select`);
|
|
623
641
|
}
|
|
624
642
|
if (f.convert) {
|
|
625
643
|
const day = conversionDay(f.convert) ??
|
|
626
644
|
fail(`convert.at "${String(f.convert.at)}" is no day a rate is read on — "today", or { field } naming a date of the row`);
|
|
627
|
-
if (day !== "today" && !isDated(all
|
|
645
|
+
if (day !== "today" && !isDated(ownField(all, day))) {
|
|
628
646
|
fail(`convert.at.field "${day}" is not a sibling date — a date or datetime, stored or a formula's or rollup's result`);
|
|
629
647
|
}
|
|
630
648
|
const code = "field" in f.code
|
|
@@ -671,10 +689,10 @@ export function checkFieldSpec(key, f, all = {}, context = {}) {
|
|
|
671
689
|
fail("a lookup's far definition is filled in by defineIndex(), never written — remove target");
|
|
672
690
|
if (!f.field)
|
|
673
691
|
fail("a lookup names the far field it shows");
|
|
674
|
-
const via = all
|
|
692
|
+
const via = ownField(all, f.via);
|
|
675
693
|
if (!via || via.type !== "relation" || via.cardinality === "many")
|
|
676
694
|
fail(`via "${f.via}" is not a single relation of this registry — across a many relation, use a rollup`);
|
|
677
|
-
if (via
|
|
695
|
+
if (via?.type === "relation" && isObject(via.relation) && "kinds" in via.relation)
|
|
678
696
|
fail(`via "${f.via}" names kinds — a lookup goes through a relation that names one kind`);
|
|
679
697
|
if (f.edit?.mutability !== undefined && f.edit.mutability !== "never")
|
|
680
698
|
fail("a lookup is read-only — it is edited on the record that owns it");
|
|
@@ -690,12 +708,13 @@ export function checkFieldSpec(key, f, all = {}, context = {}) {
|
|
|
690
708
|
}
|
|
691
709
|
if (f.type === "rollup") {
|
|
692
710
|
const first = typeof f.over === "string" ? f.over : f.over[0];
|
|
693
|
-
const over = all
|
|
711
|
+
const over = ownField(all, first);
|
|
694
712
|
if (!over || over.type !== "relation")
|
|
695
713
|
fail(`over "${String(first)}" is not a relation field of this registry`);
|
|
696
714
|
if (typeof f.over !== "string" && (f.over.length !== 2 || !f.over[1]))
|
|
697
715
|
fail("two hops name exactly two relations");
|
|
698
716
|
if (over?.type === "relation" &&
|
|
717
|
+
isObject(over.relation) &&
|
|
699
718
|
"kinds" in over.relation &&
|
|
700
719
|
(typeof f.over !== "string" ||
|
|
701
720
|
f.where ||
|
|
@@ -776,7 +795,7 @@ export function checkFieldSpec(key, f, all = {}, context = {}) {
|
|
|
776
795
|
if (f.result.type !== "percent")
|
|
777
796
|
fail("a ratio formula produces a percent");
|
|
778
797
|
for (const part of [f.ratio.numerator, f.ratio.denominator]) {
|
|
779
|
-
const field = all
|
|
798
|
+
const field = ownField(all, part);
|
|
780
799
|
const def = field && { ...field, key: part };
|
|
781
800
|
const ok = def && valueTypeOf(def) === "number" && !isGroupedRollup(def);
|
|
782
801
|
if (!ok)
|
|
@@ -862,3 +881,305 @@ function checkClosedWords(f, oneOf) {
|
|
|
862
881
|
if (f.type === "rollup" && loose.orderBy && typeof loose.orderBy === "object")
|
|
863
882
|
oneOf(loose.orderBy.direction, DIRECTIONS, "orderBy.direction");
|
|
864
883
|
}
|
|
884
|
+
export const FIELD_SPEC_MAX_DEPTH = 2 * FORMULA_MAX_NODES + 8;
|
|
885
|
+
const text = z.string();
|
|
886
|
+
const word = z.string();
|
|
887
|
+
const finite = z.number();
|
|
888
|
+
const label = z.string().refine((value) => value.trim() !== "", "is blank");
|
|
889
|
+
const ruled = z.unknown();
|
|
890
|
+
const filterTree = z
|
|
891
|
+
.custom((value) => !!value && typeof value === "object" && ["and", "or"].includes(value.op), "is a filter tree")
|
|
892
|
+
.pipe(filterTreeSchema);
|
|
893
|
+
const edit = z.strictObject({
|
|
894
|
+
mutability: word.optional(),
|
|
895
|
+
reasons: z.strictObject({ editable: text.optional(), clearable: text.optional() }).optional(),
|
|
896
|
+
});
|
|
897
|
+
const provenance = z.strictObject({ owner: word, provider: text.optional(), freshness: text.optional() });
|
|
898
|
+
const BASE = {
|
|
899
|
+
label,
|
|
900
|
+
description: text.optional(),
|
|
901
|
+
required: z.boolean().optional(),
|
|
902
|
+
nullable: z.boolean().optional(),
|
|
903
|
+
emptiable: z.boolean().optional(),
|
|
904
|
+
unique: z.boolean().optional(),
|
|
905
|
+
edit: edit.optional(),
|
|
906
|
+
provenance: provenance.optional(),
|
|
907
|
+
editThrough: text.optional(),
|
|
908
|
+
multiple: ruled.optional(),
|
|
909
|
+
key: text.optional(),
|
|
910
|
+
entity: text.optional(),
|
|
911
|
+
identity: z.literal(true).optional(),
|
|
912
|
+
subtitle: finite.optional(),
|
|
913
|
+
subtitleThrough: z.record(z.string(), finite).optional(),
|
|
914
|
+
rename: text.optional(),
|
|
915
|
+
avatar: z.literal(true).optional(),
|
|
916
|
+
};
|
|
917
|
+
const mark = z.strictObject({ kind: word, fraction: finite.optional(), color: text.optional() });
|
|
918
|
+
const option = z.strictObject({
|
|
919
|
+
value: text,
|
|
920
|
+
label: text,
|
|
921
|
+
slot: word.optional(),
|
|
922
|
+
color: word.optional(),
|
|
923
|
+
mark: mark.optional(),
|
|
924
|
+
archived: z.boolean().optional(),
|
|
925
|
+
icon: text.optional(),
|
|
926
|
+
});
|
|
927
|
+
const options = z.array(option);
|
|
928
|
+
const band = z.strictObject({ min: ruled, tone: word });
|
|
929
|
+
const numberValidation = {
|
|
930
|
+
min: finite.optional(),
|
|
931
|
+
max: finite.optional(),
|
|
932
|
+
gt: finite.optional(),
|
|
933
|
+
lt: finite.optional(),
|
|
934
|
+
integer: z.boolean().optional(),
|
|
935
|
+
decimals: finite.optional(),
|
|
936
|
+
};
|
|
937
|
+
const numberFormat = z.union([
|
|
938
|
+
z.strictObject({
|
|
939
|
+
style: z.literal("plain").optional(),
|
|
940
|
+
decimals: finite.optional(),
|
|
941
|
+
suffix: text.optional(),
|
|
942
|
+
signed: z.boolean().optional(),
|
|
943
|
+
signedTone: word.optional(),
|
|
944
|
+
bands: z.array(band).optional(),
|
|
945
|
+
}),
|
|
946
|
+
z.strictObject({ style: z.literal("duration"), unit: word.optional(), durationStyle: word.optional() }),
|
|
947
|
+
]);
|
|
948
|
+
const currencyCode = z.union([
|
|
949
|
+
z.strictObject({ fixed: text }),
|
|
950
|
+
z.strictObject({ field: text }),
|
|
951
|
+
z.strictObject({ perCode: z.literal(true) }),
|
|
952
|
+
]);
|
|
953
|
+
const conversion = z.strictObject({ at: ruled });
|
|
954
|
+
const listMax = z.strictObject({ maxItems: finite.optional() });
|
|
955
|
+
const TEXT = {
|
|
956
|
+
type: z.literal("text"),
|
|
957
|
+
validation: z.strictObject({ maxLength: finite.optional(), pattern: text.optional(), minLength: finite.optional() }).optional(),
|
|
958
|
+
format: z.strictObject({ long: z.boolean().optional() }).optional(),
|
|
959
|
+
};
|
|
960
|
+
const NUMBER = {
|
|
961
|
+
type: z.literal("number"),
|
|
962
|
+
storage: word.optional(),
|
|
963
|
+
validation: z.strictObject(numberValidation).optional(),
|
|
964
|
+
format: numberFormat.optional(),
|
|
965
|
+
};
|
|
966
|
+
const PERCENT = {
|
|
967
|
+
type: z.literal("percent"),
|
|
968
|
+
storage: word.optional(),
|
|
969
|
+
validation: z.strictObject({ ...numberValidation, reason: text.optional() }).optional(),
|
|
970
|
+
format: z.strictObject({ decimals: finite.optional(), display: word.optional(), bands: z.array(band).optional() }).optional(),
|
|
971
|
+
};
|
|
972
|
+
const CURRENCY = {
|
|
973
|
+
type: z.literal("currency"),
|
|
974
|
+
code: currencyCode,
|
|
975
|
+
convert: conversion.optional(),
|
|
976
|
+
storage: word.optional(),
|
|
977
|
+
validation: z.strictObject(numberValidation).optional(),
|
|
978
|
+
format: z
|
|
979
|
+
.strictObject({ decimals: finite.optional(), suffix: text.optional(), signed: z.boolean().optional(), signedTone: word.optional() })
|
|
980
|
+
.optional(),
|
|
981
|
+
};
|
|
982
|
+
const DATE = { type: z.literal("date"), ahead: ruled.optional(), format: z.strictObject({ dateStyle: word.optional() }).optional() };
|
|
983
|
+
const DATETIME = {
|
|
984
|
+
type: z.literal("datetime"),
|
|
985
|
+
ahead: ruled.optional(),
|
|
986
|
+
format: z.strictObject({ dateStyle: word.optional(), withTime: z.boolean().optional() }).optional(),
|
|
987
|
+
};
|
|
988
|
+
const BOOLEAN = {
|
|
989
|
+
type: z.literal("boolean"),
|
|
990
|
+
format: z.strictObject({ style: word.optional() }).optional(),
|
|
991
|
+
options: options.optional(),
|
|
992
|
+
};
|
|
993
|
+
const SELECT = { type: z.literal("select"), options: options.optional(), vocabulary: word.optional() };
|
|
994
|
+
const STATUS = {
|
|
995
|
+
type: z.literal("status"),
|
|
996
|
+
options: options.optional(),
|
|
997
|
+
groups: z.array(z.strictObject({ key: text, label: text, slot: word.optional(), options: z.array(text) })).optional(),
|
|
998
|
+
};
|
|
999
|
+
const formulaNode = z.lazy(() => z.union([
|
|
1000
|
+
z.strictObject({ field: text }),
|
|
1001
|
+
z.strictObject({ value: z.union([text, finite, z.boolean(), z.null()]) }),
|
|
1002
|
+
z.strictObject({ fn: word, args: z.array(formulaNode) }),
|
|
1003
|
+
]));
|
|
1004
|
+
const rule = z.union([formulaNode, z.strictObject({ sql: text })]);
|
|
1005
|
+
const ruledValidation = { validation: ruled.optional() };
|
|
1006
|
+
const MULTISELECT = {
|
|
1007
|
+
type: z.literal("multiselect"),
|
|
1008
|
+
vocabulary: word,
|
|
1009
|
+
options: options.optional(),
|
|
1010
|
+
validation: z
|
|
1011
|
+
.strictObject({
|
|
1012
|
+
maxItems: finite.optional(),
|
|
1013
|
+
item: z.strictObject({ minLength: finite.optional(), maxLength: finite.optional() }).optional(),
|
|
1014
|
+
})
|
|
1015
|
+
.optional(),
|
|
1016
|
+
normalize: ruled.optional(),
|
|
1017
|
+
};
|
|
1018
|
+
const RELATION = {
|
|
1019
|
+
type: z.literal("relation"),
|
|
1020
|
+
relation: z.union([z.strictObject({ kind: text }), z.strictObject({ kinds: z.array(text) })]),
|
|
1021
|
+
cardinality: word.optional(),
|
|
1022
|
+
inverse: z.strictObject({ kind: text, field: text }).optional(),
|
|
1023
|
+
links: z.strictObject({ roles: options.optional(), primary: ruled.optional() }).optional(),
|
|
1024
|
+
sortBy: text.optional(),
|
|
1025
|
+
within: ruled.optional(),
|
|
1026
|
+
where: filterTree.optional(),
|
|
1027
|
+
};
|
|
1028
|
+
const maxLength = { validation: z.strictObject({ maxLength: finite.optional() }).optional() };
|
|
1029
|
+
const RANGE = {
|
|
1030
|
+
type: z.literal("range"),
|
|
1031
|
+
of: ruled,
|
|
1032
|
+
code: z.strictObject({ fixed: text }).optional(),
|
|
1033
|
+
period: word.optional(),
|
|
1034
|
+
validation: z.strictObject({ min: finite.optional(), max: finite.optional(), integer: z.boolean().optional() }).optional(),
|
|
1035
|
+
format: z.strictObject({ decimals: finite.optional(), suffix: text.optional() }).optional(),
|
|
1036
|
+
presets: z.array(z.strictObject({ key: ruled.optional(), label: ruled.optional(), low: finite.nullable(), high: finite.nullable() })).optional(),
|
|
1037
|
+
};
|
|
1038
|
+
const STORED = [
|
|
1039
|
+
TEXT,
|
|
1040
|
+
NUMBER,
|
|
1041
|
+
PERCENT,
|
|
1042
|
+
CURRENCY,
|
|
1043
|
+
DATE,
|
|
1044
|
+
DATETIME,
|
|
1045
|
+
BOOLEAN,
|
|
1046
|
+
SELECT,
|
|
1047
|
+
STATUS,
|
|
1048
|
+
MULTISELECT,
|
|
1049
|
+
RELATION,
|
|
1050
|
+
{ type: z.literal("user") },
|
|
1051
|
+
{ type: z.literal("url"), validation: z.strictObject({ maxLength: finite.optional(), maxItems: finite.optional() }).optional() },
|
|
1052
|
+
{ type: z.literal("domain"), ...ruledValidation },
|
|
1053
|
+
{ type: z.literal("email"), validation: listMax.optional() },
|
|
1054
|
+
{ type: z.literal("phone"), ...ruledValidation },
|
|
1055
|
+
{ type: z.literal("location"), parts: z.array(word).optional(), ...maxLength },
|
|
1056
|
+
{ type: z.literal("name"), ...maxLength },
|
|
1057
|
+
{ type: z.literal("actor") },
|
|
1058
|
+
{ type: z.literal("rich_text"), ...maxLength },
|
|
1059
|
+
RANGE,
|
|
1060
|
+
];
|
|
1061
|
+
const resultSchema = z.discriminatedUnion("type", STORED.map((own) => z.strictObject(own)));
|
|
1062
|
+
const DEFAULTS = {
|
|
1063
|
+
text: text,
|
|
1064
|
+
number: z.union([finite, text]),
|
|
1065
|
+
percent: z.union([finite, text]),
|
|
1066
|
+
currency: z.union([finite, text]),
|
|
1067
|
+
date: text,
|
|
1068
|
+
datetime: text,
|
|
1069
|
+
boolean: z.boolean(),
|
|
1070
|
+
select: text,
|
|
1071
|
+
status: text,
|
|
1072
|
+
multiselect: z.array(text),
|
|
1073
|
+
rich_text: text,
|
|
1074
|
+
};
|
|
1075
|
+
const fieldSpecSchema = z.discriminatedUnion("type", [
|
|
1076
|
+
...STORED.map((own) => {
|
|
1077
|
+
const fallback = DEFAULTS[own.type.value];
|
|
1078
|
+
return z.strictObject({ ...BASE, ...own, ...(fallback ? { default: fallback.optional() } : {}) });
|
|
1079
|
+
}),
|
|
1080
|
+
z.strictObject({ ...BASE, type: z.literal("lookup"), via: ruled, field: ruled, target: z.unknown().optional() }),
|
|
1081
|
+
z.strictObject({
|
|
1082
|
+
...BASE,
|
|
1083
|
+
type: z.literal("rollup"),
|
|
1084
|
+
over: z.union([text, z.array(ruled)]),
|
|
1085
|
+
of: text.optional(),
|
|
1086
|
+
fn: word,
|
|
1087
|
+
where: filterTree.optional(),
|
|
1088
|
+
within: ruled.optional(),
|
|
1089
|
+
orderBy: z.strictObject({ field: text, direction: word }).optional(),
|
|
1090
|
+
groupBy: z.union([z.strictObject({ option: text }), z.strictObject({ time: text, bucket: word })]).optional(),
|
|
1091
|
+
headline: z.union([z.literal("total"), z.strictObject({ part: text })]).optional(),
|
|
1092
|
+
result: resultSchema,
|
|
1093
|
+
freshness: word.optional(),
|
|
1094
|
+
}),
|
|
1095
|
+
z.strictObject({
|
|
1096
|
+
...BASE,
|
|
1097
|
+
type: z.literal("formula"),
|
|
1098
|
+
from: text,
|
|
1099
|
+
rule: rule.optional(),
|
|
1100
|
+
result: resultSchema,
|
|
1101
|
+
ratio: z.strictObject({ numerator: text, denominator: text }).optional(),
|
|
1102
|
+
freshness: word.optional(),
|
|
1103
|
+
}),
|
|
1104
|
+
]);
|
|
1105
|
+
function kindOf(value) {
|
|
1106
|
+
if (value === null)
|
|
1107
|
+
return "null";
|
|
1108
|
+
if (Array.isArray(value))
|
|
1109
|
+
return "a list";
|
|
1110
|
+
switch (typeof value) {
|
|
1111
|
+
case "string":
|
|
1112
|
+
return "text";
|
|
1113
|
+
case "number":
|
|
1114
|
+
return "a number";
|
|
1115
|
+
case "boolean":
|
|
1116
|
+
return "true or false";
|
|
1117
|
+
case "object":
|
|
1118
|
+
return "an object";
|
|
1119
|
+
default:
|
|
1120
|
+
return typeof value;
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
const EXPECTED = {
|
|
1124
|
+
string: "text",
|
|
1125
|
+
number: "a number",
|
|
1126
|
+
boolean: "true or false",
|
|
1127
|
+
object: "an object",
|
|
1128
|
+
array: "a list",
|
|
1129
|
+
null: "null",
|
|
1130
|
+
};
|
|
1131
|
+
function shapeWhy(issue, type) {
|
|
1132
|
+
const path = issue.path.map(String).join(".");
|
|
1133
|
+
switch (issue.code) {
|
|
1134
|
+
case "invalid_type":
|
|
1135
|
+
if (issue.input === undefined)
|
|
1136
|
+
return `${path} is missing`;
|
|
1137
|
+
return `${path} must be ${EXPECTED[issue.expected] ?? issue.expected}, not ${kindOf(issue.input)}`;
|
|
1138
|
+
case "unrecognized_keys":
|
|
1139
|
+
return path === "" ? `"${issue.keys[0]}" is not a setting of a ${type} field` : `"${issue.keys[0]}" is not a setting of ${path}`;
|
|
1140
|
+
case "invalid_union":
|
|
1141
|
+
return issue.input === undefined ? `${path} is missing` : `${path} matches none of its forms`;
|
|
1142
|
+
case "invalid_value":
|
|
1143
|
+
return `${path} must be ${issue.values.map(String).join(" or ")}`;
|
|
1144
|
+
case "custom":
|
|
1145
|
+
return issue.message.startsWith("is ") ? `${path} ${issue.message}` : `${path}: ${issue.message}`;
|
|
1146
|
+
default:
|
|
1147
|
+
return `${path}: ${issue.message}`;
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
function checkRawData(key, f) {
|
|
1151
|
+
const stack = [[f, key, 1]];
|
|
1152
|
+
while (stack.length > 0) {
|
|
1153
|
+
const [value, path, depth] = stack.pop();
|
|
1154
|
+
if (typeof value === "function" || typeof value === "symbol" || typeof value === "bigint")
|
|
1155
|
+
throw new FieldDefinitionError(key, `${path} is not plain data — a definition must be something a row can hold`);
|
|
1156
|
+
if (!value || typeof value !== "object")
|
|
1157
|
+
continue;
|
|
1158
|
+
if (depth > FIELD_SPEC_MAX_DEPTH)
|
|
1159
|
+
throw new FieldDefinitionError(key, `the definition nests deeper than ${FIELD_SPEC_MAX_DEPTH} levels`);
|
|
1160
|
+
const proto = Object.getPrototypeOf(value);
|
|
1161
|
+
if (!Array.isArray(value) && proto !== Object.prototype && proto !== null)
|
|
1162
|
+
throw new FieldDefinitionError(key, `${path} is not plain data — a definition must be something a row can hold`);
|
|
1163
|
+
const entries = Object.entries(value);
|
|
1164
|
+
for (let i = entries.length - 1; i >= 0; i -= 1) {
|
|
1165
|
+
const [k, v] = entries[i];
|
|
1166
|
+
stack.push([v, `${path}.${k}`, depth + 1]);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
export function checkRawSpec(key, f) {
|
|
1171
|
+
if (!f || typeof f !== "object" || Array.isArray(f))
|
|
1172
|
+
throw new FieldDefinitionError(key, `a definition is an object, not ${kindOf(f)}`);
|
|
1173
|
+
checkRawData(key, f);
|
|
1174
|
+
}
|
|
1175
|
+
export function checkStrictShape(key, f) {
|
|
1176
|
+
const parsed = fieldSpecSchema.safeParse(f, { reportInput: true });
|
|
1177
|
+
if (!parsed.success) {
|
|
1178
|
+
const type = String(f.type);
|
|
1179
|
+
throw new FieldDefinitionError(key, shapeWhy(parsed.error.issues[0], type));
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
export function checkSpecShape(key, f) {
|
|
1183
|
+
checkRawSpec(key, f);
|
|
1184
|
+
checkStrictShape(key, f);
|
|
1185
|
+
}
|
package/fields/dist/types.d.ts
CHANGED
|
@@ -565,7 +565,7 @@ export type FieldDefOf<K extends FieldType> = Extract<FieldDef, {
|
|
|
565
565
|
type: K;
|
|
566
566
|
}>;
|
|
567
567
|
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
|
|
568
|
-
export type FieldSpec = DistributiveOmit<FieldDef, "key" | "identity" | "subtitle" | "subtitleThrough" | "subtitleTargets" | "rename" | "entity">;
|
|
568
|
+
export type FieldSpec = DistributiveOmit<FieldDef, "key" | "identity" | "subtitle" | "subtitleThrough" | "subtitleTargets" | "rename" | "entity" | "avatar">;
|
|
569
569
|
type StoredOfScalar<D extends {
|
|
570
570
|
type: string;
|
|
571
571
|
}> = D extends {
|
|
@@ -113,8 +113,8 @@ const view = evaluateView(
|
|
|
113
113
|
},
|
|
114
114
|
},
|
|
115
115
|
{
|
|
116
|
-
// A key the registry does not
|
|
117
|
-
fieldOf: (key) => (key
|
|
116
|
+
// A key the registry does not hold — constructor or toString included — is skipped, never an error.
|
|
117
|
+
fieldOf: (key) => (Object.hasOwn(DEAL_FIELDS, key) ? DEAL_FIELDS[key as DealKey] : undefined),
|
|
118
118
|
read: (deal, key) => deal[key as keyof Deal],
|
|
119
119
|
},
|
|
120
120
|
{ conventions }
|
|
@@ -47,6 +47,19 @@ assert.throws(
|
|
|
47
47
|
);
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
+
### Its shape
|
|
51
|
+
|
|
52
|
+
A definition is checked for its shape before its rules: every key is one its type declares, every value of the right kind, a label that is not blank, and nothing nested deeper than 408 levels. A refusal names the key. A field reused from another registry may keep the stamps `defineFields` gave it there (its key, its entity, its place on the line); `defineFields` stamps it again for its new registry, and never makes it a second identity.
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const colourful = JSON.parse('{ "type": "text", "label": "Title", "colour": "red" }');
|
|
56
|
+
|
|
57
|
+
assert.throws(() => checkFieldSpec("title", colourful), {
|
|
58
|
+
name: "FieldDefinitionError",
|
|
59
|
+
message: 'fields: "title": "colour" is not a setting of a text field',
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
50
63
|
### Options
|
|
51
64
|
|
|
52
65
|
- No two options of a field share a `value`, and an option's `icon`, when stated, is not blank.
|
|
@@ -220,7 +233,7 @@ These hold a lookup, a rollup and a formula.
|
|
|
220
233
|
- Over a relation that names `kinds`, a rollup takes no filter, order, breakdown, `first`, distinct count or second hop.
|
|
221
234
|
- A lookup names its far `field` and goes through a sibling `one` relation that names `kind` (`via`). Its far definition is never written: `defineIndex` fills it in.
|
|
222
235
|
- A formula's `rule` is typed against its siblings and produces what its `result` says. A ratio formula gives a percent from two number fields of this registry. No formula reads itself through another.
|
|
223
|
-
- A formula may name a SQL function as its rule only in a registry written in code
|
|
236
|
+
- A formula may name a SQL function as its rule only in a registry written in code that passes `allowSql: true`.
|
|
224
237
|
|
|
225
238
|
```ts
|
|
226
239
|
assert.throws(
|
|
@@ -412,7 +425,7 @@ assert.throws(() => checkRegistries({ company: sumsTitles, deal: deals }), /cann
|
|
|
412
425
|
|
|
413
426
|
A definition written in code is checked by `defineFields`, and the compiler holds its shape before that. One that arrives as data, such as a custom field a user adds and you keep in a row, has no compiler behind it. Run `checkFieldSpec` on it before you use it, with its siblings, so it is held to the same bar. A closed word it does not know is refused, never guessed at.
|
|
414
427
|
|
|
415
|
-
A definition from data may not name a SQL function as a formula's rule: that runs as your database, so only a registry written in code may,
|
|
428
|
+
A definition from data may not name a SQL function as a formula's rule: that runs as your database, so only a registry written in code may, when it passes `allowSql: true`.
|
|
416
429
|
|
|
417
430
|
```ts
|
|
418
431
|
const stored = '{ "type": "rollup", "label": "Open deals", "over": "deals", "fn": "tally", "result": { "type": "number" } }';
|
|
@@ -439,6 +452,7 @@ assert.throws(() => checkFieldSpec("health", sqlRule, companies), /a definition
|
|
|
439
452
|
const inCode = defineFields({
|
|
440
453
|
entity: "company",
|
|
441
454
|
identity: "name",
|
|
455
|
+
allowSql: true,
|
|
442
456
|
fields: { name: { type: "text", label: "Name" }, health: sqlRule },
|
|
443
457
|
});
|
|
444
458
|
assert.equal(inCode.health.type, "formula");
|
|
@@ -453,7 +467,7 @@ const wonWithUnknownSlot = JSON.parse(
|
|
|
453
467
|
assert.throws(() => checkFieldSpec("stage", wonWithUnknownSlot), /option "won"'s slot "done" is not one of: success, error/);
|
|
454
468
|
```
|
|
455
469
|
|
|
456
|
-
|
|
470
|
+
`defineFields` and `contributeFields` refuse a SQL formula unless you pass `allowSql: true`, so a registry you build from rows needs nothing.
|
|
457
471
|
|
|
458
472
|
```ts
|
|
459
473
|
assert.throws(
|
|
@@ -461,7 +475,6 @@ assert.throws(
|
|
|
461
475
|
defineFields({
|
|
462
476
|
entity: "company",
|
|
463
477
|
identity: "name",
|
|
464
|
-
allowSql: false,
|
|
465
478
|
fields: { name: { type: "text", label: "Name" }, health: sqlRule },
|
|
466
479
|
}),
|
|
467
480
|
/a definition that arrives as data cannot name one/
|