simpler-forms 0.1.0-alpha.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/LICENSE.md +21 -0
- package/README.md +111 -0
- package/dist/src/decorators/index.d.ts +4 -0
- package/dist/src/decorators/index.js +7 -0
- package/dist/src/decorators/index.js.map +1 -0
- package/dist/src/decorators/meta.d.ts +22 -0
- package/dist/src/decorators/meta.js +35 -0
- package/dist/src/decorators/meta.js.map +1 -0
- package/dist/src/decorators/shared.d.ts +49 -0
- package/dist/src/decorators/shared.js +149 -0
- package/dist/src/decorators/shared.js.map +1 -0
- package/dist/src/decorators/ui.d.ts +41 -0
- package/dist/src/decorators/ui.js +106 -0
- package/dist/src/decorators/ui.js.map +1 -0
- package/dist/src/decorators/validation.d.ts +25 -0
- package/dist/src/decorators/validation.js +91 -0
- package/dist/src/decorators/validation.js.map +1 -0
- package/dist/src/emitter.d.ts +15 -0
- package/dist/src/emitter.js +224 -0
- package/dist/src/emitter.js.map +1 -0
- package/dist/src/emitters/block-index.d.ts +4 -0
- package/dist/src/emitters/block-index.js +19 -0
- package/dist/src/emitters/block-index.js.map +1 -0
- package/dist/src/emitters/block-ui.d.ts +18 -0
- package/dist/src/emitters/block-ui.js +203 -0
- package/dist/src/emitters/block-ui.js.map +1 -0
- package/dist/src/emitters/field-occurrences.d.ts +18 -0
- package/dist/src/emitters/field-occurrences.js +237 -0
- package/dist/src/emitters/field-occurrences.js.map +1 -0
- package/dist/src/emitters/overlay.d.ts +16 -0
- package/dist/src/emitters/overlay.js +449 -0
- package/dist/src/emitters/overlay.js.map +1 -0
- package/dist/src/emitters/override-condition.d.ts +12 -0
- package/dist/src/emitters/override-condition.js +25 -0
- package/dist/src/emitters/override-condition.js.map +1 -0
- package/dist/src/index.d.ts +51 -0
- package/dist/src/index.js +55 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lib.d.ts +361 -0
- package/dist/src/lib.js +194 -0
- package/dist/src/lib.js.map +1 -0
- package/dist/src/linter.d.ts +1 -0
- package/dist/src/linter.js +337 -0
- package/dist/src/linter.js.map +1 -0
- package/dist/src/model.d.ts +134 -0
- package/dist/src/model.js +203 -0
- package/dist/src/model.js.map +1 -0
- package/dist/src/public.d.ts +33 -0
- package/dist/src/public.js +40 -0
- package/dist/src/public.js.map +1 -0
- package/dist/src/validate.d.ts +15 -0
- package/dist/src/validate.js +737 -0
- package/dist/src/validate.js.map +1 -0
- package/lib/main.tsp +4 -0
- package/lib/meta.tsp +33 -0
- package/lib/types.tsp +134 -0
- package/lib/ui.tsp +78 -0
- package/lib/validation.tsp +134 -0
- package/package.json +54 -0
- package/src/decorators/index.ts +6 -0
- package/src/decorators/meta.ts +52 -0
- package/src/decorators/shared.ts +187 -0
- package/src/decorators/ui.ts +192 -0
- package/src/decorators/validation.ts +220 -0
- package/src/emitter.ts +272 -0
- package/src/emitters/block-index.ts +24 -0
- package/src/emitters/block-ui.ts +254 -0
- package/src/emitters/field-occurrences.ts +345 -0
- package/src/emitters/overlay.ts +596 -0
- package/src/emitters/override-condition.ts +37 -0
- package/src/index.ts +59 -0
- package/src/lib.ts +195 -0
- package/src/linter.ts +380 -0
- package/src/model.ts +400 -0
- package/src/public.ts +94 -0
- package/src/validate.ts +887 -0
package/src/emitter.ts
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EmitContext,
|
|
3
|
+
Model,
|
|
4
|
+
Namespace,
|
|
5
|
+
Program,
|
|
6
|
+
} from "@typespec/compiler";
|
|
7
|
+
import { emitFile, resolvePath } from "@typespec/compiler";
|
|
8
|
+
import {
|
|
9
|
+
$onEmit as emitJsonSchema,
|
|
10
|
+
findBaseUri,
|
|
11
|
+
getId,
|
|
12
|
+
} from "@typespec/json-schema";
|
|
13
|
+
import { allBlocks, type Block } from "./model.js";
|
|
14
|
+
import { reportDiagnostic } from "./lib.js";
|
|
15
|
+
import { blockSchemaRef } from "./decorators/index.js";
|
|
16
|
+
import { emitModelOverlay, emitSchemaOverlay } from "./emitters/overlay.js";
|
|
17
|
+
import { emitBlockUi } from "./emitters/block-ui.js";
|
|
18
|
+
import { emitBlockIndex } from "./emitters/block-index.js";
|
|
19
|
+
|
|
20
|
+
export interface FormSpecOptions {
|
|
21
|
+
/**
|
|
22
|
+
* Optional base URI to make `$id` and `$ref` absolute. Omitted by default: where
|
|
23
|
+
* the artifacts are hosted is a per-consumer decision, and a block's identity is
|
|
24
|
+
* its `@Meta.question.id`, not its URI.
|
|
25
|
+
*/
|
|
26
|
+
"base-uri"?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const STAGING = ".json-schema";
|
|
30
|
+
|
|
31
|
+
type Json = Record<string, any>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Wraps `@typespec/json-schema`: it owns every keyword derivable from the type graph,
|
|
35
|
+
* this emitter owns everything derived from our decorators, and the two are composed
|
|
36
|
+
* here rather than by a downstream build step.
|
|
37
|
+
*/
|
|
38
|
+
export async function $onEmit(
|
|
39
|
+
context: EmitContext<FormSpecOptions>,
|
|
40
|
+
): Promise<void> {
|
|
41
|
+
const program = context.program;
|
|
42
|
+
if (program.compilerOptions.noEmit) return;
|
|
43
|
+
|
|
44
|
+
const outDir = context.emitterOutputDir;
|
|
45
|
+
const stagingDir = resolvePath(outDir, STAGING);
|
|
46
|
+
|
|
47
|
+
// 1. Let the stock emitter produce the base schemas into a staging directory.
|
|
48
|
+
await emitJsonSchema({
|
|
49
|
+
...context,
|
|
50
|
+
emitterOutputDir: stagingDir,
|
|
51
|
+
options: { "file-type": "json" },
|
|
52
|
+
} as any);
|
|
53
|
+
|
|
54
|
+
// 2. Read them back and index by $id and by file name.
|
|
55
|
+
const byId = new Map<string, Json>();
|
|
56
|
+
const byFile = new Map<string, Json>();
|
|
57
|
+
for (const name of await listJson(program, stagingDir)) {
|
|
58
|
+
const raw = await program.host.readFile(resolvePath(stagingDir, name));
|
|
59
|
+
const doc = JSON.parse(raw.text) as Json;
|
|
60
|
+
byFile.set(name, doc);
|
|
61
|
+
if (doc.$id) byId.set(doc.$id, doc);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const baseUri = context.options?.["base-uri"];
|
|
65
|
+
const write = (rel: string, value: unknown) =>
|
|
66
|
+
emitFile(program, {
|
|
67
|
+
path: resolvePath(outDir, rel),
|
|
68
|
+
content: JSON.stringify(value, null, 2) + "\n",
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const blocks = allBlocks(program);
|
|
72
|
+
const modelsByName = indexModels(program.getGlobalNamespaceType());
|
|
73
|
+
const refToDir = new Map(
|
|
74
|
+
blocks.map((b) => [
|
|
75
|
+
blockSchemaRef(b.id),
|
|
76
|
+
b.kind === "question" ? `question-bank/${b.id}` : `forms/${b.id}`,
|
|
77
|
+
]),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
for (const block of blocks) {
|
|
81
|
+
const dir = refToDir.get(blockSchemaRef(block.id))!;
|
|
82
|
+
const stock = byId.get(resolvedSchemaId(program, block));
|
|
83
|
+
if (!stock) {
|
|
84
|
+
reportDiagnostic(program, {
|
|
85
|
+
code: "block-not-emitted",
|
|
86
|
+
target: block.model,
|
|
87
|
+
format: {
|
|
88
|
+
name: block.model.name || block.id,
|
|
89
|
+
id: resolvedSchemaId(program, block),
|
|
90
|
+
available: [...byId.keys()].sort().join(", ") || "none",
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 3. Inline refs to unpublished declarations into $defs, as the goldens do.
|
|
97
|
+
const defs: Json = {};
|
|
98
|
+
let schema = resolveRefs(stock, {
|
|
99
|
+
byFile,
|
|
100
|
+
refToDir,
|
|
101
|
+
defs,
|
|
102
|
+
baseUri,
|
|
103
|
+
from: dir,
|
|
104
|
+
program,
|
|
105
|
+
modelsByName,
|
|
106
|
+
});
|
|
107
|
+
if (Object.keys(defs).length) schema = { ...schema, $defs: defs };
|
|
108
|
+
|
|
109
|
+
// 4. Fold in the conditional requiredness only we can know about.
|
|
110
|
+
const overlay = emitSchemaOverlay(program, block);
|
|
111
|
+
if (overlay) schema = mergeSchema(schema, overlay);
|
|
112
|
+
|
|
113
|
+
await write(`${dir}/schema.json`, schema);
|
|
114
|
+
await write(`${dir}/ui.json`, emitBlockUi(program, block));
|
|
115
|
+
await write(`${dir}/index.json`, emitBlockIndex(program, block));
|
|
116
|
+
|
|
117
|
+
if (block.kind === "form") {
|
|
118
|
+
await write(`${dir}/manifest.json`, {
|
|
119
|
+
contract: "resolved-form-package/v1",
|
|
120
|
+
form: block.meta,
|
|
121
|
+
artifacts: {
|
|
122
|
+
"schema.json": "generated",
|
|
123
|
+
"ui.json": "generated",
|
|
124
|
+
"index.json": "generated",
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The `$id` the stock emitter actually wrote for a block.
|
|
133
|
+
*
|
|
134
|
+
* We hand it a relative id via `publishAs`, but it resolves that against the nearest
|
|
135
|
+
* enclosing `@jsonSchema("<base>")` before writing the file -- see `#getDeclId` and
|
|
136
|
+
* `idWithBaseURI` in `@typespec/json-schema`'s json-schema-emitter. Indexing the staged
|
|
137
|
+
* output by `$id` therefore only matches if we resolve the id the same way, so this
|
|
138
|
+
* mirrors that composition using the two accessors that library exports.
|
|
139
|
+
*/
|
|
140
|
+
function resolvedSchemaId(program: Program, block: Block): string {
|
|
141
|
+
const relative = getId(program, block.model) ?? blockSchemaRef(block.id);
|
|
142
|
+
const baseUri = findBaseUri(program, block.model);
|
|
143
|
+
return baseUri ? new URL(relative, baseUri).href : relative;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function listJson(program: any, dir: string): Promise<string[]> {
|
|
147
|
+
try {
|
|
148
|
+
const entries = await program.host.readDir(dir);
|
|
149
|
+
return entries.filter((e: string) => e.endsWith(".json"));
|
|
150
|
+
} catch {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
interface RefCtx {
|
|
156
|
+
byFile: Map<string, Json>;
|
|
157
|
+
refToDir: Map<string, string>;
|
|
158
|
+
defs: Json;
|
|
159
|
+
baseUri?: string;
|
|
160
|
+
from: string;
|
|
161
|
+
program: EmitContext<FormSpecOptions>["program"];
|
|
162
|
+
modelsByName: Map<string, Model>;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Rewrite every `$ref`: published blocks keep their identity, others become `$defs`. */
|
|
166
|
+
function resolveRefs(node: any, ctx: RefCtx, seen = new Set<string>()): any {
|
|
167
|
+
if (Array.isArray(node)) return node.map((n) => resolveRefs(n, ctx, seen));
|
|
168
|
+
if (!node || typeof node !== "object") return node;
|
|
169
|
+
|
|
170
|
+
const out: Json = {};
|
|
171
|
+
for (const [k, v] of Object.entries(node)) {
|
|
172
|
+
if (k === "$ref" && typeof v === "string" && !v.startsWith("#")) {
|
|
173
|
+
if (ctx.refToDir.has(v)) {
|
|
174
|
+
out[k] = ctx.baseUri
|
|
175
|
+
? `${ctx.baseUri.replace(/\/$/, "")}/${v}`
|
|
176
|
+
: relativeRef(ctx.from, v, ctx);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const target = ctx.byFile.get(v);
|
|
180
|
+
if (!target) {
|
|
181
|
+
out[k] = v;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const name = v
|
|
185
|
+
.replace(/\.json$/, "")
|
|
186
|
+
.split("/")
|
|
187
|
+
.pop()!;
|
|
188
|
+
if (!seen.has(name)) {
|
|
189
|
+
seen.add(name);
|
|
190
|
+
const { $schema, $id, ...body } = target;
|
|
191
|
+
const overlay = ctx.modelsByName.has(name)
|
|
192
|
+
? emitModelOverlay(ctx.program, ctx.modelsByName.get(name)!)
|
|
193
|
+
: undefined;
|
|
194
|
+
ctx.defs[name] = resolveRefs(
|
|
195
|
+
overlay ? mergeSchema(body, overlay) : body,
|
|
196
|
+
ctx,
|
|
197
|
+
seen,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
out[k] = `#/$defs/${name}`;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (k === "$id" && typeof v === "string" && ctx.baseUri) {
|
|
204
|
+
out[k] = `${ctx.baseUri.replace(/\/$/, "")}/${v}`;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
out[k] = resolveRefs(v, ctx, seen);
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function indexModels(root: Namespace): Map<string, Model> {
|
|
213
|
+
const indexed = new Map<string, Model>();
|
|
214
|
+
const duplicates = new Set<string>();
|
|
215
|
+
const visit = (namespace: Namespace): void => {
|
|
216
|
+
for (const model of namespace.models.values()) {
|
|
217
|
+
if (indexed.has(model.name)) duplicates.add(model.name);
|
|
218
|
+
else indexed.set(model.name, model);
|
|
219
|
+
}
|
|
220
|
+
for (const child of namespace.namespaces.values()) visit(child);
|
|
221
|
+
};
|
|
222
|
+
visit(root);
|
|
223
|
+
for (const name of duplicates) indexed.delete(name);
|
|
224
|
+
return indexed;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** A path from one block's directory to another's schema, for base-less artifacts. */
|
|
228
|
+
function relativeRef(fromDir: string, ref: string, ctx: RefCtx): string {
|
|
229
|
+
const toDir = ctx.refToDir.get(ref)!;
|
|
230
|
+
const up = fromDir
|
|
231
|
+
.split("/")
|
|
232
|
+
.map(() => "..")
|
|
233
|
+
.join("/");
|
|
234
|
+
return `${up}/${toDir}/schema.json`;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Fold the overlay over the stock schema.
|
|
239
|
+
*
|
|
240
|
+
* `allOf` appends, because both sides contribute branches. `properties` merges one level
|
|
241
|
+
* down, because the overlay patches individual properties and replacing the map wholesale
|
|
242
|
+
* would delete every property it did not mention. Anything else the overlay states, it
|
|
243
|
+
* states outright.
|
|
244
|
+
*/
|
|
245
|
+
function mergeSchema(a: Json, b: Json): Json {
|
|
246
|
+
const out = { ...a };
|
|
247
|
+
for (const [key, value] of Object.entries(b)) {
|
|
248
|
+
if (key === "allOf" && Array.isArray(out.allOf)) {
|
|
249
|
+
out.allOf = [...out.allOf, ...value];
|
|
250
|
+
} else if (
|
|
251
|
+
key === "properties" &&
|
|
252
|
+
isObject(out.properties) &&
|
|
253
|
+
isObject(value)
|
|
254
|
+
) {
|
|
255
|
+
out.properties = { ...out.properties };
|
|
256
|
+
for (const [name, patch] of Object.entries(value as Json)) {
|
|
257
|
+
const existing = out.properties[name];
|
|
258
|
+
out.properties[name] = isObject(existing)
|
|
259
|
+
? mergeSchema(existing as Json, patch as Json)
|
|
260
|
+
: patch;
|
|
261
|
+
}
|
|
262
|
+
} else if (isObject(out[key]) && isObject(value)) {
|
|
263
|
+
out[key] = mergeSchema(out[key], value);
|
|
264
|
+
} else {
|
|
265
|
+
out[key] = value;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const isObject = (value: unknown): value is Json =>
|
|
272
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Program } from "@typespec/compiler";
|
|
2
|
+
import { Block, blockAncestry } from "../model.js";
|
|
3
|
+
import { emitFieldOccurrences } from "./field-occurrences.js";
|
|
4
|
+
|
|
5
|
+
/** One catalogue entry per block, mirroring the consuming site's CatalogItem. */
|
|
6
|
+
export function emitBlockIndex(
|
|
7
|
+
program: Program,
|
|
8
|
+
block: Block,
|
|
9
|
+
): Record<string, unknown> {
|
|
10
|
+
return {
|
|
11
|
+
id: block.id,
|
|
12
|
+
kind: block.kind,
|
|
13
|
+
classification:
|
|
14
|
+
block.kind === "question" ? block.classification : undefined,
|
|
15
|
+
name: block.label ?? block.id,
|
|
16
|
+
description: block.doc ?? "",
|
|
17
|
+
tags: block.tags,
|
|
18
|
+
...(block.entity ? { entity: block.entity } : {}),
|
|
19
|
+
...(block.responseRole ? { responseRole: block.responseRole } : {}),
|
|
20
|
+
...(block.kind === "question"
|
|
21
|
+
? { composes: blockAncestry(program, block.model).slice(1) }
|
|
22
|
+
: { fieldOccurrences: emitFieldOccurrences(program, block) }),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import type { Program } from "@typespec/compiler";
|
|
2
|
+
import { getDoc } from "@typespec/compiler";
|
|
3
|
+
import {
|
|
4
|
+
AtomicCondition,
|
|
5
|
+
Block,
|
|
6
|
+
Condition,
|
|
7
|
+
childBlock,
|
|
8
|
+
orderedProps,
|
|
9
|
+
propEnabledWhen,
|
|
10
|
+
propLabel,
|
|
11
|
+
propReadOnly,
|
|
12
|
+
propReadOnlyWhen,
|
|
13
|
+
propSection,
|
|
14
|
+
propVisibleWhen,
|
|
15
|
+
propWidget,
|
|
16
|
+
} from "../model.js";
|
|
17
|
+
import { normalizedOverrideEnabledWhen } from "./override-condition.js";
|
|
18
|
+
|
|
19
|
+
const conditionSchema = (
|
|
20
|
+
condition: AtomicCondition,
|
|
21
|
+
): Record<string, unknown> =>
|
|
22
|
+
condition.operator === "in"
|
|
23
|
+
? { enum: condition.values }
|
|
24
|
+
: condition.operator === "countAtLeast"
|
|
25
|
+
? { type: "array", minItems: condition.minimum }
|
|
26
|
+
: condition.operator === "present"
|
|
27
|
+
? {
|
|
28
|
+
not: {
|
|
29
|
+
anyOf: [
|
|
30
|
+
{ type: "null" },
|
|
31
|
+
{ const: "" },
|
|
32
|
+
{ type: "array", maxItems: 0 },
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
: { const: condition.value };
|
|
37
|
+
|
|
38
|
+
/** A condition over the block root, used only for the bounded cross-field disjunction. */
|
|
39
|
+
const rootConditionSchema = (
|
|
40
|
+
condition: AtomicCondition,
|
|
41
|
+
): Record<string, unknown> => {
|
|
42
|
+
let schema = conditionSchema(condition);
|
|
43
|
+
for (const step of [...condition.sourcePath].reverse()) {
|
|
44
|
+
schema = { properties: { [step]: schema }, required: [step] };
|
|
45
|
+
}
|
|
46
|
+
return schema;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const jsonFormsCondition = (condition: Condition): Record<string, unknown> =>
|
|
50
|
+
condition.operator === "any"
|
|
51
|
+
? {
|
|
52
|
+
scope: "#",
|
|
53
|
+
schema: { anyOf: condition.predicates.map(rootConditionSchema) },
|
|
54
|
+
}
|
|
55
|
+
: {
|
|
56
|
+
scope: `#/${condition.sourcePath.map((step) => `properties/${step}`).join("/")}`,
|
|
57
|
+
schema: conditionSchema(condition),
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export interface UiNode {
|
|
61
|
+
type: string;
|
|
62
|
+
scope?: string;
|
|
63
|
+
label?: string;
|
|
64
|
+
text?: string;
|
|
65
|
+
elements?: UiNode[];
|
|
66
|
+
options?: Record<string, unknown>;
|
|
67
|
+
rule?: Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type Overrides = Record<string, Record<string, unknown>>;
|
|
71
|
+
|
|
72
|
+
/** Convert a canonical JSON Forms scope into the dotted path addressed by @UI.overrides. */
|
|
73
|
+
const dataPathFromScope = (scope: string): string | undefined => {
|
|
74
|
+
if (!scope.startsWith("#/properties/")) return undefined;
|
|
75
|
+
const tokens = scope.slice(2).split("/");
|
|
76
|
+
const path: string[] = [];
|
|
77
|
+
for (let index = 0; index < tokens.length;) {
|
|
78
|
+
if (tokens[index] !== "properties" || !tokens[index + 1]) return undefined;
|
|
79
|
+
path.push(tokens[index + 1]);
|
|
80
|
+
index += 2;
|
|
81
|
+
if (tokens[index] === "items") index += 1;
|
|
82
|
+
}
|
|
83
|
+
return path.join(".");
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** Emit the portable JSON Forms equivalent of a form-scoped enabledWhen override. */
|
|
87
|
+
const overrideEnabledRule = (
|
|
88
|
+
override: Record<string, unknown>,
|
|
89
|
+
): Record<string, unknown> | undefined => {
|
|
90
|
+
const condition = normalizedOverrideEnabledWhen(override);
|
|
91
|
+
if (!condition) return undefined;
|
|
92
|
+
const scope = `#/${condition.sourcePath.map((step) => `properties/${step}`).join("/")}`;
|
|
93
|
+
const schema =
|
|
94
|
+
condition.operator === "in"
|
|
95
|
+
? { enum: condition.values }
|
|
96
|
+
: { const: condition.value };
|
|
97
|
+
return { effect: "ENABLE", condition: { scope, schema } };
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/** Apply occurrence-specific behavior after recursively composing canonical child UI trees. */
|
|
101
|
+
const applyOverrides = (node: UiNode, overrides: Overrides): UiNode => {
|
|
102
|
+
const out: UiNode = { ...node };
|
|
103
|
+
if (typeof node.scope === "string") {
|
|
104
|
+
const path = dataPathFromScope(node.scope);
|
|
105
|
+
const rule = path ? overrideEnabledRule(overrides[path] ?? {}) : undefined;
|
|
106
|
+
if (rule && node.rule) {
|
|
107
|
+
throw new Error(
|
|
108
|
+
`@UI.overrides enabledWhen collides with intrinsic UI behavior at ${path}`,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
if (rule) out.rule = rule;
|
|
112
|
+
}
|
|
113
|
+
if (node.elements)
|
|
114
|
+
out.elements = node.elements.map((child) =>
|
|
115
|
+
applyOverrides(child, overrides),
|
|
116
|
+
);
|
|
117
|
+
return out;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/** Re-prefix every Control scope so a child's tree sits under `propName`. */
|
|
121
|
+
export function rescopeUi(node: UiNode, propName: string): UiNode {
|
|
122
|
+
const out: UiNode = { ...node };
|
|
123
|
+
if (typeof out.scope === "string" && out.scope.startsWith("#/")) {
|
|
124
|
+
out.scope = `#/properties/${propName}/${out.scope.slice(2)}`;
|
|
125
|
+
}
|
|
126
|
+
const condition = out.rule?.condition as
|
|
127
|
+
{ scope?: unknown; schema?: unknown } | undefined;
|
|
128
|
+
if (
|
|
129
|
+
typeof condition?.scope === "string" &&
|
|
130
|
+
condition.scope.startsWith("#/")
|
|
131
|
+
) {
|
|
132
|
+
out.rule = {
|
|
133
|
+
...out.rule,
|
|
134
|
+
condition: {
|
|
135
|
+
...condition,
|
|
136
|
+
scope: `#/properties/${propName}/${condition.scope.slice(2)}`,
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
} else if (condition?.scope === "#" && condition.schema !== undefined) {
|
|
140
|
+
out.rule = {
|
|
141
|
+
...out.rule,
|
|
142
|
+
condition: {
|
|
143
|
+
...condition,
|
|
144
|
+
schema: {
|
|
145
|
+
properties: { [propName]: condition.schema },
|
|
146
|
+
required: [propName],
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
if (out.elements)
|
|
152
|
+
out.elements = out.elements.map((c) => rescopeUi(c, propName));
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The canonical UI artifact for one block. Scopes are relative to this block's own
|
|
158
|
+
* root, so it renders standalone; children are incorporated and re-scoped.
|
|
159
|
+
*/
|
|
160
|
+
export function emitBlockUi(program: Program, block: Block): UiNode {
|
|
161
|
+
// A single-value question renders as one Control at its own root.
|
|
162
|
+
if (block.scalar) {
|
|
163
|
+
const node: UiNode = { type: "Control", scope: "#" };
|
|
164
|
+
if (block.label) node.label = block.label;
|
|
165
|
+
return node;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const nodeForProperty = (
|
|
169
|
+
prop: ReturnType<typeof orderedProps>[number],
|
|
170
|
+
): UiNode => {
|
|
171
|
+
const child = childBlock(program, prop);
|
|
172
|
+
if (child && !child.scalar) {
|
|
173
|
+
return rescopeUi(emitBlockUi(program, child), prop.name);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const node: UiNode = {
|
|
177
|
+
type: "Control",
|
|
178
|
+
scope: `#/properties/${prop.name}`,
|
|
179
|
+
};
|
|
180
|
+
const label = propLabel(program, prop);
|
|
181
|
+
if (label) node.label = label;
|
|
182
|
+
|
|
183
|
+
const widget = propWidget(program, prop);
|
|
184
|
+
if (widget) node.options = { ...(node.options ?? {}), widget };
|
|
185
|
+
if (propReadOnly(program, prop))
|
|
186
|
+
node.options = { ...(node.options ?? {}), readonly: true };
|
|
187
|
+
|
|
188
|
+
const conds = propVisibleWhen(program, prop);
|
|
189
|
+
if (conds.length === 1) {
|
|
190
|
+
const c = conds[0];
|
|
191
|
+
node.rule = {
|
|
192
|
+
effect: "SHOW",
|
|
193
|
+
condition: jsonFormsCondition(c),
|
|
194
|
+
};
|
|
195
|
+
} else {
|
|
196
|
+
const enabled = propEnabledWhen(program, prop);
|
|
197
|
+
if (enabled.length === 1) {
|
|
198
|
+
const c = enabled[0];
|
|
199
|
+
node.rule = {
|
|
200
|
+
effect: "ENABLE",
|
|
201
|
+
condition: jsonFormsCondition(c),
|
|
202
|
+
};
|
|
203
|
+
} else {
|
|
204
|
+
const readOnly = propReadOnlyWhen(program, prop);
|
|
205
|
+
if (readOnly.length === 1) {
|
|
206
|
+
const c = readOnly[0];
|
|
207
|
+
node.rule = {
|
|
208
|
+
effect: "DISABLE",
|
|
209
|
+
condition: jsonFormsCondition(c),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return node;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const props = orderedProps(program, block);
|
|
219
|
+
const elements: UiNode[] = [];
|
|
220
|
+
const documentedStaticSections = block.sections
|
|
221
|
+
? [...block.sections.members.values()].filter(
|
|
222
|
+
(member) =>
|
|
223
|
+
getDoc(program, member) &&
|
|
224
|
+
!props.some(
|
|
225
|
+
(prop) => propSection(program, prop)?.name === member.name,
|
|
226
|
+
),
|
|
227
|
+
)
|
|
228
|
+
: [];
|
|
229
|
+
|
|
230
|
+
if (documentedStaticSections.length && block.sections) {
|
|
231
|
+
const emitted = new Set<string>();
|
|
232
|
+
for (const member of block.sections.members.values()) {
|
|
233
|
+
const description = getDoc(program, member);
|
|
234
|
+
const members = props.filter(
|
|
235
|
+
(prop) => propSection(program, prop)?.name === member.name,
|
|
236
|
+
);
|
|
237
|
+
if (!members.length && description)
|
|
238
|
+
elements.push({ type: "Label", text: description });
|
|
239
|
+
for (const prop of members) {
|
|
240
|
+
elements.push(nodeForProperty(prop));
|
|
241
|
+
emitted.add(prop.name);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
for (const prop of props)
|
|
245
|
+
if (!emitted.has(prop.name)) elements.push(nodeForProperty(prop));
|
|
246
|
+
} else {
|
|
247
|
+
elements.push(...props.map(nodeForProperty));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const group: UiNode = { type: "Group", elements };
|
|
251
|
+
if (block.label) group.label = block.label;
|
|
252
|
+
else if (block.doc) group.label = block.doc;
|
|
253
|
+
return applyOverrides(group, block.overrides);
|
|
254
|
+
}
|