@plitzi/sdk-schema 0.38.23 → 0.39.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.
@@ -1,498 +0,0 @@
1
- import { isValidElementId as e } from "./elementId.mjs";
2
- import { isInstance as t, propNameProblem as n } from "./components.mjs";
3
- import { findLayoutCycle as r } from "@plitzi/sdk-shared/schema";
4
- //#region src/helpers/schemaValidator.ts
5
- var i = /* @__PURE__ */ new Set([
6
- "UNRESOLVED_BINDING_SOURCE",
7
- "MISMATCHED_BINDING_SOURCE",
8
- "UNRESOLVED_INTERACTION_TARGET",
9
- "UNRESOLVED_COMPONENT",
10
- "UNKNOWN_COMPONENT_SLOT"
11
- ]), a = (e) => !i.has(e.code), o = (i, a) => {
12
- let s = [], c = [], { flat: l, pages: u, pageFolders: d, variables: f, components: p } = i, m = a?.baseElementId, h = (e) => !!l[e], g = (e) => l[e], _ = () => Object.values(l).filter((e) => e?.definition?.type === "layoutContainer" && !e.definition.parentId).map((e) => e.id), v = () => [.../* @__PURE__ */ new Set([
13
- ...u,
14
- ..._(),
15
- ...m ? [m] : []
16
- ])], y = () => !l || typeof l != "object" ? (s.push({
17
- code: "INVALID_FLAT",
18
- message: "Schema.flat must be a valid Record<string, Element>"
19
- }), !1) : Array.isArray(u) ? Array.isArray(d) ? Array.isArray(f) ? !p || typeof p != "object" ? (s.push({
20
- code: "INVALID_COMPONENTS",
21
- message: "Schema.components must be a Record<string, SpaceComponent>"
22
- }), !1) : !0 : (s.push({
23
- code: "INVALID_VARIABLES",
24
- message: "Schema.variables must be an array"
25
- }), !1) : (s.push({
26
- code: "INVALID_PAGE_FOLDERS",
27
- message: "Schema.pageFolders must be an array"
28
- }), !1) : (s.push({
29
- code: "INVALID_PAGES",
30
- message: "Schema.pages must be an array of element IDs"
31
- }), !1), b = () => {
32
- Object.entries(l).forEach(([e, t]) => {
33
- if (!t) {
34
- s.push({
35
- code: "NULL_ELEMENT",
36
- message: `Element with id "${e}" is null or undefined`,
37
- elementId: e
38
- });
39
- return;
40
- }
41
- if (!t.id) {
42
- s.push({
43
- code: "MISSING_ID",
44
- message: `Element at key "${e}" has no id property`
45
- });
46
- return;
47
- }
48
- if (t.id !== e && s.push({
49
- code: "ID_MISMATCH",
50
- message: `Element id "${t.id}" doesn't match flat key "${e}"`,
51
- elementId: e
52
- }), !t.definition) {
53
- s.push({
54
- code: "MISSING_DEFINITION",
55
- message: "Element missing definition",
56
- elementId: e
57
- });
58
- return;
59
- }
60
- let { definition: n, attributes: r } = t;
61
- n.type || s.push({
62
- code: "MISSING_TYPE",
63
- message: "Element missing definition.type",
64
- elementId: e
65
- }), n.label === void 0 && s.push({
66
- code: "MISSING_LABEL",
67
- message: "Element missing definition.label",
68
- elementId: e
69
- }), n.rootId || s.push({
70
- code: "MISSING_ROOT_ID",
71
- message: "Element missing definition.rootId",
72
- elementId: e
73
- }), (!n.styleSelectors || typeof n.styleSelectors != "object") && s.push({
74
- code: "MISSING_STYLE_SELECTORS",
75
- message: "Element missing or invalid definition.styleSelectors",
76
- elementId: e
77
- }), r || c.push({
78
- code: "MISSING_ATTRIBUTES",
79
- message: "Element has no attributes",
80
- elementId: e
81
- }), n.items && (Array.isArray(n.items) ? n.items.forEach((t, n) => {
82
- h(t) || s.push({
83
- code: "ORPHANED_ITEM_REFERENCE",
84
- message: `Element "${e}" references non-existent child "${t}" at items[${n}]`,
85
- elementId: e,
86
- details: {
87
- childId: t,
88
- index: n
89
- }
90
- });
91
- }) : s.push({
92
- code: "INVALID_ITEMS",
93
- message: "Element.definition.items must be an array",
94
- elementId: e
95
- })), n.parentId && !h(n.parentId) && s.push({
96
- code: "ORPHANED_PARENT_REFERENCE",
97
- message: `Element "${e}" has parentId "${n.parentId}" but parent doesn't exist in flat`,
98
- elementId: e,
99
- details: { parentId: n.parentId }
100
- });
101
- });
102
- }, x = () => {
103
- Object.values(l).forEach((e) => {
104
- if (!e?.definition) return;
105
- let { id: t, definition: n } = e;
106
- if (n.items && n.items.forEach((e) => {
107
- let n = g(e);
108
- n?.definition && n.definition.parentId !== t && s.push({
109
- code: "PARENT_CHILD_MISMATCH",
110
- message: `Element "${e}" is in "${t}".items but has parentId "${n.definition.parentId}"`,
111
- elementId: e,
112
- details: {
113
- expectedParent: t,
114
- actualParent: n.definition.parentId
115
- }
116
- });
117
- }), n.parentId) {
118
- let e = g(n.parentId);
119
- if (!e?.definition) return;
120
- let r = e.definition.items || [];
121
- r.includes(t) || s.push({
122
- code: "PARENT_CHILD_MISMATCH",
123
- message: `Element "${t}" has parentId "${n.parentId}" but parent doesn't have it in items`,
124
- elementId: t,
125
- details: {
126
- parentId: n.parentId,
127
- parentItems: r
128
- }
129
- });
130
- }
131
- });
132
- }, S = () => {
133
- let e = /* @__PURE__ */ new Set(), t = /* @__PURE__ */ new Set(), n = (r) => {
134
- if (t.has(r)) return s.push({
135
- code: "CIRCULAR_REFERENCE",
136
- message: `Circular reference detected involving element "${r}"`,
137
- elementId: r
138
- }), !0;
139
- if (e.has(r)) return !1;
140
- e.add(r), t.add(r);
141
- let i = g(r);
142
- if (i?.definition?.items) {
143
- for (let e of i.definition.items) if (n(e)) return t.delete(r), !0;
144
- }
145
- let a = /* @__PURE__ */ new Set(), o = i;
146
- for (; o?.definition?.parentId;) {
147
- if (a.has(o.id)) return s.push({
148
- code: "CIRCULAR_PARENT_REFERENCE",
149
- message: `Circular parent reference detected involving element "${o.id}"`,
150
- elementId: o.id
151
- }), !0;
152
- a.add(o.id), o = g(o.definition.parentId);
153
- }
154
- return t.delete(r), !1;
155
- };
156
- Object.keys(l).forEach((t) => {
157
- e.has(t) || n(t);
158
- });
159
- }, C = () => {
160
- let e = /* @__PURE__ */ new Set(), t = 0;
161
- u.forEach((n, r) => {
162
- e.has(n) && s.push({
163
- code: "DUPLICATE_PAGE",
164
- message: `Duplicate page ID "${n}" in pages array at index ${r}`,
165
- elementId: n
166
- }), e.add(n);
167
- let i = g(n);
168
- i ? (i.definition.type !== "page" && s.push({
169
- code: "INVALID_PAGE_TYPE",
170
- message: `Element "${n}" in pages array has type "${i.definition.type}" instead of "page"`,
171
- elementId: n
172
- }), i.definition.rootId !== n && s.push({
173
- code: "PAGE_ROOT_ID_MISMATCH",
174
- message: `Page "${n}" has rootId "${i.definition.rootId}" instead of its own ID`,
175
- elementId: n
176
- }), i.attributes?.default && t++) : s.push({
177
- code: "INVALID_PAGE_REFERENCE",
178
- message: `Page ID "${n}" in pages array doesn't exist in flat`,
179
- elementId: n
180
- });
181
- }), t === 0 && u.length > 0 ? c.push({
182
- code: "NO_DEFAULT_PAGE",
183
- message: "No default page found in schema"
184
- }) : t > 1 && c.push({
185
- code: "MULTIPLE_DEFAULT_PAGES",
186
- message: `Found ${t} default pages, expected 1`
187
- });
188
- }, w = () => {
189
- v().forEach((e) => {
190
- let t = g(e);
191
- if (!t) return;
192
- let n = /* @__PURE__ */ new Set([e]), r = (t) => {
193
- let i = g(t);
194
- i?.definition && !n.has(t) && (n.add(t), i.definition.rootId !== e && s.push({
195
- code: "ROOT_ID_MISMATCH",
196
- message: `Element "${t}" has rootId "${i.definition.rootId}" but should be "${e}" (its root's ID)`,
197
- elementId: t,
198
- details: {
199
- expectedRootId: e,
200
- actualRootId: i.definition.rootId
201
- }
202
- }), i.definition.items && i.definition.items.forEach((e) => r(e)));
203
- };
204
- t.definition.items && t.definition.items.forEach((e) => r(e));
205
- });
206
- }, T = () => {
207
- let e = /* @__PURE__ */ new Set();
208
- d.forEach((t, n) => {
209
- t.id ? (e.has(t.id) && s.push({
210
- code: "DUPLICATE_FOLDER",
211
- message: `Duplicate folder ID "${t.id}"`,
212
- elementId: t.id
213
- }), e.add(t.id), t.parentId && (e.has(t.parentId) || s.push({
214
- code: "ORPHANED_FOLDER_PARENT",
215
- message: `Folder "${t.id}" has non-existent parent "${t.parentId}"`,
216
- elementId: t.id,
217
- details: { parentId: t.parentId }
218
- }))) : s.push({
219
- code: "INVALID_FOLDER",
220
- message: `Page folder at index ${n} has no id`
221
- });
222
- });
223
- }, E = () => {
224
- let e = /* @__PURE__ */ new Set();
225
- _().forEach((t) => {
226
- let n = r(g, t);
227
- n.length === 0 || e.has(n[0]) || (n.forEach((t) => e.add(t)), s.push({
228
- code: "LAYOUT_CYCLE",
229
- message: `Layouts sit inside each other in a circle: ${n.join(" → ")}`,
230
- elementId: t,
231
- details: { cycle: n }
232
- }));
233
- });
234
- }, D = () => {
235
- let e = /* @__PURE__ */ new Set(), t = (n) => {
236
- if (e.has(n)) return;
237
- e.add(n);
238
- let r = g(n);
239
- r?.definition?.items && r.definition.items.forEach((e) => t(e));
240
- }, n = /* @__PURE__ */ new Set(), r = (e) => {
241
- if (n.has(e)) return;
242
- n.add(e), t(e);
243
- let i = g(e)?.attributes;
244
- i?.layout && r(i.layout), i?.layoutContainer && t(i.layoutContainer);
245
- };
246
- m ? r(m) : v().forEach(r);
247
- let i = new Set(v());
248
- Object.keys(l).forEach((t) => {
249
- let n = g(t);
250
- n && !e.has(t) && n.definition.type !== "page" && !i.has(t) && c.push({
251
- code: "ORPHANED_ELEMENT",
252
- message: `Element "${t}" is not reachable from any page or layout`,
253
- elementId: t
254
- });
255
- });
256
- }, O = () => {
257
- let e = /* @__PURE__ */ new Set();
258
- f.forEach((t, n) => {
259
- t.name ? (e.has(t.name) && s.push({
260
- code: "DUPLICATE_VARIABLE",
261
- message: `Duplicate variable name "${t.name}"`,
262
- details: { variableName: t.name }
263
- }), e.add(t.name), t.type || c.push({
264
- code: "MISSING_VARIABLE_TYPE",
265
- message: `Variable "${t.name}" has no type`,
266
- details: { variableName: t.name }
267
- })) : s.push({
268
- code: "INVALID_VARIABLE",
269
- message: `Variable at index ${n} has no name`
270
- });
271
- });
272
- }, k = () => {
273
- Object.values(l).forEach((t) => {
274
- t && (e(t.id) || s.push({
275
- code: "INVALID_ELEMENT_ID",
276
- message: `Element "${t.id}" has a name that must start with a letter, then letters, numbers, hyphens and underscores (no hyphen or underscore at the start, and no hyphen at the end)`,
277
- elementId: t.id
278
- }));
279
- });
280
- }, A = (e) => {
281
- let t = /* @__PURE__ */ new Set(), n = /* @__PURE__ */ new Set(), r = /* @__PURE__ */ new Map();
282
- Object.values(l).forEach((i) => {
283
- if (!i) return;
284
- t.add(i.id);
285
- let a = e?.[i.definition.type];
286
- a && r.set(i.id, a), n.add(i.definition.type);
287
- }), Object.values(l).forEach((i) => {
288
- i && i.definition.bindings && Object.entries(i.definition.bindings).forEach(([a, o]) => {
289
- (o ?? []).forEach((o) => {
290
- let c = o.source.split(".")[0], l = c.indexOf("_");
291
- if (l === -1) return;
292
- let u = c.slice(0, l), d = c.slice(l + 1), f = `Element "${i.id}" binds ${a}.${o.to} to "${o.source}"`;
293
- if (!t.has(d)) {
294
- (e || n.has(u)) && s.push({
295
- code: "UNRESOLVED_BINDING_SOURCE",
296
- message: `${f}, but no element answers to the name "${d}"`,
297
- elementId: i.id,
298
- missing: d,
299
- details: {
300
- source: o.source,
301
- elementId: d
302
- }
303
- });
304
- return;
305
- }
306
- let p = r.get(d);
307
- p && p !== u && s.push({
308
- code: "MISMATCHED_BINDING_SOURCE",
309
- message: `${f}, but "${d}" publishes its source as "${p}_${d}"`,
310
- elementId: i.id,
311
- details: {
312
- source: o.source,
313
- elementId: d,
314
- expected: `${p}_${d}`
315
- }
316
- });
317
- });
318
- });
319
- });
320
- }, j = () => {
321
- let t = new Set(Object.keys(l));
322
- Object.values(l).forEach((n) => {
323
- let r = n?.definition.interactions;
324
- if (!r) return;
325
- let i = Object.keys(r);
326
- Object.entries(r).forEach(([r, a]) => {
327
- e(r) || s.push({
328
- code: "INVALID_INTERACTION_ID",
329
- message: `Interaction "${r}" on element "${n.id}" has a name that must start with a letter, then letters, numbers, hyphens and underscores — a later step reads it as {{ ${r}.field }}`,
330
- elementId: n.id
331
- }), a.id !== r && s.push({
332
- code: "INTERACTION_ID_MISMATCH",
333
- message: `Interaction id "${a.id}" on element "${n.id}" doesn't match its key "${r}"`,
334
- elementId: n.id
335
- }), ["beforeNode", "afterNode"].forEach((e) => {
336
- let t = a[e];
337
- t && !i.includes(t) && s.push({
338
- code: "BROKEN_FLOW_LINK",
339
- message: `Interaction "${a.id}" on element "${n.id}" names ${e} "${t}", which is not a node of this flow`,
340
- elementId: n.id
341
- });
342
- }), a.flowId && !i.includes(a.flowId) && s.push({
343
- code: "BROKEN_FLOW_ID",
344
- message: `Interaction "${a.id}" on element "${n.id}" belongs to flow "${a.flowId}", which is not a node of this flow`,
345
- elementId: n.id
346
- }), a.type === "callback" && a.elementId && !t.has(a.elementId) && s.push({
347
- code: "UNRESOLVED_INTERACTION_TARGET",
348
- message: `Interaction "${a.id}" on element "${n.id}" runs against "${a.elementId}", but no element answers to that name`,
349
- elementId: n.id,
350
- missing: a.elementId,
351
- wantedBy: a.action
352
- });
353
- let o = a.type === "trigger" && a.action === "onSubmit" && a.enabled ? g(a.elementId ?? n.id) : void 0;
354
- o?.definition.type === "form" && o.attributes.managedByInteractions !== !0 && c.push({
355
- code: "FORM_SUBMIT_UNMANAGED",
356
- message: `Form "${o.id}" has an onSubmit flow but is not managedByInteractions, so the browser submits it natively and the flow never runs — set managedByInteractions: true`,
357
- elementId: o.id
358
- });
359
- });
360
- });
361
- }, M = () => {
362
- Object.values(l).forEach((e) => {
363
- if (!e || !e.definition.bindings) return;
364
- let t = /* @__PURE__ */ new Set();
365
- Object.entries(e.definition.bindings).forEach(([n, r]) => {
366
- (r ?? []).forEach((r) => {
367
- r.id ? (t.has(r.id) && s.push({
368
- code: "DUPLICATE_BINDING_ID",
369
- message: `Element "${e.id}" has two bindings called "${r.id}"`,
370
- elementId: e.id,
371
- details: { bindingId: r.id }
372
- }), t.add(r.id)) : s.push({
373
- code: "MISSING_BINDING_ID",
374
- message: `Element "${e.id}" has a ${n} binding to "${r.to}" with no id`,
375
- elementId: e.id
376
- });
377
- });
378
- });
379
- });
380
- }, N = () => {
381
- let r = new Map(Object.keys(l).map((e) => [e, ""])), u = (e) => e ? `component "${e}"` : "the pages", d = /* @__PURE__ */ new Map();
382
- Object.entries(p).forEach(([t, l]) => {
383
- let d = `Component "${t}"`;
384
- l.id !== t && s.push({
385
- code: "COMPONENT_ID_MISMATCH",
386
- message: `${d} has id "${l.id}"`,
387
- componentId: t
388
- }), e(t) || s.push({
389
- code: "INVALID_COMPONENT_ID",
390
- message: `${d} has a name that must start with a letter, then letters, numbers, hyphens and underscores`,
391
- componentId: t
392
- });
393
- let f = l.flat[l.rootId];
394
- if (!f) {
395
- s.push({
396
- code: "COMPONENT_ROOT_MISSING",
397
- message: `${d} is rooted at "${l.rootId}", which is not an element of its tree`,
398
- componentId: t
399
- });
400
- return;
401
- }
402
- f.definition.parentId && s.push({
403
- code: "COMPONENT_ROOT_HAS_PARENT",
404
- message: `${d} is rooted at "${l.rootId}", which names a parent — a component's root has none`,
405
- elementId: l.rootId,
406
- componentId: t
407
- }), Object.keys(l.flat).forEach((e) => {
408
- let n = r.get(e);
409
- n === void 0 ? r.set(e, t) : s.push({
410
- code: "DUPLICATE_ELEMENT_ID",
411
- message: `"${e}" names an element of ${u(n)} and one of ${u(t)} — an id is one element in the whole space`,
412
- elementId: e,
413
- componentId: t
414
- });
415
- }), Object.keys(l.props ?? {}).forEach((e) => {
416
- let r = n(e);
417
- r && s.push({
418
- code: "INVALID_COMPONENT_PROP",
419
- message: `${d}: ${r}`,
420
- componentId: t
421
- });
422
- }), (l.slots ?? []).forEach((e) => {
423
- Object.hasOwn(l.flat, e) || s.push({
424
- code: "MISSING_COMPONENT_SLOT",
425
- message: `${d} declares slot "${e}", which is not an element of its tree`,
426
- componentId: t
427
- });
428
- });
429
- let p = o({
430
- ...i,
431
- flat: l.flat,
432
- pages: [],
433
- pageFolders: [],
434
- components: {}
435
- }, {
436
- baseElementId: l.rootId,
437
- sourceTypes: a?.sourceTypes
438
- }).validate();
439
- p.errors.forEach((e) => s.push({
440
- ...e,
441
- message: `${d}: ${e.message}`,
442
- componentId: t
443
- })), p.warnings.forEach((e) => c.push({
444
- ...e,
445
- message: `${d}: ${e.message}`,
446
- componentId: t
447
- }));
448
- }), [["", l], ...Object.entries(p).map(([e, t]) => [e, t.flat])].forEach(([e, n]) => {
449
- Object.values(n).forEach((r) => {
450
- if (!r || !t(r)) return;
451
- let i = r.attributes.referenceId, a = typeof i == "string" ? p[i] : void 0;
452
- !a || typeof i != "string" ? s.push({
453
- code: "UNRESOLVED_COMPONENT",
454
- message: `Instance "${r.id}" places component "${String(i)}", which this space does not declare`,
455
- elementId: r.id,
456
- ...e ? { componentId: e } : {}
457
- }) : (e && d.set(e, (d.get(e) ?? /* @__PURE__ */ new Set()).add(i)), (r.definition.items ?? []).forEach((t) => {
458
- let o = n[t]?.attributes.slot ?? a.slots?.[0];
459
- (typeof o != "string" || !a.slots?.includes(o)) && s.push({
460
- code: "UNKNOWN_COMPONENT_SLOT",
461
- message: `"${t}" fills ${typeof o == "string" ? `slot "${o}"` : "a slot"} of instance "${r.id}", but component "${i}" declares ${a.slots?.length ? `only ${a.slots.map((e) => `"${e}"`).join(", ")}` : "no slots"}`,
462
- elementId: t,
463
- ...e ? { componentId: e } : {}
464
- });
465
- }));
466
- });
467
- });
468
- let f = /* @__PURE__ */ new Set(), m = (e, t) => {
469
- if (t.includes(e)) {
470
- let n = [...t.slice(t.indexOf(e)), e];
471
- n.some((e) => f.has(e)) || (n.forEach((e) => f.add(e)), s.push({
472
- code: "COMPONENT_CYCLE",
473
- message: `Components place each other in a circle: ${n.join(" → ")}`,
474
- componentId: e,
475
- details: { cycle: n }
476
- }));
477
- } else d.get(e)?.forEach((n) => m(n, [...t, e]));
478
- };
479
- Object.keys(p).forEach((e) => m(e, []));
480
- };
481
- return { validate: () => y() ? (b(), x(), S(), C(), w(), T(), E(), D(), O(), k(), A(a?.sourceTypes), M(), j(), m || N(), {
482
- valid: s.length === 0,
483
- errors: s,
484
- warnings: c
485
- }) : {
486
- valid: !1,
487
- errors: s,
488
- warnings: c
489
- } };
490
- }, s = (e, t) => o(e, t).validate(), c = (e, t, n) => {
491
- let r = s(e, t);
492
- if (!r.valid) {
493
- let e = `Invalid schema${n ? ` (${n})` : ""}: ${r.errors.map((e) => e.message).join("; ")}`;
494
- throw Error(e);
495
- }
496
- }, l = (e, t) => s(e, t).valid;
497
- //#endregion
498
- export { i as REFERENCE_ERROR_CODES, c as assertSchemaValid, a as isIntegrityError, l as isSchemaValid, s as validateSchema };