@golemui/schemas 1.5.1 → 1.6.0-rc.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.
@@ -0,0 +1,1514 @@
1
+ const ye = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
2
+ function _(t) {
3
+ return typeof t != "string" ? JSON.stringify(t) : JSON.stringify(t).replace(/\.(?=\d)/g, '." + "');
4
+ }
5
+ function z(t, e) {
6
+ return e.reduce((r, s, i) => {
7
+ const n = i === 0 ? "" : "?.";
8
+ return ye.test(s) ? `${r}${n === "" ? "." : n}${s}` : `${r}${n}[${_(s)}]`;
9
+ }, t);
10
+ }
11
+ function oe(t) {
12
+ for (let e = t; e; e = e.parent) {
13
+ const r = e.parent;
14
+ if (r?.type === "array" && e.path === `${r.path}.items`)
15
+ return e;
16
+ }
17
+ }
18
+ function K(t, e) {
19
+ const r = oe(t);
20
+ if (r === void 0)
21
+ return z("$form", e.split("."));
22
+ const s = e.slice(r.path.length + 1);
23
+ return s === "" ? "$item" : z("$item", s.split("."));
24
+ }
25
+ function I(t, e) {
26
+ const r = e.map((s) => `${t} === ${_(s)}`);
27
+ return r.length === 1 ? r[0] : `(${r.join(" || ")})`;
28
+ }
29
+ const ae = /* @__PURE__ */ new Set([
30
+ "title",
31
+ "description",
32
+ "$comment",
33
+ "examples",
34
+ "default",
35
+ "deprecated"
36
+ ]);
37
+ function ge(t, e) {
38
+ if (typeof t == "boolean")
39
+ return { expression: String(t), vacuous: !1, reads: [] };
40
+ if (!j(t))
41
+ return { unsupported: "a value that is not a schema" };
42
+ const r = { vacuous: !1 }, s = ce(t, [], e, r);
43
+ if ("unsupported" in s)
44
+ return s;
45
+ const i = j(t.properties) ? Object.keys(t.properties) : [], n = [.../* @__PURE__ */ new Set([...i, ...pe(t)])];
46
+ return { expression: ue(s.tests), vacuous: r.vacuous, reads: n };
47
+ }
48
+ function ce(t, e, r, s) {
49
+ for (const [a, c] of Object.entries(t))
50
+ if (!(a === "properties" || a === "required" || ae.has(a) || a === "type" && c === "object"))
51
+ return { unsupported: a };
52
+ const i = pe(t), n = j(t.properties) ? t.properties : {}, o = [];
53
+ for (const [a, c] of Object.entries(n)) {
54
+ const u = [...e, a], p = r(u), d = be(c, u, r, s);
55
+ if ("unsupported" in d)
56
+ return d;
57
+ i.includes(a) ? o.push(
58
+ d.impliesPresence ? d.test : `${p} !== undefined && ${d.test}`
59
+ ) : d.test !== "true" && (o.push(`(${p} === undefined || ${d.test})`), s.vacuous = !0);
60
+ }
61
+ for (const a of i)
62
+ a in n || o.push(`${r([...e, a])} !== undefined`);
63
+ return { tests: o };
64
+ }
65
+ function be(t, e, r, s) {
66
+ if (t === !0)
67
+ return { test: "true", impliesPresence: !1 };
68
+ if (t === !1)
69
+ return { test: "false", impliesPresence: !0 };
70
+ if (!j(t))
71
+ return { unsupported: "a value that is not a schema" };
72
+ const i = r(e), n = [];
73
+ let o = !1;
74
+ for (const [a, c] of Object.entries(t))
75
+ if (!(a === "type" || a === "properties" || a === "required" || ae.has(a)))
76
+ if (a === "const" && x(c))
77
+ n.push(`${i} === ${_(c)}`), o ||= c !== null;
78
+ else if (a === "enum" && Array.isArray(c) && c.every(x))
79
+ n.push(I(i, c)), o ||= !c.includes(null);
80
+ else if (a === "not" && B(c) !== void 0)
81
+ n.push(`!(${I(i, B(c))})`);
82
+ else if (L[a] !== void 0 && typeof c == "number")
83
+ n.push(`${i} ${L[a]} ${c}`), o = !0;
84
+ else
85
+ return { unsupported: a };
86
+ if ("properties" in t || "required" in t) {
87
+ const a = ce(
88
+ { properties: t.properties, required: t.required },
89
+ e,
90
+ r,
91
+ s
92
+ );
93
+ if ("unsupported" in a)
94
+ return a;
95
+ n.push(...a.tests);
96
+ }
97
+ return { test: ue(n), impliesPresence: o };
98
+ }
99
+ const L = {
100
+ minimum: ">=",
101
+ maximum: "<=",
102
+ exclusiveMinimum: ">",
103
+ exclusiveMaximum: "<"
104
+ };
105
+ function B(t) {
106
+ if (!(!j(t) || Object.keys(t).length !== 1)) {
107
+ if ("const" in t && x(t.const))
108
+ return [t.const];
109
+ if (Array.isArray(t.enum) && t.enum.every(x))
110
+ return t.enum;
111
+ }
112
+ }
113
+ function ue(t) {
114
+ return t.length === 0 ? "true" : t.join(" && ");
115
+ }
116
+ function pe(t) {
117
+ return Array.isArray(t.required) ? t.required.filter((e) => typeof e == "string") : [];
118
+ }
119
+ function x(t) {
120
+ return t === null || typeof t == "string" || typeof t == "number" || typeof t == "boolean";
121
+ }
122
+ function j(t) {
123
+ return t !== null && typeof t == "object" && !Array.isArray(t);
124
+ }
125
+ function V(t) {
126
+ return "match" in t && !("when" in t);
127
+ }
128
+ const ve = {
129
+ $type: (t) => t.type,
130
+ $path: (t) => t.path,
131
+ $name: (t) => t.name,
132
+ $defName: (t) => t.defName,
133
+ $required: (t) => t.required,
134
+ $inRepeater: (t) => t.repeaterDepth > 0
135
+ };
136
+ function J(t, e) {
137
+ return Object.entries(t.match).every(([r, s]) => {
138
+ const i = ve[r], n = i ? i(e) : e.schema[r];
139
+ return Oe(s) ? Ne(n, s) : r === "$path" && typeof s == "string" && typeof n == "string" ? $e(s, n) : de(n, s);
140
+ });
141
+ }
142
+ function Oe(t) {
143
+ if (t === null || typeof t != "object" || Array.isArray(t))
144
+ return !1;
145
+ const e = Object.keys(t);
146
+ return e.length === 1 && ["$in", "$exists", "$regex"].includes(e[0]);
147
+ }
148
+ function Ne(t, e) {
149
+ return "$in" in e ? e.$in.some((r) => de(r, t)) : "$exists" in e ? t !== void 0 === e.$exists : typeof t == "string" && new RegExp(e.$regex).test(t);
150
+ }
151
+ function $e(t, e) {
152
+ const r = (i, n) => {
153
+ if (i.length === 0)
154
+ return n.length === 0;
155
+ const [o, ...a] = i;
156
+ return o === "**" ? n.some((c, u) => r(a, n.slice(u))) || r(a, []) : n.length === 0 ? !1 : (o === "*" || o === n[0]) && r(a, n.slice(1));
157
+ }, s = (i) => i === "" ? [] : i.split(".");
158
+ return r(s(t), s(e));
159
+ }
160
+ function de(t, e) {
161
+ return t === e || JSON.stringify(t) === JSON.stringify(e);
162
+ }
163
+ function R(t) {
164
+ return t.replace(/[_-]+/g, " ").replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/\s+/g, " ").trim().replace(/^./, (e) => e.toUpperCase());
165
+ }
166
+ function Ae(t) {
167
+ const { schema: e } = t;
168
+ if (Array.isArray(e.enum)) {
169
+ const r = Array.isArray(e.enumNames) ? e.enumNames : [], s = [];
170
+ return e.enum.forEach((i, n) => {
171
+ if (Y(i)) {
172
+ const o = r[n];
173
+ s.push({ label: typeof o == "string" ? o : G(i), value: i });
174
+ }
175
+ }), s;
176
+ }
177
+ for (const r of ["oneOf", "anyOf"]) {
178
+ const s = e[r];
179
+ if (M(s))
180
+ return s.filter((i) => Y(i.const)).map((i) => ({
181
+ label: typeof i.title == "string" ? i.title : G(i.const),
182
+ value: i.const
183
+ }));
184
+ }
185
+ }
186
+ function M(t) {
187
+ return Array.isArray(t) && t.length > 0 && t.every(we);
188
+ }
189
+ function we(t) {
190
+ return t !== null && typeof t == "object" && "const" in t;
191
+ }
192
+ function Y(t) {
193
+ return typeof t == "string" || typeof t == "number" || typeof t == "boolean";
194
+ }
195
+ function G(t) {
196
+ return typeof t == "string" ? R(t) : String(t);
197
+ }
198
+ const je = /* @__PURE__ */ new Set([
199
+ "minimum",
200
+ "exclusiveMinimum",
201
+ "minLength",
202
+ "minItems",
203
+ "minProperties",
204
+ "minContains"
205
+ ]), Se = /* @__PURE__ */ new Set([
206
+ "maximum",
207
+ "exclusiveMaximum",
208
+ "maxLength",
209
+ "maxItems",
210
+ "maxProperties",
211
+ "maxContains"
212
+ ]), Te = /* @__PURE__ */ new Set([
213
+ "const",
214
+ "pattern",
215
+ "format",
216
+ "multipleOf",
217
+ "prefixItems",
218
+ "oneOf",
219
+ "anyOf",
220
+ "not",
221
+ "contains"
222
+ ]), xe = /* @__PURE__ */ new Set(["properties", "patternProperties"]), S = /* @__PURE__ */ Symbol("conflict");
223
+ function Re(t, e) {
224
+ const r = {}, s = [];
225
+ let i;
226
+ for (const n of t) {
227
+ const o = ke(i, n.allowedTypes);
228
+ o === S ? s.push({ keyword: "type", pointer: n.pointer }) : i = o;
229
+ for (const [a, c] of Object.entries(n.schema)) {
230
+ if (a === "type")
231
+ continue;
232
+ if (!(a in r)) {
233
+ r[a] = c;
234
+ continue;
235
+ }
236
+ const u = De(a, r[a], c, e);
237
+ u === S ? s.push({ keyword: a, pointer: n.pointer }) : r[a] = u;
238
+ }
239
+ }
240
+ return { schema: r, allowedTypes: i, conflicts: s };
241
+ }
242
+ function qe(t) {
243
+ const e = typeof t.type == "string" ? [t.type] : Array.isArray(t.type) ? t.type : void 0;
244
+ if (e === void 0)
245
+ return;
246
+ const r = e.filter(Pe);
247
+ return r.includes("number") && !r.includes("integer") && r.splice(r.indexOf("number") + 1, 0, "integer"), r;
248
+ }
249
+ function Pe(t) {
250
+ return t === "string" || t === "number" || t === "integer" || t === "boolean" || t === "object" || t === "array" || t === "null";
251
+ }
252
+ function ke(t, e) {
253
+ if (e === void 0)
254
+ return t;
255
+ if (t === void 0)
256
+ return e;
257
+ const r = t.filter((s) => e.includes(s));
258
+ return r.length > 0 ? r : S;
259
+ }
260
+ function De(t, e, r, s) {
261
+ if (U(e, r))
262
+ return e;
263
+ if (typeof e == "number" && typeof r == "number") {
264
+ if (je.has(t))
265
+ return Math.max(e, r);
266
+ if (Se.has(t))
267
+ return Math.min(e, r);
268
+ }
269
+ if (t === "required" && Array.isArray(e) && Array.isArray(r))
270
+ return [...e, ...r.filter((i) => !e.includes(i))];
271
+ if (t === "enum" && Array.isArray(e) && Array.isArray(r)) {
272
+ const i = e.filter(
273
+ (n) => r.some((o) => U(n, o))
274
+ );
275
+ return i.length > 0 ? i : S;
276
+ }
277
+ return t === "uniqueItems" ? e === !0 || r === !0 : xe.has(t) && C(e) && C(r) ? Ee(e, r, s) : t === "items" ? fe(e, r, s) : Te.has(t) ? S : e;
278
+ }
279
+ function Ee(t, e, r) {
280
+ const s = { ...t };
281
+ for (const [i, n] of Object.entries(e))
282
+ s[i] = i in s ? fe(s[i], n, r) : n;
283
+ return s;
284
+ }
285
+ function fe(t, e, r) {
286
+ if (U(t, e))
287
+ return t;
288
+ const s = { allOf: [t, e] }, i = C(t) ? r.get(t) : void 0;
289
+ return i !== void 0 && r.set(s, i), s;
290
+ }
291
+ function C(t) {
292
+ return t !== null && typeof t == "object" && !Array.isArray(t);
293
+ }
294
+ function U(t, e) {
295
+ return t === e || JSON.stringify(t) === JSON.stringify(e);
296
+ }
297
+ function Ie(t) {
298
+ return t.replace(/~/g, "~0").replace(/\//g, "~1");
299
+ }
300
+ function le(t) {
301
+ return t.replace(/~1/g, "/").replace(/~0/g, "~");
302
+ }
303
+ function g(t, ...e) {
304
+ return t + e.map((r) => `/${Ie(String(r))}`).join("");
305
+ }
306
+ function Me(t, e) {
307
+ const r = t.indexOf("#"), s = r === -1 ? t : t.slice(0, r), i = r === -1 ? "" : t.slice(r + 1);
308
+ if (s !== "" && s !== e.$id)
309
+ return { error: `Only refs inside the same document are supported, found "${t}".` };
310
+ if (i !== "" && !i.startsWith("/"))
311
+ return { error: `Anchor refs are not supported, found "${t}".` };
312
+ let n = e;
313
+ const o = [];
314
+ for (const a of i === "" ? [] : i.slice(1).split("/")) {
315
+ let c;
316
+ try {
317
+ c = le(decodeURIComponent(a));
318
+ } catch {
319
+ return { error: `"${t}" is not a valid URI fragment.` };
320
+ }
321
+ if (n === null || typeof n != "object" || !Object.prototype.hasOwnProperty.call(n, c))
322
+ return { error: `"${t}" points to nothing in the document.` };
323
+ n = n[c], o.push(c);
324
+ }
325
+ return { target: n, pointer: g("", ...o) };
326
+ }
327
+ function Ce(t) {
328
+ const e = /(?:^|\/)(?:\$defs|definitions|components\/schemas)\/([^/]+)$/.exec(t);
329
+ return e ? le(e[1]) : void 0;
330
+ }
331
+ const Ue = [
332
+ "$defs",
333
+ "definitions",
334
+ "$schema",
335
+ "$id",
336
+ "$anchor",
337
+ "$dynamicAnchor"
338
+ ];
339
+ function w(t, e, r, s) {
340
+ if (t === !1)
341
+ return;
342
+ if (t === !0 || t === void 0)
343
+ return { schema: {}, nullable: !1, conditionals: [], refTrail: [...r] };
344
+ if (t === null || typeof t != "object" || Array.isArray(t)) {
345
+ s.report({
346
+ severity: "error",
347
+ code: "invalid-schema",
348
+ message: "This value is not a JSON Schema object or boolean.",
349
+ pointer: e
350
+ });
351
+ return;
352
+ }
353
+ He(t, e, s.pointers);
354
+ const i = _e(t, e);
355
+ let n = [...r], o = i.nullableKeyword, a;
356
+ const c = [...i.conditionals], p = [{
357
+ schema: i.schema,
358
+ pointer: e,
359
+ allowedTypes: Z(qe(i.schema), i.nullableKeyword)
360
+ }], d = (m, $, P = !1) => {
361
+ p.push({
362
+ schema: m.schema,
363
+ pointer: $,
364
+ allowedTypes: Z(m.allowedTypes, P)
365
+ }), c.push(...m.conditionals), n = [...n, ...m.refTrail.slice(r.length)];
366
+ }, f = i.schema.$ref;
367
+ if (delete i.schema.$ref, typeof f == "string") {
368
+ const m = We(f, e, [...r], s);
369
+ if (m === void 0)
370
+ return;
371
+ d(m.node, m.pointer), a = Ce(m.pointer) ?? m.node.defName;
372
+ }
373
+ const y = i.schema.allOf;
374
+ if (delete i.schema.allOf, Array.isArray(y)) {
375
+ const m = y.map(($, P) => {
376
+ const F = g(e, "allOf", P), k = w($, F, r, s);
377
+ return k !== void 0 && d(k, F), k;
378
+ });
379
+ m.length === 1 && (a ??= m[0]?.defName);
380
+ }
381
+ const l = Fe(i.schema);
382
+ if (l !== void 0) {
383
+ delete i.schema[l.keyword];
384
+ const m = g(e, l.keyword, l.index), $ = w(l.branch, m, r, s);
385
+ if ($ === void 0)
386
+ return;
387
+ d($, m, !0), o = !0, a ??= $.defName;
388
+ }
389
+ const b = Re(p, s.pointers);
390
+ for (const m of b.conflicts)
391
+ s.report({
392
+ severity: "warning",
393
+ code: "allof-conflict",
394
+ message: `The combined schemas disagree on \`${m.keyword}\`. The first value is used.`,
395
+ pointer: m.pointer
396
+ });
397
+ const h = b.schema;
398
+ Array.isArray(h.enum) && h.enum.includes(null) && (h.enum = h.enum.filter((m) => m !== null), o = !0);
399
+ const N = ze(b.allowedTypes, h, e, s);
400
+ return N.type === void 0 ? delete h.type : h.type = N.type, {
401
+ schema: h,
402
+ type: N.type,
403
+ nullable: o || N.nullable,
404
+ defName: a,
405
+ conditionals: c,
406
+ refTrail: n,
407
+ allowedTypes: b.allowedTypes
408
+ };
409
+ }
410
+ function We(t, e, r, s) {
411
+ const i = r.filter((a) => a === t).length;
412
+ if (i >= s.maxRefDepth) {
413
+ s.report({
414
+ severity: "warning",
415
+ code: "recursive-ref",
416
+ message: `"${t}" repeats ${i} times on this branch, so the form stops here.`,
417
+ pointer: e
418
+ });
419
+ return;
420
+ }
421
+ const n = Me(t, s.root);
422
+ if ("error" in n) {
423
+ s.report({
424
+ severity: "error",
425
+ code: "unresolved-ref",
426
+ message: n.error,
427
+ pointer: e
428
+ });
429
+ return;
430
+ }
431
+ n.target !== null && typeof n.target == "object" && (s.pointers.has(n.target) || s.pointers.set(n.target, n.pointer));
432
+ const o = w(n.target, n.pointer, [...r, t], s);
433
+ return o === void 0 ? void 0 : { node: o, pointer: n.pointer };
434
+ }
435
+ function _e(t, e) {
436
+ const r = { ...t }, s = [];
437
+ for (const n of Ue)
438
+ delete r[n];
439
+ "if" in r && s.push({
440
+ kind: "if",
441
+ if: r.if,
442
+ then: r.then,
443
+ else: r.else,
444
+ pointer: e
445
+ }), delete r.if, delete r.then, delete r.else;
446
+ for (const n of ["dependentRequired", "dependentSchemas", "dependencies"]) {
447
+ const o = r[n];
448
+ if (delete r[n], !(o === null || typeof o != "object" || Array.isArray(o)))
449
+ for (const [a, c] of Object.entries(o)) {
450
+ const u = g(e, n, a);
451
+ if (Array.isArray(c)) {
452
+ if (n !== "dependentSchemas") {
453
+ const p = c.filter((d) => typeof d == "string");
454
+ s.push({
455
+ kind: "dependentRequired",
456
+ property: a,
457
+ required: p,
458
+ pointer: u
459
+ });
460
+ }
461
+ } else n !== "dependentRequired" && s.push({
462
+ kind: "dependentSchemas",
463
+ property: a,
464
+ schema: c,
465
+ pointer: u
466
+ });
467
+ }
468
+ }
469
+ Array.isArray(r.items) && (r.prefixItems ??= r.items, delete r.items, "additionalItems" in r && (r.items = r.additionalItems)), delete r.additionalItems;
470
+ for (const [n, o] of [
471
+ ["exclusiveMinimum", "minimum"],
472
+ ["exclusiveMaximum", "maximum"]
473
+ ])
474
+ r[n] === !0 && typeof r[o] == "number" ? (r[n] = r[o], delete r[o]) : typeof r[n] == "boolean" && delete r[n];
475
+ "example" in r && (r.examples ??= [r.example], delete r.example);
476
+ const i = r.nullable === !0;
477
+ return delete r.nullable, { schema: r, conditionals: s, nullableKeyword: i };
478
+ }
479
+ function Fe(t) {
480
+ for (const e of ["oneOf", "anyOf"]) {
481
+ const r = t[e];
482
+ if (!Array.isArray(r) || r.length !== 2)
483
+ continue;
484
+ const s = r.findIndex(H);
485
+ if (s !== -1 && !H(r[1 - s]))
486
+ return { keyword: e, branch: r[1 - s], index: 1 - s };
487
+ }
488
+ }
489
+ function H(t) {
490
+ if (t === null || typeof t != "object")
491
+ return !1;
492
+ const e = t;
493
+ return e.type === "null" || e.const === null || Array.isArray(e.enum) && e.enum.length === 1 && e.enum[0] === null;
494
+ }
495
+ function Z(t, e) {
496
+ return t === void 0 || !e || t.includes("null") ? t : [...t, "null"];
497
+ }
498
+ function ze(t, e, r, s) {
499
+ if (t === void 0)
500
+ return { type: Be(e), nullable: !1 };
501
+ const i = t.filter((o) => o !== "null"), n = i.includes("number") ? i.filter((o) => o !== "integer") : i;
502
+ return n.length === 0 ? { type: "null", nullable: !1 } : (n.length > 1 && s.report({
503
+ severity: "warning",
504
+ code: "multi-type",
505
+ message: `The schema allows several types (${n.join(", ")}). The form uses the first one, ${n[0]}.`,
506
+ pointer: r
507
+ }), { type: n[0], nullable: t.includes("null") });
508
+ }
509
+ const Ke = ["format", "pattern", "minLength", "maxLength"], Le = [
510
+ "minimum",
511
+ "maximum",
512
+ "exclusiveMinimum",
513
+ "exclusiveMaximum",
514
+ "multipleOf"
515
+ ];
516
+ function Be(t) {
517
+ if (["properties", "additionalProperties", "patternProperties", "required"].some((r) => r in t))
518
+ return "object";
519
+ if ("items" in t || "prefixItems" in t)
520
+ return "array";
521
+ const e = "const" in t ? [t.const] : Array.isArray(t.enum) ? t.enum : X(t.oneOf) ?? X(t.anyOf);
522
+ if (e !== void 0 && e.length > 0) {
523
+ const r = [...new Set(e.map(Ve))];
524
+ return r.every((s) => s === "integer" || s === "number") ? r.includes("number") ? "number" : "integer" : r.length === 1 ? r[0] : void 0;
525
+ }
526
+ if (Ke.some((r) => r in t))
527
+ return "string";
528
+ if (Le.some((r) => r in t))
529
+ return "number";
530
+ }
531
+ function X(t) {
532
+ if (!Array.isArray(t) || t.length === 0)
533
+ return;
534
+ const e = [];
535
+ for (const r of t) {
536
+ if (r === null || typeof r != "object" || !("const" in r))
537
+ return;
538
+ e.push(r.const);
539
+ }
540
+ return e;
541
+ }
542
+ function Ve(t) {
543
+ return t === null ? "null" : Array.isArray(t) ? "array" : typeof t == "number" ? Number.isInteger(t) ? "integer" : "number" : typeof t == "string" ? "string" : typeof t == "boolean" ? "boolean" : "object";
544
+ }
545
+ const Je = [
546
+ "additionalProperties",
547
+ "additionalItems",
548
+ "items",
549
+ "contains",
550
+ "not",
551
+ "if",
552
+ "then",
553
+ "else",
554
+ "propertyNames"
555
+ ], Ye = ["allOf", "anyOf", "oneOf", "prefixItems", "items"], Ge = [
556
+ "properties",
557
+ "patternProperties",
558
+ "dependentSchemas",
559
+ "dependencies"
560
+ ];
561
+ function He(t, e, r) {
562
+ const s = (i, n) => {
563
+ i !== null && typeof i == "object" && !r.has(i) && r.set(i, n);
564
+ };
565
+ for (const i of Je)
566
+ Array.isArray(t[i]) || s(t[i], g(e, i));
567
+ for (const i of Ye) {
568
+ const n = t[i];
569
+ Array.isArray(n) && n.forEach((o, a) => s(o, g(e, i, a)));
570
+ }
571
+ for (const i of Ge) {
572
+ const n = t[i];
573
+ if (n !== null && typeof n == "object" && !Array.isArray(n))
574
+ for (const [o, a] of Object.entries(n))
575
+ s(a, g(e, i, o));
576
+ }
577
+ }
578
+ function Ze(t, e) {
579
+ const r = t.discriminator;
580
+ if (q(r) && typeof r.propertyName == "string") {
581
+ const i = r.propertyName, n = q(r.mapping) ? r.mapping : {}, o = e.map(
582
+ (a) => ee(te(a.schema, i)) ?? Object.keys(n).find((c) => n[c] === a.ref) ?? a.defName
583
+ );
584
+ return Q(o) ? { property: i, values: o } : void 0;
585
+ }
586
+ const s = e[0] ? he(e[0].schema) : {};
587
+ for (const i of Object.keys(s)) {
588
+ const n = e.map((o) => ee(te(o.schema, i)));
589
+ if (Q(n))
590
+ return { property: i, values: n };
591
+ }
592
+ }
593
+ function Q(t) {
594
+ return t.every((e) => e !== void 0) && new Set(t).size === t.length;
595
+ }
596
+ function ee(t) {
597
+ if (!q(t))
598
+ return;
599
+ const e = "const" in t ? t.const : Array.isArray(t.enum) && t.enum.length === 1 ? t.enum[0] : void 0;
600
+ return Xe(e) ? e : void 0;
601
+ }
602
+ function he(t) {
603
+ return q(t.properties) ? t.properties : {};
604
+ }
605
+ function te(t, e) {
606
+ return he(t)[e];
607
+ }
608
+ function Xe(t) {
609
+ return t === null || typeof t == "string" || typeof t == "number" || typeof t == "boolean";
610
+ }
611
+ function q(t) {
612
+ return t !== null && typeof t == "object" && !Array.isArray(t);
613
+ }
614
+ function O(t) {
615
+ return t == null ? [] : Array.isArray(t) ? t : [t];
616
+ }
617
+ const Qe = [
618
+ "label",
619
+ "validator",
620
+ "defaultValue",
621
+ "readonly",
622
+ "size",
623
+ "uid"
624
+ ];
625
+ function D(t, e) {
626
+ const r = O(t);
627
+ if (e === void 0 || r.length === 0)
628
+ return t;
629
+ const s = { ...r[0] };
630
+ for (const n of Qe)
631
+ e[n] !== void 0 && (s[n] = e[n]);
632
+ e.props !== void 0 && (s.props = { ...r[0].props, ...e.props });
633
+ const i = s;
634
+ return Array.isArray(t) ? [i, ...r.slice(1)] : i;
635
+ }
636
+ function re(t, e) {
637
+ const r = O(t).map((s) => {
638
+ const i = s.include, n = i !== void 0 && "when" in i ? `(${i.when}) && ${e}` : e;
639
+ return { ...s, include: { when: n } };
640
+ });
641
+ return t == null ? t : Array.isArray(t) ? r : r[0];
642
+ }
643
+ function et(t, e) {
644
+ const r = /* @__PURE__ */ new Set(), s = /* @__PURE__ */ new Set(), i = (o) => {
645
+ const a = [];
646
+ for (const c of o) {
647
+ if (c.kind === "input" && typeof c.path == "string") {
648
+ if (r.has(c.path)) {
649
+ e(c, "path");
650
+ continue;
651
+ }
652
+ r.add(c.path);
653
+ }
654
+ if (typeof c.uid == "string") {
655
+ if (s.has(c.uid)) {
656
+ e(c, "uid");
657
+ continue;
658
+ }
659
+ s.add(c.uid);
660
+ }
661
+ a.push(n(c));
662
+ }
663
+ return a;
664
+ }, n = (o) => {
665
+ let a = o;
666
+ Array.isArray(o.children) && (a = { ...a, children: i(o.children) });
667
+ const c = o.props?.template;
668
+ return me(c) && (a = { ...a, props: { ...o.props, template: n(c) } }), a;
669
+ };
670
+ return i(t);
671
+ }
672
+ const ie = [
673
+ "kind",
674
+ "type",
675
+ "uid",
676
+ "actionType",
677
+ "path",
678
+ "label",
679
+ "include",
680
+ "exclude",
681
+ "disabled",
682
+ "readonly",
683
+ "defaultValue",
684
+ "validator",
685
+ "size",
686
+ "on",
687
+ "props",
688
+ "children"
689
+ ];
690
+ function W(t) {
691
+ const e = (i) => {
692
+ const n = ie.indexOf(i.split(".")[0]);
693
+ return n === -1 ? ie.length : n;
694
+ }, r = Object.keys(t).filter((i) => t[i] !== void 0).map((i, n) => ({ key: i, position: n })).sort((i, n) => {
695
+ const o = e(i.key) - e(n.key);
696
+ if (o !== 0)
697
+ return o;
698
+ const a = Number(i.key.includes(".")) - Number(n.key.includes("."));
699
+ return a !== 0 ? a : i.position - n.position;
700
+ }), s = {};
701
+ for (const { key: i } of r)
702
+ s[i] = t[i];
703
+ if (Array.isArray(t.children) && (s.children = t.children.map(W)), t.props !== void 0) {
704
+ const i = Object.fromEntries(
705
+ Object.entries(t.props).filter(([, n]) => n !== void 0)
706
+ );
707
+ me(i.template) && (i.template = W(i.template)), s.props = i;
708
+ }
709
+ return s;
710
+ }
711
+ function me(t) {
712
+ return t !== null && typeof t == "object" && typeof t.kind == "string" && typeof t.type == "string";
713
+ }
714
+ const tt = "x-golemui", rt = 2, it = 2e3, nt = [
715
+ "not",
716
+ "contains",
717
+ "propertyNames",
718
+ "patternProperties",
719
+ "unevaluatedProperties",
720
+ "unevaluatedItems",
721
+ "$dynamicRef",
722
+ "$recursiveRef"
723
+ ];
724
+ function pt(t, e) {
725
+ return new st(t, e).run();
726
+ }
727
+ class st {
728
+ constructor(e, r) {
729
+ this.schema = e, this.options = r, this.diagnostics = [], this.pointers = /* @__PURE__ */ new WeakMap(), this.normalizedNodes = /* @__PURE__ */ new WeakMap(), this.childrenCache = /* @__PURE__ */ new WeakMap(), this.itemCache = /* @__PURE__ */ new WeakMap(), this.objectUnions = /* @__PURE__ */ new WeakMap(), this.states = /* @__PURE__ */ new Map(), this.stateCounters = /* @__PURE__ */ new Map(), this.widgetNodes = /* @__PURE__ */ new WeakMap(), this.builtNodes = 0, this.preset = r.preset, this.userRules = (r.rules ?? []).map(
730
+ (i, n) => V(i) ? this.declarativeRuleEntry(i, `rule ${n}`) : {
731
+ name: i.name ?? `rule ${n}`,
732
+ test: (o) => i.when(o),
733
+ build: i.build
734
+ }
735
+ );
736
+ const s = this.preset.rules.map((i, n) => ({
737
+ name: i.name ?? `${this.preset.name} rule ${n}`,
738
+ test: (o) => i.when(o),
739
+ build: i.build
740
+ }));
741
+ this.allRules = [...this.userRules, ...s], this.refRoot = r.refRoot ?? (typeof e == "object" ? e : {}), this.vendorKeyword = r.vendorKeyword ?? tt, this.maxRefDepth = r.maxRefDepth ?? rt, this.maxNodes = r.maxNodes ?? it;
742
+ }
743
+ run() {
744
+ const e = this.createNode(this.schema, {
745
+ path: "",
746
+ pointer: "",
747
+ required: !1,
748
+ repeaterDepth: 0,
749
+ refTrail: []
750
+ });
751
+ let r = [];
752
+ this.schema === !1 ? this.diagnostics.push({
753
+ severity: "error",
754
+ code: "root-not-object",
755
+ message: "The root schema is `false`, so no data is valid and there is no form.",
756
+ path: "",
757
+ pointer: ""
758
+ }) : e !== void 0 && e.type !== "object" ? this.report(e, {
759
+ severity: "error",
760
+ code: "root-not-object",
761
+ message: "Only an object schema at the root can become a form."
762
+ }) : e !== void 0 && (r = this.buildRoot(e));
763
+ const s = et(this.preset.root(r), (n, o) => {
764
+ const a = this.widgetNodes.get(n);
765
+ this.diagnostics.push({
766
+ severity: "error",
767
+ code: o === "path" ? "duplicate-path" : "duplicate-uid",
768
+ message: `Two widgets have the ${o} \`${String(n[o])}\`. The later one is removed.`,
769
+ path: a?.path ?? String(n.path ?? ""),
770
+ pointer: a?.pointer ?? ""
771
+ });
772
+ }), i = {
773
+ ...this.preset.schemaUrl === void 0 ? {} : { $schema: this.preset.schemaUrl },
774
+ ...this.states.size === 0 ? {} : {
775
+ states: Object.fromEntries(
776
+ [...this.states].map(([n, o]) => [o, n])
777
+ )
778
+ },
779
+ form: s.map(W)
780
+ };
781
+ return {
782
+ formDefinition: this.options.transform?.(i) ?? i,
783
+ diagnostics: this.diagnostics
784
+ };
785
+ }
786
+ /**
787
+ * The root object renders its properties directly. A layer that names a widget, or a user
788
+ * rule, can build it instead. The preset rules never do, they would wrap it in a group.
789
+ */
790
+ buildRoot(e) {
791
+ const r = this.patchesFor(e), s = this.namedWidget(r);
792
+ let i = s !== void 0 ? this.buildNamedWidget(s, e) : this.buildWithRules(e, 0, this.userRules);
793
+ if (i === void 0)
794
+ return this.buildChildren(e);
795
+ for (const n of r)
796
+ i = D(i, n);
797
+ return O(i);
798
+ }
799
+ /** Builds a node with every layer, see {@link fromJsonSchema}. */
800
+ buildNode(e) {
801
+ if (!this.countNode(e))
802
+ return null;
803
+ const r = this.patchesFor(e);
804
+ if (r.some((n) => n.skip === !0))
805
+ return null;
806
+ const s = this.namedWidget(r);
807
+ let i = s !== void 0 ? this.buildNamedWidget(s, e) : this.buildWithRules(e, 0, this.allRules);
808
+ if (i === void 0)
809
+ return this.report(e, {
810
+ severity: "error",
811
+ code: "unsupported-shape",
812
+ message: `No rule can build this ${e.type ?? "untyped"} schema, so it is not rendered.`
813
+ }), null;
814
+ for (const n of r)
815
+ i = D(i, n);
816
+ for (const n of O(i))
817
+ this.widgetNodes.set(n, e);
818
+ return i;
819
+ }
820
+ /** The first rule from `startIndex` on that matches and does not decline builds the node. */
821
+ buildWithRules(e, r, s) {
822
+ for (let i = r; i < s.length; i++) {
823
+ const n = s[i];
824
+ let o;
825
+ try {
826
+ if (!n.test(e))
827
+ continue;
828
+ o = n.build(e, this.contextFor(e, i, s));
829
+ } catch (a) {
830
+ this.report(e, {
831
+ severity: "warning",
832
+ code: "rule-error",
833
+ message: `${n.name} threw (${se(a)}), so the next rule is used.`
834
+ });
835
+ continue;
836
+ }
837
+ if (o !== void 0)
838
+ return D(o, n.patch);
839
+ }
840
+ }
841
+ /**
842
+ * Builds the widget type a layer names. A type the preset does not know is most likely a
843
+ * custom widget of the app, so it becomes a plain input of that type.
844
+ */
845
+ buildNamedWidget(e, r) {
846
+ const s = this.preset.widgets[e], i = this.contextFor(r, -1, this.allRules);
847
+ if (s === void 0)
848
+ return this.report(r, {
849
+ severity: "info",
850
+ code: "custom-widget",
851
+ message: `The ${this.preset.name} preset has no \`${e}\` widget, so a plain input of that type is built.`
852
+ }), {
853
+ kind: "input",
854
+ type: e,
855
+ path: r.path,
856
+ label: i.label(r),
857
+ validator: i.validator(r)
858
+ };
859
+ try {
860
+ return s(r, i);
861
+ } catch (n) {
862
+ return this.report(r, {
863
+ severity: "warning",
864
+ code: "rule-error",
865
+ message: `The \`${e}\` builder threw (${se(n)}), so the rules are used.`
866
+ }), this.buildWithRules(r, 0, this.allRules);
867
+ }
868
+ }
869
+ /** @param ruleIndex - The rule that builds the node, `-1` for a named widget. */
870
+ contextFor(e, r, s) {
871
+ return {
872
+ next: (i = e) => this.buildWithRules(i, r + 1, s),
873
+ build: (i) => this.buildNode(i),
874
+ children: (i) => this.childrenOf(i),
875
+ buildChildren: (i) => this.buildChildren(i),
876
+ item: (i) => this.itemOf(i),
877
+ label: (i) => ot(i),
878
+ validator: (i) => this.preset.validator(i),
879
+ enumOptions: (i) => Ae(i),
880
+ diagnostic: (i) => this.diagnostics.push({
881
+ ...i,
882
+ path: i.path ?? e.path,
883
+ pointer: i.pointer ?? e.pointer
884
+ })
885
+ };
886
+ }
887
+ buildChildren(e) {
888
+ const r = this.childrenOf(e), s = this.objectUnions.get(e.schema), i = s ? this.buildUnionChildren(e, r, s) : r.flatMap((n) => O(this.buildNode(n)));
889
+ return this.applyConditionals(e, r, i);
890
+ }
891
+ /**
892
+ * Compiles the conditionals of an object (`if/then/else`, `dependentRequired`,
893
+ * `dependentSchemas`) into its widgets:
894
+ * - a property only a branch defines is built once, visible for the branches that define it,
895
+ * and placed after the last property the condition reads;
896
+ * - a declared property that a branch constrains gets a `validator.<state>` for that branch.
897
+ */
898
+ applyConditionals(e, r, s) {
899
+ const i = this.compileConditionals(e);
900
+ if (i.length === 0)
901
+ return s;
902
+ const n = new Map(
903
+ r.flatMap((p) => p.name === void 0 ? [] : [[p.name, p]])
904
+ ), o = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map(), c = (p, d, f) => p.set(d, [...p.get(d) ?? [], f]);
905
+ for (const p of i) {
906
+ const d = p.schema?.schema ?? {}, f = A(d.properties), y = [...T(d), ...p.requiredNames].filter(
907
+ (l) => typeof l == "string"
908
+ );
909
+ for (const [l, b] of Object.entries(f))
910
+ n.has(l) ? c(o, l, { branch: p, raw: b, required: y.includes(l) }) : c(a, l, {
911
+ branch: p,
912
+ raw: b,
913
+ required: y.includes(l),
914
+ pointer: this.pointerOf(b) ?? g(p.schemaPointer, "properties", l)
915
+ });
916
+ for (const l of y)
917
+ !(l in f) && n.has(l) && c(o, l, { branch: p, raw: void 0, required: !0 });
918
+ }
919
+ const u = this.applyOverlays(e, n, o, s);
920
+ return this.insertBranchProperties(e, a, u);
921
+ }
922
+ compileConditionals(e) {
923
+ const r = this.normalizedNodes.get(e)?.conditionals ?? [], s = (n) => K(e, v(e.path, n.join("."))), i = [];
924
+ return r.forEach((n, o) => {
925
+ if (n.kind !== "if") {
926
+ i.push({
927
+ condition: `${s([n.property])} !== undefined`,
928
+ statePrefix: "dep",
929
+ group: o,
930
+ schema: n.kind === "dependentSchemas" ? this.normalizeBranch(e, n.schema, n.pointer) : void 0,
931
+ schemaPointer: n.pointer,
932
+ requiredNames: n.kind === "dependentRequired" ? n.required : [],
933
+ reads: [n.property]
934
+ });
935
+ return;
936
+ }
937
+ const a = ge(n.if, s);
938
+ "unsupported" in a ? this.diagnostics.push({
939
+ severity: "warning",
940
+ code: "if-unsupported",
941
+ message: `The \`if\` uses \`${a.unsupported}\`, which has no expression equivalent. The \`then\` and \`else\` properties are always shown and optional.`,
942
+ path: e.path,
943
+ pointer: g(n.pointer, "if")
944
+ }) : a.vacuous && this.diagnostics.push({
945
+ severity: "info",
946
+ code: "if-vacuous",
947
+ message: "The `if` also holds when a property it tests is absent, because that property is not in `if.required`. JSON Schema defines it that way.",
948
+ path: e.path,
949
+ pointer: g(n.pointer, "if")
950
+ });
951
+ const c = "unsupported" in a ? void 0 : a.expression;
952
+ for (const u of ["then", "else"]) {
953
+ const p = n[u];
954
+ if (p === void 0)
955
+ continue;
956
+ const d = g(n.pointer, u);
957
+ i.push({
958
+ condition: c === void 0 ? void 0 : u === "then" ? c : `!(${c})`,
959
+ statePrefix: "if",
960
+ group: o,
961
+ side: u,
962
+ schema: this.normalizeBranch(e, p, d),
963
+ schemaPointer: d,
964
+ requiredNames: [],
965
+ reads: "unsupported" in a ? [] : a.reads
966
+ });
967
+ }
968
+ }), i;
969
+ }
970
+ normalizeBranch(e, r, s) {
971
+ const i = this.pointerOf(r) ?? s, n = w(
972
+ r,
973
+ i,
974
+ this.refTrailOf(e),
975
+ this.environmentFor(e.path)
976
+ );
977
+ return n !== void 0 && n.conditionals.length > 0 && this.diagnostics.push({
978
+ severity: "warning",
979
+ code: "nested-conditional",
980
+ message: "A conditional inside a conditional branch is not supported, so it is not applied.",
981
+ path: e.path,
982
+ pointer: i
983
+ }), n;
984
+ }
985
+ /**
986
+ * Adds `validator.<state>` to the widget of each declared property a branch constrains. One
987
+ * state applies per field at a time, so only these combinations are expressible: required
988
+ * from any number of conditions (one state that ORs them), or the branches of one conditional
989
+ * (they never hold together). Anything else keeps the first conditional and warns.
990
+ */
991
+ applyOverlays(e, r, s, i) {
992
+ let n = i;
993
+ for (const [o, a] of s) {
994
+ const c = r.get(o), u = a.filter((f) => f.branch.condition !== void 0);
995
+ if (u.length === 0)
996
+ continue;
997
+ if (oe(e) !== void 0) {
998
+ this.report(c, {
999
+ severity: "warning",
1000
+ code: "conditional-validator-in-repeater",
1001
+ message: `Inside a repeater a condition can only show or hide widgets, so the conditional constraints of \`${o}\` are not applied.`
1002
+ });
1003
+ continue;
1004
+ }
1005
+ const p = {};
1006
+ for (const f of this.selectOverlays(c, u)) {
1007
+ const y = this.stateFor(f.prefix, f.condition), l = this.preset.validator(
1008
+ this.derivedNode(c, f.raw, f.required || c.required)
1009
+ );
1010
+ l !== void 0 && (p[`validator.${y}`] = l);
1011
+ }
1012
+ const d = n.findIndex(
1013
+ (f) => f.kind === "input" && f.path === c.path
1014
+ );
1015
+ if (d === -1) {
1016
+ this.report(c, {
1017
+ severity: "warning",
1018
+ code: "overlay-limit",
1019
+ message: `\`${o}\` is not built as one input, so its conditional constraints are not applied.`
1020
+ });
1021
+ continue;
1022
+ }
1023
+ n = [...n], n[d] = { ...n[d], ...p };
1024
+ }
1025
+ return n;
1026
+ }
1027
+ selectOverlays(e, r) {
1028
+ if (r.every((n) => n.raw === void 0)) {
1029
+ const n = [
1030
+ ...new Set(r.map((o) => o.branch.condition))
1031
+ ];
1032
+ return [
1033
+ {
1034
+ condition: ne(n),
1035
+ raw: void 0,
1036
+ required: !0,
1037
+ prefix: n.length === 1 ? r[0].branch.statePrefix : "required"
1038
+ }
1039
+ ];
1040
+ }
1041
+ const s = r[0].branch.group, i = r.filter((n) => n.branch.group === s);
1042
+ return i.length < r.length && this.report(e, {
1043
+ severity: "warning",
1044
+ code: "overlay-limit",
1045
+ message: `\`${e.name}\` gets constraints from several conditions. Only the first condition is applied.`
1046
+ }), i.map((n) => ({
1047
+ condition: n.branch.condition,
1048
+ raw: n.raw,
1049
+ required: n.required,
1050
+ prefix: n.branch.statePrefix
1051
+ }));
1052
+ }
1053
+ /** The node with the constraints of a branch merged in, for the validator of that branch. */
1054
+ derivedNode(e, r, s) {
1055
+ if (r === void 0)
1056
+ return { ...e, required: s };
1057
+ const i = w(
1058
+ { allOf: [e.schema, r] },
1059
+ e.pointer,
1060
+ this.refTrailOf(e),
1061
+ this.environmentFor(e.path)
1062
+ );
1063
+ return i === void 0 ? { ...e, required: s } : { ...e, schema: i.schema, type: i.type ?? e.type, required: s };
1064
+ }
1065
+ /**
1066
+ * Builds each property that only conditional branches define, once, visible when one of those
1067
+ * branches applies. It goes after the last property its condition reads, or at the end.
1068
+ */
1069
+ insertBranchProperties(e, r, s) {
1070
+ const i = [...s], n = /* @__PURE__ */ new Set();
1071
+ for (const [o, a] of r) {
1072
+ const [c] = a, u = v(e.path, o);
1073
+ if (!this.isValidPropertyName(o, u, c.pointer))
1074
+ continue;
1075
+ const p = a.some((h) => h.branch.condition === void 0), d = this.createNode(c.raw, {
1076
+ parent: e,
1077
+ name: o,
1078
+ path: u,
1079
+ pointer: c.pointer,
1080
+ required: !p && a.every((h) => h.required),
1081
+ repeaterDepth: e.repeaterDepth,
1082
+ refTrail: c.branch.schema?.refTrail ?? this.refTrailOf(e)
1083
+ });
1084
+ if (d === void 0)
1085
+ continue;
1086
+ a.some((h) => !E(h.raw, c.raw) || h.required !== c.required) && this.report(d, {
1087
+ severity: "warning",
1088
+ code: "branch-schema-conflict",
1089
+ message: `The branches define \`${o}\` differently. The form uses the first definition.`
1090
+ });
1091
+ let f = this.buildNode(d);
1092
+ if (!p && !at(a)) {
1093
+ const h = [
1094
+ ...new Set(a.map((N) => N.branch.condition))
1095
+ ];
1096
+ f = re(f, ne(h));
1097
+ }
1098
+ const y = c.branch.reads.map((h) => v(e.path, h));
1099
+ let l = i.reduce(
1100
+ (h, N, m) => y.includes(String(N.path)) ? m + 1 : h,
1101
+ i.length
1102
+ );
1103
+ if (l < i.length || y.length > 0)
1104
+ for (; l < i.length && n.has(i[l]); )
1105
+ l++;
1106
+ const b = O(f);
1107
+ b.forEach((h) => n.add(h)), i.splice(l, 0, ...b);
1108
+ }
1109
+ return i;
1110
+ }
1111
+ /** The name of the state for an expression. The same expression always gets the same state. */
1112
+ stateFor(e, r) {
1113
+ const s = this.states.get(r);
1114
+ if (s !== void 0)
1115
+ return s;
1116
+ const i = (this.stateCounters.get(e) ?? 0) + 1;
1117
+ this.stateCounters.set(e, i);
1118
+ const n = `${e}${String(i).padStart(2, "0")}`;
1119
+ return this.states.set(r, n), n;
1120
+ }
1121
+ /**
1122
+ * Builds an object whose `oneOf`/`anyOf` branches are objects. The discriminator becomes one
1123
+ * selector, in its own place when the object declares it, after the declared properties
1124
+ * otherwise. Then every branch property is built once, visible only for the branches that
1125
+ * define it, so no two widgets ever share a path.
1126
+ */
1127
+ buildUnionChildren(e, r, s) {
1128
+ const { discriminator: i } = s, n = [];
1129
+ let o = !1;
1130
+ for (const a of r)
1131
+ i !== void 0 && a.name === i.property ? (n.push(...this.buildSelector(e, s, i)), o = !0) : n.push(...O(this.buildNode(a)));
1132
+ return i === void 0 ? this.report(e, {
1133
+ severity: "warning",
1134
+ code: "no-discriminator",
1135
+ message: `No property tells the \`${s.keyword}\` branches apart (a \`const\` in every branch, or \`discriminator.propertyName\`). Every branch property is rendered, optional and always visible.`
1136
+ }) : o || n.push(...this.buildSelector(e, s, i)), n.push(...this.buildUnionProperties(e, s)), n;
1137
+ }
1138
+ /**
1139
+ * The discriminator as an enumeration of the branch values, labelled by the branch titles. A
1140
+ * declaration next to the union keeps its annotations, e.g. its title.
1141
+ */
1142
+ buildSelector(e, r, s) {
1143
+ const { property: i, values: n } = s, o = A(e.schema.properties)[i], a = A(r.branches[0].normalized.schema.properties)[i], c = {
1144
+ oneOf: r.branches.map((d, f) => ({
1145
+ const: n[f],
1146
+ title: ct(d, n[f])
1147
+ }))
1148
+ }, u = o !== void 0 ? { allOf: [o, c] } : { title: A(a).title, ...c }, p = this.createNode(u, {
1149
+ parent: e,
1150
+ name: i,
1151
+ path: v(e.path, i),
1152
+ pointer: this.pointerOf(o) ?? this.pointerOf(a) ?? g(e.pointer, "properties", i),
1153
+ required: T(e.schema).includes(i) || r.branches.every((d) => T(d.normalized.schema).includes(i)),
1154
+ repeaterDepth: e.repeaterDepth,
1155
+ refTrail: this.refTrailOf(e)
1156
+ });
1157
+ return p === void 0 ? [] : O(this.buildNode(p));
1158
+ }
1159
+ /** Builds each branch property once, see {@link Conversion.buildUnionChildren}. */
1160
+ buildUnionProperties(e, r) {
1161
+ const { discriminator: s } = r, i = A(e.schema.properties), n = /* @__PURE__ */ new Map();
1162
+ r.branches.forEach((a, c) => {
1163
+ const u = T(a.normalized.schema);
1164
+ for (const [p, d] of Object.entries(A(a.normalized.schema.properties))) {
1165
+ if (p === s?.property)
1166
+ continue;
1167
+ const f = this.pointerOf(d) ?? g(a.pointer, "properties", p);
1168
+ if (p in i) {
1169
+ E(d, i[p]) || this.diagnostics.push({
1170
+ severity: "warning",
1171
+ code: "branch-schema-conflict",
1172
+ message: `\`${p}\` is declared next to the union and again in a branch. The declaration next to the union is used.`,
1173
+ path: v(e.path, p),
1174
+ pointer: f
1175
+ });
1176
+ continue;
1177
+ }
1178
+ const y = n.get(p) ?? [];
1179
+ y.push({
1180
+ branchIndex: c,
1181
+ raw: d,
1182
+ required: u.includes(p),
1183
+ pointer: f,
1184
+ refTrail: a.normalized.refTrail
1185
+ }), n.set(p, y);
1186
+ }
1187
+ });
1188
+ const o = [];
1189
+ for (const [a, c] of n) {
1190
+ const [u] = c, p = v(e.path, a);
1191
+ if (!this.isValidPropertyName(a, p, u.pointer))
1192
+ continue;
1193
+ const d = this.createNode(u.raw, {
1194
+ parent: e,
1195
+ name: a,
1196
+ path: p,
1197
+ pointer: u.pointer,
1198
+ // Without a discriminator no branch is known to be active, so nothing is required.
1199
+ required: s !== void 0 && u.required,
1200
+ repeaterDepth: e.repeaterDepth,
1201
+ refTrail: u.refTrail
1202
+ });
1203
+ if (d === void 0)
1204
+ continue;
1205
+ c.some((y) => !E(y.raw, u.raw) || y.required !== u.required) && this.report(d, {
1206
+ severity: "warning",
1207
+ code: "branch-schema-conflict",
1208
+ message: `The branches define \`${a}\` differently. The form uses the first definition.`
1209
+ });
1210
+ let f = this.buildNode(d);
1211
+ if (s !== void 0 && c.length < r.branches.length) {
1212
+ const y = K(e, v(e.path, s.property)), l = c.map((b) => s.values[b.branchIndex]);
1213
+ f = re(f, I(y, l));
1214
+ }
1215
+ o.push(...O(f));
1216
+ }
1217
+ return o;
1218
+ }
1219
+ /**
1220
+ * The union of object branches of a node, `undefined` when it has none. The branches are
1221
+ * normalized once, here, and the result is cached.
1222
+ */
1223
+ objectUnionOf(e, r, s) {
1224
+ const i = this.objectUnions.get(e.schema);
1225
+ if (i !== void 0)
1226
+ return i ?? void 0;
1227
+ let n = null;
1228
+ const o = ["oneOf", "anyOf"].find(
1229
+ (a) => Array.isArray(e.schema[a]) && !M(e.schema[a])
1230
+ );
1231
+ if (o !== void 0) {
1232
+ const a = this.environmentFor(s), c = e.schema[o].flatMap((u, p) => {
1233
+ const d = this.pointerOf(u) ?? g(r, o, p), f = w(u, d, e.refTrail, a);
1234
+ return f === void 0 ? [] : [{ raw: u, pointer: d, normalized: f }];
1235
+ });
1236
+ if (c.length > 0 && c.every((u) => u.normalized.type === "object")) {
1237
+ const u = Ze(
1238
+ e.schema,
1239
+ c.map((p) => ({
1240
+ schema: p.normalized.schema,
1241
+ ref: ut(p.raw),
1242
+ defName: p.normalized.defName
1243
+ }))
1244
+ );
1245
+ n = { keyword: o, branches: c, discriminator: u };
1246
+ }
1247
+ }
1248
+ return this.objectUnions.set(e.schema, n), n ?? void 0;
1249
+ }
1250
+ /** The hint, the `$defs` override and the path override of a node, least specific first. */
1251
+ patchesFor(e) {
1252
+ const r = this.options.overrides ?? {}, s = e.defName !== void 0 ? r[`$defs/${e.defName}`] : void 0;
1253
+ return [e.hint, s, r[e.path]].filter(
1254
+ (i) => i !== void 0
1255
+ );
1256
+ }
1257
+ namedWidget(e) {
1258
+ return [...e].reverse().find((r) => r.widget !== void 0)?.widget;
1259
+ }
1260
+ declarativeRuleEntry(e, r) {
1261
+ return {
1262
+ name: e.name ?? r,
1263
+ test: (s) => J(e, s),
1264
+ build: (s, i) => e.skip === !0 ? null : e.widget !== void 0 ? this.buildNamedWidget(e.widget, s) : i.next(),
1265
+ patch: {
1266
+ label: e.label,
1267
+ props: e.props,
1268
+ validator: e.validator,
1269
+ defaultValue: e.defaultValue,
1270
+ readonly: e.readonly,
1271
+ size: e.size,
1272
+ uid: e.uid
1273
+ }
1274
+ };
1275
+ }
1276
+ /** The child nodes of an object or a tuple, see {@link BuildContext.children}. */
1277
+ childrenOf(e) {
1278
+ const r = this.childrenCache.get(e);
1279
+ if (r !== void 0)
1280
+ return r;
1281
+ const s = e.type === "object" ? this.propertyNodes(e) : e.type === "array" ? this.tupleNodes(e) : [];
1282
+ return this.childrenCache.set(e, s), s;
1283
+ }
1284
+ propertyNodes(e) {
1285
+ const r = e.schema.properties;
1286
+ if (r === null || typeof r != "object")
1287
+ return [];
1288
+ const s = Array.isArray(e.schema.required) ? e.schema.required : [], i = [];
1289
+ for (const n of this.orderedNames(e, Object.keys(r))) {
1290
+ const o = v(e.path, n), a = g(e.pointer, "properties", n);
1291
+ if (!this.isValidPropertyName(n, o, a))
1292
+ continue;
1293
+ const c = this.createNode(r[n], {
1294
+ parent: e,
1295
+ name: n,
1296
+ path: o,
1297
+ pointer: a,
1298
+ required: s.includes(n),
1299
+ repeaterDepth: e.repeaterDepth,
1300
+ refTrail: this.refTrailOf(e)
1301
+ });
1302
+ c !== void 0 && i.push(c);
1303
+ }
1304
+ return i;
1305
+ }
1306
+ /** False for a name that cannot be a path segment. Warns about a name the form reads as an index. */
1307
+ isValidPropertyName(e, r, s) {
1308
+ return e === "" || e.includes(".") ? (this.diagnostics.push({
1309
+ severity: "error",
1310
+ code: "invalid-property-name",
1311
+ message: `The property name "${e}" cannot be part of a form path, so it is not rendered.`,
1312
+ path: r,
1313
+ pointer: s
1314
+ }), !1) : (/^\d+$/.test(e) && this.diagnostics.push({
1315
+ severity: "warning",
1316
+ code: "numeric-property-name",
1317
+ message: `The form writes "${e}" as an array index, so this object becomes an array.`,
1318
+ path: r,
1319
+ pointer: s
1320
+ }), !0);
1321
+ }
1322
+ /**
1323
+ * Property names in the `order` of the most specific layer that has one: the path override,
1324
+ * the `$defs` override, the hint, then the first matching declarative rule.
1325
+ */
1326
+ orderedNames(e, r) {
1327
+ const s = [...this.patchesFor(e)].reverse().find((u) => u.order)?.order ?? this.declarativeOrder(e);
1328
+ if (s === void 0)
1329
+ return r;
1330
+ const i = s.filter((u) => u !== "*" && r.includes(u)), n = r.filter((u) => !i.includes(u)), o = s.indexOf("*");
1331
+ if (o === -1)
1332
+ return [...i, ...n];
1333
+ const a = s.slice(0, o).filter((u) => i.includes(u)), c = s.slice(o + 1).filter((u) => i.includes(u));
1334
+ return [...a, ...n, ...c];
1335
+ }
1336
+ declarativeOrder(e) {
1337
+ for (const r of this.options.rules ?? [])
1338
+ if (!(!V(r) || r.order === void 0))
1339
+ try {
1340
+ if (J(r, e))
1341
+ return r.order;
1342
+ } catch {
1343
+ }
1344
+ }
1345
+ tupleNodes(e) {
1346
+ const r = e.schema.prefixItems;
1347
+ if (!Array.isArray(r))
1348
+ return [];
1349
+ const s = typeof e.schema.minItems == "number" ? e.schema.minItems : 0;
1350
+ return r.flatMap((i, n) => {
1351
+ const o = this.createNode(i, {
1352
+ parent: e,
1353
+ path: v(e.path, String(n)),
1354
+ pointer: g(e.pointer, "prefixItems", n),
1355
+ required: n < s,
1356
+ repeaterDepth: e.repeaterDepth,
1357
+ refTrail: this.refTrailOf(e)
1358
+ });
1359
+ return o === void 0 ? [] : [o];
1360
+ });
1361
+ }
1362
+ itemOf(e) {
1363
+ if (this.itemCache.has(e))
1364
+ return this.itemCache.get(e);
1365
+ const r = e.schema.items, s = e.type === "array" && r !== void 0 && r !== !1 ? this.createNode(r, {
1366
+ parent: e,
1367
+ path: v(e.path, "items"),
1368
+ pointer: g(e.pointer, "items"),
1369
+ required: !1,
1370
+ repeaterDepth: e.repeaterDepth + 1,
1371
+ refTrail: this.refTrailOf(e)
1372
+ }) : void 0;
1373
+ return this.itemCache.set(e, s), s;
1374
+ }
1375
+ createNode(e, r) {
1376
+ const s = this.pointerOf(e) ?? r.pointer, i = w(
1377
+ e,
1378
+ s,
1379
+ r.refTrail,
1380
+ this.environmentFor(r.path)
1381
+ );
1382
+ if (i === void 0)
1383
+ return;
1384
+ this.objectUnionOf(i, s, r.path) && i.type === void 0 && (i.type = "object", i.schema.type = "object");
1385
+ const n = {
1386
+ schema: i.schema,
1387
+ type: i.type,
1388
+ nullable: i.nullable,
1389
+ path: r.path,
1390
+ name: r.name,
1391
+ defName: i.defName,
1392
+ pointer: s,
1393
+ required: r.required,
1394
+ repeaterDepth: r.repeaterDepth,
1395
+ hint: this.hintOf(i, r.path, s),
1396
+ parent: r.parent
1397
+ };
1398
+ return this.normalizedNodes.set(n, i), this.reportUnsupported(n, i), n;
1399
+ }
1400
+ hintOf(e, r, s) {
1401
+ if (this.vendorKeyword === !1 || !(this.vendorKeyword in e.schema))
1402
+ return;
1403
+ const i = e.schema[this.vendorKeyword];
1404
+ if (i !== null && typeof i == "object" && !Array.isArray(i))
1405
+ return i;
1406
+ this.diagnostics.push({
1407
+ severity: "warning",
1408
+ code: "invalid-hint",
1409
+ message: `\`${this.vendorKeyword}\` must be an object, so it is ignored.`,
1410
+ path: r,
1411
+ pointer: s
1412
+ });
1413
+ }
1414
+ reportUnsupported(e, r) {
1415
+ for (const n of nt)
1416
+ n in e.schema && this.report(e, {
1417
+ severity: "warning",
1418
+ code: "unsupported-keyword",
1419
+ message: `\`${n}\` has no form equivalent, so the form does not check it.`
1420
+ });
1421
+ const s = e.schema.additionalProperties;
1422
+ s !== null && typeof s == "object" && this.report(e, {
1423
+ severity: "warning",
1424
+ code: "unsupported-keyword",
1425
+ message: "No widget edits free-form keys (`additionalProperties` with a schema). Only the declared properties are rendered."
1426
+ });
1427
+ const i = this.objectUnions.get(e.schema);
1428
+ for (const n of ["oneOf", "anyOf"]) {
1429
+ const o = e.schema[n];
1430
+ Array.isArray(o) && !M(o) && i?.keyword !== n && this.report(e, {
1431
+ severity: "warning",
1432
+ code: "unsupported-keyword",
1433
+ message: `\`${n}\` is only supported with object branches, so its branches are not rendered.`
1434
+ });
1435
+ }
1436
+ if (e.type !== "object")
1437
+ for (const n of r.conditionals)
1438
+ this.diagnostics.push({
1439
+ severity: "warning",
1440
+ code: "unsupported-keyword",
1441
+ message: `\`${n.kind}\` is only supported on objects, so it is not applied.`,
1442
+ path: e.path,
1443
+ pointer: n.pointer
1444
+ });
1445
+ }
1446
+ refTrailOf(e) {
1447
+ return this.normalizedNodes.get(e)?.refTrail ?? [];
1448
+ }
1449
+ /** The document pointer recorded for a subschema object. */
1450
+ pointerOf(e) {
1451
+ return e !== null && typeof e == "object" ? this.pointers.get(e) : void 0;
1452
+ }
1453
+ /** Normalization problems get the form data path of the node being normalized. */
1454
+ environmentFor(e) {
1455
+ return {
1456
+ root: this.refRoot,
1457
+ maxRefDepth: this.maxRefDepth,
1458
+ pointers: this.pointers,
1459
+ report: (r) => this.diagnostics.push({ ...r, path: e })
1460
+ };
1461
+ }
1462
+ /** Counts a node, false once `maxNodes` is reached. The limit is reported once. */
1463
+ countNode(e) {
1464
+ return this.builtNodes++, this.builtNodes === this.maxNodes + 1 && this.report(e, {
1465
+ severity: "error",
1466
+ code: "max-nodes",
1467
+ message: `The schema has more than ${this.maxNodes} nodes, so the rest is not rendered.`
1468
+ }), this.builtNodes <= this.maxNodes;
1469
+ }
1470
+ report(e, r) {
1471
+ this.diagnostics.push({ ...r, path: e.path, pointer: e.pointer });
1472
+ }
1473
+ }
1474
+ function ot(t) {
1475
+ const e = t.schema.title;
1476
+ return typeof e == "string" && e !== "" ? e : t.name !== void 0 ? R(t.name) : void 0;
1477
+ }
1478
+ function ne(t) {
1479
+ return t.length === 1 ? t[0] : t.map((e) => `(${e})`).join(" || ");
1480
+ }
1481
+ function at(t) {
1482
+ return t.some(
1483
+ (e) => e.branch.side === "then" && t.some(
1484
+ (r) => r.branch.group === e.branch.group && r.branch.side === "else"
1485
+ )
1486
+ );
1487
+ }
1488
+ function ct(t, e) {
1489
+ const r = t.normalized.schema.title;
1490
+ return typeof r == "string" && r !== "" ? r : t.normalized.defName !== void 0 ? R(t.normalized.defName) : typeof e == "string" ? R(e) : String(e);
1491
+ }
1492
+ function A(t) {
1493
+ return t !== null && typeof t == "object" && !Array.isArray(t) ? t : {};
1494
+ }
1495
+ function T(t) {
1496
+ return Array.isArray(t.required) ? t.required : [];
1497
+ }
1498
+ function ut(t) {
1499
+ const e = A(t).$ref;
1500
+ return typeof e == "string" ? e : void 0;
1501
+ }
1502
+ function E(t, e) {
1503
+ return t === e || JSON.stringify(t) === JSON.stringify(e);
1504
+ }
1505
+ function v(t, e) {
1506
+ return t === "" ? e : `${t}.${e}`;
1507
+ }
1508
+ function se(t) {
1509
+ return t instanceof Error ? t.message : String(t);
1510
+ }
1511
+ export {
1512
+ pt as f,
1513
+ Me as r
1514
+ };