simpler-forms 0.1.0-alpha.1 → 0.1.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/README.md +221 -76
- package/dist/src/casing.d.ts +43 -0
- package/dist/src/casing.js +146 -0
- package/dist/src/casing.js.map +1 -0
- package/dist/src/decorators/index.d.ts +1 -0
- package/dist/src/decorators/index.js +1 -0
- package/dist/src/decorators/index.js.map +1 -1
- package/dist/src/decorators/mapping.d.ts +11 -0
- package/dist/src/decorators/mapping.js +22 -0
- package/dist/src/decorators/mapping.js.map +1 -0
- package/dist/src/decorators/meta.d.ts +13 -3
- package/dist/src/decorators/meta.js +31 -4
- package/dist/src/decorators/meta.js.map +1 -1
- package/dist/src/decorators/ui.d.ts +14 -12
- package/dist/src/decorators/ui.js +20 -43
- package/dist/src/decorators/ui.js.map +1 -1
- package/dist/src/emitter.d.ts +8 -0
- package/dist/src/emitter.js +48 -13
- package/dist/src/emitter.js.map +1 -1
- package/dist/src/emitters/block-ui.d.ts +14 -3
- package/dist/src/emitters/block-ui.js +81 -119
- package/dist/src/emitters/block-ui.js.map +1 -1
- package/dist/src/emitters/layout-ui.d.ts +12 -0
- package/dist/src/emitters/layout-ui.js +225 -0
- package/dist/src/emitters/layout-ui.js.map +1 -0
- package/dist/src/emitters/overlay.d.ts +3 -6
- package/dist/src/emitters/overlay.js +3 -86
- package/dist/src/emitters/overlay.js.map +1 -1
- package/dist/src/forms.d.ts +23 -0
- package/dist/src/forms.js +54 -0
- package/dist/src/forms.js.map +1 -0
- package/dist/src/index.d.ts +15 -6
- package/dist/src/index.js +12 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/layout.d.ts +50 -0
- package/dist/src/layout.js +397 -0
- package/dist/src/layout.js.map +1 -0
- package/dist/src/lib.d.ts +485 -39
- package/dist/src/lib.js +215 -15
- package/dist/src/lib.js.map +1 -1
- package/dist/src/linter.js +2 -41
- package/dist/src/linter.js.map +1 -1
- package/dist/src/mapping-from.d.ts +20 -0
- package/dist/src/mapping-from.js +149 -0
- package/dist/src/mapping-from.js.map +1 -0
- package/dist/src/mapping.d.ts +58 -0
- package/dist/src/mapping.js +209 -0
- package/dist/src/mapping.js.map +1 -0
- package/dist/src/model.d.ts +69 -17
- package/dist/src/model.js +65 -19
- package/dist/src/model.js.map +1 -1
- package/dist/src/public.d.ts +20 -4
- package/dist/src/public.js +28 -4
- package/dist/src/public.js.map +1 -1
- package/dist/src/validate.d.ts +2 -1
- package/dist/src/validate.js +180 -58
- package/dist/src/validate.js.map +1 -1
- package/lib/main.tsp +1 -0
- package/lib/mapping.tsp +71 -0
- package/lib/meta.tsp +23 -4
- package/lib/types.tsp +13 -65
- package/lib/ui.tsp +34 -17
- package/package.json +7 -1
- package/src/casing.ts +172 -0
- package/src/decorators/index.ts +1 -0
- package/src/decorators/mapping.ts +57 -0
- package/src/decorators/meta.ts +50 -11
- package/src/decorators/ui.ts +43 -59
- package/src/emitter.ts +59 -13
- package/src/emitters/block-ui.ts +83 -135
- package/src/emitters/layout-ui.ts +280 -0
- package/src/emitters/overlay.ts +3 -103
- package/src/forms.ts +82 -0
- package/src/index.ts +12 -3
- package/src/layout.ts +592 -0
- package/src/lib.ts +221 -15
- package/src/linter.ts +1 -42
- package/src/mapping-from.ts +193 -0
- package/src/mapping.ts +351 -0
- package/src/model.ts +170 -37
- package/src/public.ts +50 -6
- package/src/validate.ts +188 -59
- package/dist/src/emitters/override-condition.d.ts +0 -12
- package/dist/src/emitters/override-condition.js +0 -25
- package/dist/src/emitters/override-condition.js.map +0 -1
- package/src/emitters/override-condition.ts +0 -37
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import type { Diagnostic, Program } from "@typespec/compiler";
|
|
2
|
+
import { getDoc } from "@typespec/compiler";
|
|
3
|
+
import { createDiagnostic } from "../lib.js";
|
|
4
|
+
import {
|
|
5
|
+
type FieldNode,
|
|
6
|
+
type LayoutNode,
|
|
7
|
+
type ResolvedLayout,
|
|
8
|
+
behind,
|
|
9
|
+
isList,
|
|
10
|
+
} from "../layout.js";
|
|
11
|
+
import {
|
|
12
|
+
modelLabel,
|
|
13
|
+
propHelpText,
|
|
14
|
+
propLabel,
|
|
15
|
+
propWidget,
|
|
16
|
+
readBlock,
|
|
17
|
+
} from "../model.js";
|
|
18
|
+
import {
|
|
19
|
+
type UiNode,
|
|
20
|
+
emitBlockUi,
|
|
21
|
+
jsonFormsCondition,
|
|
22
|
+
layoutSource,
|
|
23
|
+
propertyUi,
|
|
24
|
+
rescopeUi,
|
|
25
|
+
} from "./block-ui.js";
|
|
26
|
+
|
|
27
|
+
const key = (path: string[]) => path.join(".");
|
|
28
|
+
|
|
29
|
+
const emitted = new WeakMap<
|
|
30
|
+
ResolvedLayout,
|
|
31
|
+
{ ui: UiNode; diagnostics: Diagnostic[] }
|
|
32
|
+
>();
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The JSON Forms UI for a layout. Each section is a top-level labelled Group. A field is laid
|
|
36
|
+
* out as its model lays it out, less whatever the layout places elsewhere or omits, with the
|
|
37
|
+
* entry's label, help text, widget and condition on top.
|
|
38
|
+
*/
|
|
39
|
+
export function emitLayoutUi(
|
|
40
|
+
program: Program,
|
|
41
|
+
layout: ResolvedLayout,
|
|
42
|
+
): { ui: UiNode; diagnostics: Diagnostic[] } {
|
|
43
|
+
let result = emitted.get(layout);
|
|
44
|
+
if (!result) emitted.set(layout, (result = build(program, layout)));
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function build(
|
|
49
|
+
program: Program,
|
|
50
|
+
layout: ResolvedLayout,
|
|
51
|
+
): { ui: UiNode; diagnostics: Diagnostic[] } {
|
|
52
|
+
const excluded = [...layout.placed, ...layout.omitted];
|
|
53
|
+
/** What is placed elsewhere or omitted from inside the field at `path`. */
|
|
54
|
+
const excludedUnder = (path: string[]) =>
|
|
55
|
+
excluded.filter((k) => k.startsWith(`${key(path)}.`));
|
|
56
|
+
|
|
57
|
+
const help = (entry: FieldNode["entry"]) =>
|
|
58
|
+
propHelpText(program, entry) ?? getDoc(program, entry);
|
|
59
|
+
|
|
60
|
+
/** Apply what an entry says about how its field is presented. */
|
|
61
|
+
const present = (
|
|
62
|
+
ui: UiNode,
|
|
63
|
+
node: LayoutNode,
|
|
64
|
+
fallbackLabel?: string,
|
|
65
|
+
): UiNode => {
|
|
66
|
+
const out: UiNode = { ...ui };
|
|
67
|
+
const label = propLabel(program, node.entry) ?? fallbackLabel;
|
|
68
|
+
if (label) out.label = label;
|
|
69
|
+
const description = help(node.entry);
|
|
70
|
+
const widget = propWidget(program, node.entry);
|
|
71
|
+
if (description || widget)
|
|
72
|
+
out.options = {
|
|
73
|
+
...(out.options ?? {}),
|
|
74
|
+
...(description ? { description } : {}),
|
|
75
|
+
...(widget ? { widget } : {}),
|
|
76
|
+
};
|
|
77
|
+
// A layout's condition replaces the field's own for this occurrence.
|
|
78
|
+
if (node.condition)
|
|
79
|
+
out.rule = {
|
|
80
|
+
effect: node.condition.effect,
|
|
81
|
+
condition: jsonFormsCondition(node.condition.condition),
|
|
82
|
+
};
|
|
83
|
+
return out;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** The fields of `node`'s type that no entry lists, in their model's order. */
|
|
87
|
+
const remaining = (node: FieldNode): UiNode[] => {
|
|
88
|
+
const inner = behind(node.prop.type);
|
|
89
|
+
if (!inner) return [];
|
|
90
|
+
const list = isList(node.prop.type);
|
|
91
|
+
const base = list ? node.path : node.scopeBase;
|
|
92
|
+
const steps = list ? [] : node.path.slice(node.scopeBase.length);
|
|
93
|
+
const own = emitBlockUi(program, layoutSource(program, inner));
|
|
94
|
+
// A single-value question has no fields to lay out.
|
|
95
|
+
if (!own.elements) return [];
|
|
96
|
+
return own.elements
|
|
97
|
+
.map((child) => rescopeAll(child, steps))
|
|
98
|
+
.map((child) => prune(child, base, excludedUnder(node.path)))
|
|
99
|
+
.filter((child): child is UiNode => !!child);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const fieldUi = (node: FieldNode): UiNode[] => {
|
|
103
|
+
const own = rescopeAll(
|
|
104
|
+
propertyUi(program, node.prop),
|
|
105
|
+
node.path.slice(node.scopeBase.length, -1),
|
|
106
|
+
);
|
|
107
|
+
let ui: UiNode | undefined;
|
|
108
|
+
if (node.listed) {
|
|
109
|
+
const elements = [...node.listed.flatMap(nodeUi), ...remaining(node)];
|
|
110
|
+
if (isList(node.prop.type)) {
|
|
111
|
+
ui = {
|
|
112
|
+
...own,
|
|
113
|
+
options: {
|
|
114
|
+
...(own.options ?? {}),
|
|
115
|
+
detail: { type: "Group", elements },
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
} else if (own.type === "Group") {
|
|
119
|
+
ui = { ...own, elements };
|
|
120
|
+
} else {
|
|
121
|
+
// A field that lays out as one Control becomes a Group once a layout lists entries in it.
|
|
122
|
+
const inner = behind(node.prop.type);
|
|
123
|
+
const label =
|
|
124
|
+
propLabel(program, node.prop) ??
|
|
125
|
+
(inner ? modelLabel(program, inner) : undefined);
|
|
126
|
+
const description = getDoc(program, node.prop);
|
|
127
|
+
ui = {
|
|
128
|
+
type: "Group",
|
|
129
|
+
...(label ? { label } : {}),
|
|
130
|
+
elements,
|
|
131
|
+
...(description ? { options: { description } } : {}),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
ui = prune(own, node.scopeBase, excludedUnder(node.path));
|
|
136
|
+
}
|
|
137
|
+
if (!ui) return [];
|
|
138
|
+
ui = present(ui, node);
|
|
139
|
+
if (node.flatten && ui.type === "Group" && !isList(node.prop.type)) {
|
|
140
|
+
const rule = ui.rule;
|
|
141
|
+
return (ui.elements ?? []).map((child) =>
|
|
142
|
+
rule && !child.rule ? { ...child, rule } : child,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return [ui];
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const nodeUi = (node: LayoutNode): UiNode[] =>
|
|
149
|
+
node.kind === "field"
|
|
150
|
+
? fieldUi(node)
|
|
151
|
+
: [
|
|
152
|
+
present(
|
|
153
|
+
{ type: "Group", elements: node.children.flatMap(nodeUi) },
|
|
154
|
+
node,
|
|
155
|
+
node.entry.name,
|
|
156
|
+
),
|
|
157
|
+
];
|
|
158
|
+
|
|
159
|
+
/** A section is always a labelled Group, so a renderer shows it as one. */
|
|
160
|
+
const sectionUi = (node: LayoutNode): UiNode[] => {
|
|
161
|
+
const flat = node.kind === "field" ? { ...node, flatten: false } : node;
|
|
162
|
+
return nodeUi(flat).map((ui) => {
|
|
163
|
+
if (ui.type === "Group" && ui.label) return ui;
|
|
164
|
+
if (ui.type === "Group") return { ...ui, label: node.entry.name };
|
|
165
|
+
// The section carries the control's rule and description: they are about the section.
|
|
166
|
+
const { rule, ...control } = ui;
|
|
167
|
+
const { description, ...options } = control.options ?? {};
|
|
168
|
+
if (control.options)
|
|
169
|
+
if (Object.keys(options).length) control.options = options;
|
|
170
|
+
else delete control.options;
|
|
171
|
+
const label =
|
|
172
|
+
propLabel(program, node.entry) ??
|
|
173
|
+
(node.kind === "field"
|
|
174
|
+
? (propLabel(program, node.prop) ?? node.prop.name)
|
|
175
|
+
: node.entry.name);
|
|
176
|
+
return {
|
|
177
|
+
type: "Group",
|
|
178
|
+
label,
|
|
179
|
+
elements: [control],
|
|
180
|
+
...(rule ? { rule } : {}),
|
|
181
|
+
...(description ? { options: { description } } : {}),
|
|
182
|
+
};
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const block = readBlock(program, layout.presented);
|
|
187
|
+
const label =
|
|
188
|
+
modelLabel(program, layout.layout) ?? block?.label ?? block?.doc;
|
|
189
|
+
const ui: UiNode = {
|
|
190
|
+
type: "Group",
|
|
191
|
+
elements: layout.sections.flatMap(sectionUi),
|
|
192
|
+
...(label ? { label } : {}),
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
return { ui, diagnostics: duplicateLabels(layout, ui) };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Re-prefix a node's scopes with each of `steps`, outermost first. */
|
|
199
|
+
function rescopeAll(node: UiNode, steps: string[]): UiNode {
|
|
200
|
+
return [...steps].reverse().reduce(rescopeUi, node);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** The data path a Control's scope addresses, relative to its root. */
|
|
204
|
+
function dataPath(scope: string): string[] {
|
|
205
|
+
const tokens = scope.replace(/^#\/?/, "").split("/").filter(Boolean);
|
|
206
|
+
const path: string[] = [];
|
|
207
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
208
|
+
if (tokens[i] === "properties" && tokens[i + 1]) path.push(tokens[++i]);
|
|
209
|
+
}
|
|
210
|
+
return path;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Drop every Control whose path is excluded, and any Group left empty by that. `base` is the
|
|
215
|
+
* path the node's scopes are relative to.
|
|
216
|
+
*/
|
|
217
|
+
function prune(
|
|
218
|
+
node: UiNode,
|
|
219
|
+
base: string[],
|
|
220
|
+
excluded: string[],
|
|
221
|
+
): UiNode | undefined {
|
|
222
|
+
if (!excluded.length) return node;
|
|
223
|
+
const out: UiNode = { ...node };
|
|
224
|
+
if (node.scope) {
|
|
225
|
+
const path = key([...base, ...dataPath(node.scope)]);
|
|
226
|
+
if (excluded.some((e) => path === e || path.startsWith(`${e}.`)))
|
|
227
|
+
return undefined;
|
|
228
|
+
const detail = node.options?.detail as UiNode | undefined;
|
|
229
|
+
if (detail) {
|
|
230
|
+
const pruned = prune(detail, path ? path.split(".") : base, excluded);
|
|
231
|
+
out.options = {
|
|
232
|
+
...node.options,
|
|
233
|
+
detail: pruned ?? { ...detail, elements: [] },
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (node.elements) {
|
|
238
|
+
out.elements = node.elements
|
|
239
|
+
.map((child) => prune(child, base, excluded))
|
|
240
|
+
.filter((child): child is UiNode => !!child);
|
|
241
|
+
if (node.elements.length && !out.elements.length) return undefined;
|
|
242
|
+
}
|
|
243
|
+
return out;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Sibling entries that render under one label, which an applicant cannot tell apart. */
|
|
247
|
+
function duplicateLabels(layout: ResolvedLayout, ui: UiNode): Diagnostic[] {
|
|
248
|
+
const out: Diagnostic[] = [];
|
|
249
|
+
const name = (node: UiNode) =>
|
|
250
|
+
node.label ?? (node.scope ? dataPath(node.scope).slice(-1)[0] : undefined);
|
|
251
|
+
const visit = (node: UiNode, where: string) => {
|
|
252
|
+
const seen = new Map<string, UiNode>();
|
|
253
|
+
for (const child of node.elements ?? []) {
|
|
254
|
+
const label = name(child);
|
|
255
|
+
const first = label === undefined ? undefined : seen.get(label);
|
|
256
|
+
if (label !== undefined && first)
|
|
257
|
+
out.push(
|
|
258
|
+
createDiagnostic({
|
|
259
|
+
code: "layout-duplicate-label",
|
|
260
|
+
target: layout.layout,
|
|
261
|
+
format: {
|
|
262
|
+
where,
|
|
263
|
+
label,
|
|
264
|
+
first: describe(first),
|
|
265
|
+
second: describe(child),
|
|
266
|
+
},
|
|
267
|
+
}),
|
|
268
|
+
);
|
|
269
|
+
else if (label !== undefined) seen.set(label, child);
|
|
270
|
+
visit(child, child.label ?? where);
|
|
271
|
+
const detail = child.options?.detail as UiNode | undefined;
|
|
272
|
+
if (detail) visit(detail, label ?? where);
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
visit(ui, layout.layout.name);
|
|
276
|
+
return out;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const describe = (node: UiNode) =>
|
|
280
|
+
node.scope ?? (node.label ? `the group "${node.label}"` : "a group");
|
package/src/emitters/overlay.ts
CHANGED
|
@@ -19,12 +19,9 @@ import {
|
|
|
19
19
|
} from "../model.js";
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* The parts of a block's JSON Schema that `@typespec/json-schema` cannot produce
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* presentation a *form* puts on a member of a question it composes, from `@UI.overrides` --
|
|
26
|
-
* SF-424 calls the authorized representative's phone "AOR Telephone Number" where the
|
|
27
|
-
* question calls it "Telephone Number", and that belongs to the form.
|
|
22
|
+
* The parts of a block's JSON Schema that `@typespec/json-schema` cannot produce: what the
|
|
23
|
+
* `@Validation.*` decorators require, and the help text, read-only status and choice
|
|
24
|
+
* contracts the `@UI.*` decorators carry into the schema.
|
|
28
25
|
*
|
|
29
26
|
* Everything else -- types, constraints, enums, arrays, `required`, `$ref` composition,
|
|
30
27
|
* `extends` as `allOf` -- comes from the stock emitter. The build merges this over it.
|
|
@@ -39,7 +36,6 @@ export function emitSchemaOverlay(
|
|
|
39
36
|
program,
|
|
40
37
|
orderedProps(program, block),
|
|
41
38
|
);
|
|
42
|
-
const patches = overriddenPresentation(block);
|
|
43
39
|
const readOnly = readOnlyAnnotations(program, model);
|
|
44
40
|
const helpText = helpTextAnnotations(program, model);
|
|
45
41
|
const constraints = constraintAnnotations(program, model);
|
|
@@ -49,7 +45,6 @@ export function emitSchemaOverlay(
|
|
|
49
45
|
const alternatives = atLeastOneAnnotations(program, model);
|
|
50
46
|
const parts = [
|
|
51
47
|
conditionals,
|
|
52
|
-
patches,
|
|
53
48
|
readOnly,
|
|
54
49
|
helpText,
|
|
55
50
|
constraints,
|
|
@@ -474,101 +469,6 @@ function conditionSchema(
|
|
|
474
469
|
return nested;
|
|
475
470
|
}
|
|
476
471
|
|
|
477
|
-
/**
|
|
478
|
-
* A form's own label and help text for a member of a question it composes.
|
|
479
|
-
*
|
|
480
|
-
* The member's schema is behind a `$ref`, so the patch goes beside it: a second `allOf`
|
|
481
|
-
* branch naming the member. A consumer that resolves the reference sees the question's
|
|
482
|
-
* wording, then the form's on top -- which is the order that makes the form win.
|
|
483
|
-
*/
|
|
484
|
-
function overriddenPresentation(
|
|
485
|
-
block: Block,
|
|
486
|
-
): Record<string, unknown> | undefined {
|
|
487
|
-
const patches: Record<string, Record<string, unknown>> = {};
|
|
488
|
-
for (const [path, override] of Object.entries(block.overrides)) {
|
|
489
|
-
const presentation: Record<string, unknown> = {};
|
|
490
|
-
if (typeof override.label === "string") presentation.title = override.label;
|
|
491
|
-
if (typeof override.helpText === "string")
|
|
492
|
-
presentation.description = override.helpText;
|
|
493
|
-
if (override.readOnly === true) presentation.readOnly = true;
|
|
494
|
-
if (!Object.keys(presentation).length) continue;
|
|
495
|
-
|
|
496
|
-
const [head, ...rest] = path.split(".");
|
|
497
|
-
if (!rest.length && override.readOnly !== true) {
|
|
498
|
-
// A root property is declared in this block, so `@UI.label` already covers it.
|
|
499
|
-
continue;
|
|
500
|
-
}
|
|
501
|
-
const property = modelProperty(block.model as Model, head);
|
|
502
|
-
const child = property ? childModel(property.type) : undefined;
|
|
503
|
-
let patch =
|
|
504
|
-
rest.length && child
|
|
505
|
-
? nestPresentation(child.model, rest, presentation)
|
|
506
|
-
: rest.length
|
|
507
|
-
? nest(rest, presentation)
|
|
508
|
-
: presentation;
|
|
509
|
-
if (rest.length && child?.repeated) patch = { items: patch };
|
|
510
|
-
patches[head] = merge(patches[head] ?? {}, patch);
|
|
511
|
-
}
|
|
512
|
-
if (!Object.keys(patches).length) return undefined;
|
|
513
|
-
return {
|
|
514
|
-
properties: Object.fromEntries(
|
|
515
|
-
Object.entries(patches).map(([name, patch]) => [
|
|
516
|
-
name,
|
|
517
|
-
{ allOf: [patch] },
|
|
518
|
-
]),
|
|
519
|
-
),
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
/** Find one own or inherited property, preferring the derived declaration. */
|
|
524
|
-
function modelProperty(model: Model, name: string): ModelProperty | undefined {
|
|
525
|
-
for (
|
|
526
|
-
let current: Model | undefined = model;
|
|
527
|
-
current;
|
|
528
|
-
current = current.baseModel
|
|
529
|
-
) {
|
|
530
|
-
const property = current.properties.get(name);
|
|
531
|
-
if (property) return property;
|
|
532
|
-
}
|
|
533
|
-
return undefined;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
/**
|
|
537
|
-
* Nest one form-scoped presentation patch through the addressed model shape.
|
|
538
|
-
*
|
|
539
|
-
* Arrays require an `items` boundary in JSON Schema even though the authoring
|
|
540
|
-
* path deliberately omits array syntax (`reports.total`, not `reports[].total`).
|
|
541
|
-
*/
|
|
542
|
-
function nestPresentation(
|
|
543
|
-
model: Model,
|
|
544
|
-
steps: string[],
|
|
545
|
-
leaf: Record<string, unknown>,
|
|
546
|
-
): Record<string, unknown> {
|
|
547
|
-
const [step, ...rest] = steps;
|
|
548
|
-
if (!step) return leaf;
|
|
549
|
-
const property = modelProperty(model, step);
|
|
550
|
-
const child = property ? childModel(property.type) : undefined;
|
|
551
|
-
let nested =
|
|
552
|
-
rest.length && child
|
|
553
|
-
? nestPresentation(child.model, rest, leaf)
|
|
554
|
-
: rest.length
|
|
555
|
-
? nest(rest, leaf)
|
|
556
|
-
: leaf;
|
|
557
|
-
if (rest.length && child?.repeated) nested = { items: nested };
|
|
558
|
-
return { properties: { [step]: nested } };
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
/** `["phone"], {title}` becomes `{properties: {phone: {title}}}`. */
|
|
562
|
-
function nest(
|
|
563
|
-
steps: string[],
|
|
564
|
-
leaf: Record<string, unknown>,
|
|
565
|
-
): Record<string, unknown> {
|
|
566
|
-
return steps.reduceRight<Record<string, unknown>>(
|
|
567
|
-
(inner, step) => ({ properties: { [step]: inner } }),
|
|
568
|
-
leaf,
|
|
569
|
-
);
|
|
570
|
-
}
|
|
571
|
-
|
|
572
472
|
function merge(
|
|
573
473
|
a: Record<string, unknown>,
|
|
574
474
|
b: Record<string, unknown>,
|
package/src/forms.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Diagnostic, Model, Program } from "@typespec/compiler";
|
|
2
|
+
import { createDiagnostic } from "./lib.js";
|
|
3
|
+
import {
|
|
4
|
+
type Block,
|
|
5
|
+
type FormMeta,
|
|
6
|
+
allFormMetas,
|
|
7
|
+
allLayouts,
|
|
8
|
+
modelFormMeta,
|
|
9
|
+
readBlock,
|
|
10
|
+
} from "./model.js";
|
|
11
|
+
|
|
12
|
+
/** One form an applicant opens, published under its id. */
|
|
13
|
+
export interface PublishedForm {
|
|
14
|
+
id: string;
|
|
15
|
+
/** The model carrying `@Meta.form`, whose schema the form publishes. */
|
|
16
|
+
model: Model;
|
|
17
|
+
meta: FormMeta;
|
|
18
|
+
block: Block;
|
|
19
|
+
/** The `@UI.layout` presenting the form, when its own fields do not lay it out. */
|
|
20
|
+
layout?: Model;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Every `@Meta.form` that can be published, and the problems that stop the others.
|
|
25
|
+
*
|
|
26
|
+
* A form's fields lay it out, unless one `@UI.layout` model presents it. A layout of a
|
|
27
|
+
* model with no `@Meta.form` is reported, and so is a form with two layouts, which is not
|
|
28
|
+
* published. Two forms with one id are reported as `duplicate-block-id`.
|
|
29
|
+
*/
|
|
30
|
+
export function resolveForms(program: Program): {
|
|
31
|
+
forms: PublishedForm[];
|
|
32
|
+
problems: Diagnostic[];
|
|
33
|
+
} {
|
|
34
|
+
const forms: PublishedForm[] = [];
|
|
35
|
+
const problems: Diagnostic[] = [];
|
|
36
|
+
|
|
37
|
+
const layoutsOf = new Map<Model, Model[]>();
|
|
38
|
+
for (const [layout, presented] of allLayouts(program)) {
|
|
39
|
+
if (!modelFormMeta(program, presented)) {
|
|
40
|
+
problems.push(
|
|
41
|
+
createDiagnostic({
|
|
42
|
+
code: "layout-not-form",
|
|
43
|
+
target: layout,
|
|
44
|
+
format: { name: layout.name, presented: presented.name },
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
layoutsOf.set(presented, [...(layoutsOf.get(presented) ?? []), layout]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const ids = new Set<string>();
|
|
53
|
+
for (const [model, meta] of allFormMetas(program)) {
|
|
54
|
+
const layouts = layoutsOf.get(model) ?? [];
|
|
55
|
+
if (layouts.length > 1) {
|
|
56
|
+
for (const target of layouts)
|
|
57
|
+
problems.push(
|
|
58
|
+
createDiagnostic({
|
|
59
|
+
code: "layout-duplicate",
|
|
60
|
+
target,
|
|
61
|
+
format: {
|
|
62
|
+
form: model.name,
|
|
63
|
+
first: layouts[0].name,
|
|
64
|
+
second: layouts[1].name,
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (ids.has(meta.id)) continue;
|
|
71
|
+
ids.add(meta.id);
|
|
72
|
+
forms.push({
|
|
73
|
+
id: meta.id,
|
|
74
|
+
model,
|
|
75
|
+
meta,
|
|
76
|
+
block: readBlock(program, model)!,
|
|
77
|
+
...(layouts.length ? { layout: layouts[0] } : {}),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { forms, problems };
|
|
82
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -14,15 +14,18 @@ export const $decorators = {
|
|
|
14
14
|
"SimplerForms.Meta": {
|
|
15
15
|
question: d.$question,
|
|
16
16
|
form: d.$form,
|
|
17
|
+
tags: d.$tags,
|
|
18
|
+
entities: d.$entities,
|
|
17
19
|
tag: d.$tag,
|
|
18
20
|
role: d.$role,
|
|
19
21
|
},
|
|
20
22
|
"SimplerForms.UI": {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
layout: d.$layout,
|
|
24
|
+
field: d.$field,
|
|
25
|
+
flatten: d.$flatten,
|
|
24
26
|
label: d.$label,
|
|
25
27
|
helpText: d.$helpText,
|
|
28
|
+
widgets: d.$widgets,
|
|
26
29
|
widget: d.$widget,
|
|
27
30
|
encodedCheckboxGroup: d.$encodedCheckboxGroup,
|
|
28
31
|
order: d.$order,
|
|
@@ -58,4 +61,10 @@ export const $decorators = {
|
|
|
58
61
|
evaluationOrder: d.$evaluationOrder,
|
|
59
62
|
totals: d.$totals,
|
|
60
63
|
},
|
|
64
|
+
"SimplerForms.Mapping": {
|
|
65
|
+
targets: d.$targets,
|
|
66
|
+
to: d.$to,
|
|
67
|
+
path: d.$path,
|
|
68
|
+
from: d.$from,
|
|
69
|
+
},
|
|
61
70
|
};
|