@plitzi/sdk-schema 0.38.24 → 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.
- package/CHANGELOG.md +152 -1
- package/dist/SchemaReducer.d.ts +2 -1
- package/dist/SchemaReducer.mjs +91 -90
- package/dist/helpers/schemaValidator/bindings.d.ts +32 -0
- package/dist/helpers/schemaValidator/bindings.mjs +121 -0
- package/dist/helpers/schemaValidator/components.d.ts +13 -0
- package/dist/helpers/schemaValidator/components.mjs +106 -0
- package/dist/helpers/schemaValidator/context.d.ts +20 -0
- package/dist/helpers/schemaValidator/context.mjs +26 -0
- package/dist/helpers/schemaValidator/index.d.ts +7 -0
- package/dist/helpers/schemaValidator/index.mjs +27 -0
- package/dist/helpers/schemaValidator/pages.d.ts +7 -0
- package/dist/helpers/schemaValidator/pages.mjs +102 -0
- package/dist/helpers/schemaValidator/structure.d.ts +7 -0
- package/dist/helpers/schemaValidator/structure.mjs +156 -0
- package/dist/helpers/{schemaValidator.d.ts → schemaValidator/types.d.ts} +0 -4
- package/dist/helpers/schemaValidator/types.mjs +10 -0
- package/dist/index.mjs +3 -2
- package/package.json +4 -4
- package/dist/helpers/schemaValidator.mjs +0 -498
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { isValidElementId as e } from "../elementId.mjs";
|
|
2
|
+
import { isInstance as t, propNameProblem as n } from "../components.mjs";
|
|
3
|
+
//#region src/helpers/schemaValidator/components.ts
|
|
4
|
+
var r = ({ errors: r, warnings: i, flat: a, components: o, schema: s, options: c }, l) => {
|
|
5
|
+
let u = new Map(Object.keys(a).map((e) => [e, ""])), d = (e) => e ? `component "${e}"` : "the pages", f = /* @__PURE__ */ new Map();
|
|
6
|
+
Object.entries(o).forEach(([t, a]) => {
|
|
7
|
+
let o = `Component "${t}"`;
|
|
8
|
+
a.id !== t && r.push({
|
|
9
|
+
code: "COMPONENT_ID_MISMATCH",
|
|
10
|
+
message: `${o} has id "${a.id}"`,
|
|
11
|
+
componentId: t
|
|
12
|
+
}), e(t) || r.push({
|
|
13
|
+
code: "INVALID_COMPONENT_ID",
|
|
14
|
+
message: `${o} has a name that must start with a letter, then letters, numbers, hyphens and underscores`,
|
|
15
|
+
componentId: t
|
|
16
|
+
});
|
|
17
|
+
let f = a.flat[a.rootId];
|
|
18
|
+
if (!f) {
|
|
19
|
+
r.push({
|
|
20
|
+
code: "COMPONENT_ROOT_MISSING",
|
|
21
|
+
message: `${o} is rooted at "${a.rootId}", which is not an element of its tree`,
|
|
22
|
+
componentId: t
|
|
23
|
+
});
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
f.definition.parentId && r.push({
|
|
27
|
+
code: "COMPONENT_ROOT_HAS_PARENT",
|
|
28
|
+
message: `${o} is rooted at "${a.rootId}", which names a parent — a component's root has none`,
|
|
29
|
+
elementId: a.rootId,
|
|
30
|
+
componentId: t
|
|
31
|
+
}), Object.keys(a.flat).forEach((e) => {
|
|
32
|
+
let n = u.get(e);
|
|
33
|
+
n === void 0 ? u.set(e, t) : r.push({
|
|
34
|
+
code: "DUPLICATE_ELEMENT_ID",
|
|
35
|
+
message: `"${e}" names an element of ${d(n)} and one of ${d(t)} — an id is one element in the whole space`,
|
|
36
|
+
elementId: e,
|
|
37
|
+
componentId: t
|
|
38
|
+
});
|
|
39
|
+
}), Object.keys(a.props ?? {}).forEach((e) => {
|
|
40
|
+
let i = n(e);
|
|
41
|
+
i && r.push({
|
|
42
|
+
code: "INVALID_COMPONENT_PROP",
|
|
43
|
+
message: `${o}: ${i}`,
|
|
44
|
+
componentId: t
|
|
45
|
+
});
|
|
46
|
+
}), (a.slots ?? []).forEach((e) => {
|
|
47
|
+
Object.hasOwn(a.flat, e) || r.push({
|
|
48
|
+
code: "MISSING_COMPONENT_SLOT",
|
|
49
|
+
message: `${o} declares slot "${e}", which is not an element of its tree`,
|
|
50
|
+
componentId: t
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
let p = l({
|
|
54
|
+
...s,
|
|
55
|
+
flat: a.flat,
|
|
56
|
+
pages: [],
|
|
57
|
+
pageFolders: [],
|
|
58
|
+
components: {}
|
|
59
|
+
}, {
|
|
60
|
+
baseElementId: a.rootId,
|
|
61
|
+
sourceTypes: c?.sourceTypes
|
|
62
|
+
});
|
|
63
|
+
p.errors.forEach((e) => r.push({
|
|
64
|
+
...e,
|
|
65
|
+
message: `${o}: ${e.message}`,
|
|
66
|
+
componentId: t
|
|
67
|
+
})), p.warnings.forEach((e) => i.push({
|
|
68
|
+
...e,
|
|
69
|
+
message: `${o}: ${e.message}`,
|
|
70
|
+
componentId: t
|
|
71
|
+
}));
|
|
72
|
+
}), [["", a], ...Object.entries(o).map(([e, t]) => [e, t.flat])].forEach(([e, n]) => {
|
|
73
|
+
Object.values(n).forEach((i) => {
|
|
74
|
+
if (!i || !t(i)) return;
|
|
75
|
+
let a = i.attributes.referenceId, s = typeof a == "string" ? o[a] : void 0;
|
|
76
|
+
!s || typeof a != "string" ? r.push({
|
|
77
|
+
code: "UNRESOLVED_COMPONENT",
|
|
78
|
+
message: `Instance "${i.id}" places component "${String(a)}", which this space does not declare`,
|
|
79
|
+
elementId: i.id,
|
|
80
|
+
...e ? { componentId: e } : {}
|
|
81
|
+
}) : (e && f.set(e, (f.get(e) ?? /* @__PURE__ */ new Set()).add(a)), (i.definition.items ?? []).forEach((t) => {
|
|
82
|
+
let o = n[t]?.attributes.slot ?? s.slots?.[0];
|
|
83
|
+
(typeof o != "string" || !s.slots?.includes(o)) && r.push({
|
|
84
|
+
code: "UNKNOWN_COMPONENT_SLOT",
|
|
85
|
+
message: `"${t}" fills ${typeof o == "string" ? `slot "${o}"` : "a slot"} of instance "${i.id}", but component "${a}" declares ${s.slots?.length ? `only ${s.slots.map((e) => `"${e}"`).join(", ")}` : "no slots"}`,
|
|
86
|
+
elementId: t,
|
|
87
|
+
...e ? { componentId: e } : {}
|
|
88
|
+
});
|
|
89
|
+
}));
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
let p = /* @__PURE__ */ new Set(), m = (e, t) => {
|
|
93
|
+
if (t.includes(e)) {
|
|
94
|
+
let n = [...t.slice(t.indexOf(e)), e];
|
|
95
|
+
n.some((e) => p.has(e)) || (n.forEach((e) => p.add(e)), r.push({
|
|
96
|
+
code: "COMPONENT_CYCLE",
|
|
97
|
+
message: `Components place each other in a circle: ${n.join(" → ")}`,
|
|
98
|
+
componentId: e,
|
|
99
|
+
details: { cycle: n }
|
|
100
|
+
}));
|
|
101
|
+
} else f.get(e)?.forEach((n) => m(n, [...t, e]));
|
|
102
|
+
};
|
|
103
|
+
Object.keys(o).forEach((e) => m(e, []));
|
|
104
|
+
};
|
|
105
|
+
//#endregion
|
|
106
|
+
export { r as validateComponents };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { SchemaValidationError, SchemaValidationOptions } from './types';
|
|
2
|
+
import { Element, Schema } from '@plitzi/sdk-shared';
|
|
3
|
+
/** What every check of one validation shares: the document's parts, what was found so far, and how its roots are read. */
|
|
4
|
+
export declare const createValidationContext: (schema: Schema, options?: SchemaValidationOptions) => {
|
|
5
|
+
schema: Schema;
|
|
6
|
+
options: SchemaValidationOptions | undefined;
|
|
7
|
+
errors: SchemaValidationError[];
|
|
8
|
+
warnings: SchemaValidationError[];
|
|
9
|
+
flat: Record<string, Element<Record<string, unknown>>>;
|
|
10
|
+
pages: string[];
|
|
11
|
+
pageFolders: import('@plitzi/sdk-shared').PageFolder[];
|
|
12
|
+
variables: import('@plitzi/sdk-shared').SchemaVariable[];
|
|
13
|
+
components: Record<string, import('@plitzi/sdk-shared').SpaceComponent>;
|
|
14
|
+
baseElementId: string | undefined;
|
|
15
|
+
elementExists: (id: string) => boolean;
|
|
16
|
+
getElement: (id: string) => Element | undefined;
|
|
17
|
+
layoutRootIds: () => string[];
|
|
18
|
+
rootIds: () => string[];
|
|
19
|
+
};
|
|
20
|
+
export type ValidationContext = ReturnType<typeof createValidationContext>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/helpers/schemaValidator/context.ts
|
|
2
|
+
var e = (e, t) => {
|
|
3
|
+
let n = [], r = [], { flat: i, pages: a, pageFolders: o, variables: s, components: c } = e, l = t?.baseElementId, u = (e) => !!i[e], d = (e) => i[e], f = () => Object.values(i).filter((e) => e?.definition?.type === "layoutContainer" && !e.definition.parentId).map((e) => e.id);
|
|
4
|
+
return {
|
|
5
|
+
schema: e,
|
|
6
|
+
options: t,
|
|
7
|
+
errors: n,
|
|
8
|
+
warnings: r,
|
|
9
|
+
flat: i,
|
|
10
|
+
pages: a,
|
|
11
|
+
pageFolders: o,
|
|
12
|
+
variables: s,
|
|
13
|
+
components: c,
|
|
14
|
+
baseElementId: l,
|
|
15
|
+
elementExists: u,
|
|
16
|
+
getElement: d,
|
|
17
|
+
layoutRootIds: f,
|
|
18
|
+
rootIds: () => [.../* @__PURE__ */ new Set([
|
|
19
|
+
...a,
|
|
20
|
+
...f(),
|
|
21
|
+
...l ? [l] : []
|
|
22
|
+
])]
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { e as createValidationContext };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { SchemaValidationOptions, SchemaValidationResult } from './types';
|
|
2
|
+
import { Schema } from '@plitzi/sdk-shared';
|
|
3
|
+
export { REFERENCE_ERROR_CODES, isIntegrityError } from './types';
|
|
4
|
+
export type { SchemaValidationError, SchemaValidationOptions, SchemaValidationResult } from './types';
|
|
5
|
+
export declare const validateSchema: (schema: Schema, options?: SchemaValidationOptions) => SchemaValidationResult;
|
|
6
|
+
export declare const assertSchemaValid: (schema: Schema, options?: SchemaValidationOptions, context?: string) => void;
|
|
7
|
+
export declare const isSchemaValid: (schema: Schema, options?: SchemaValidationOptions) => boolean;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { validateBindingSources as e, validateBindings as t, validateInteractions as n, validateVariables as r } from "./bindings.mjs";
|
|
2
|
+
import { validateComponents as i } from "./components.mjs";
|
|
3
|
+
import { createValidationContext as a } from "./context.mjs";
|
|
4
|
+
import { validateLayoutCycles as o, validateOrphanedElements as s, validatePageFolders as c, validatePages as l, validateRootConsistency as u } from "./pages.mjs";
|
|
5
|
+
import { validateCircularReferences as d, validateElementIds as f, validateElements as p, validateParentChildConsistency as m, validateStructure as h } from "./structure.mjs";
|
|
6
|
+
import { REFERENCE_ERROR_CODES as g, isIntegrityError as _ } from "./types.mjs";
|
|
7
|
+
//#region src/helpers/schemaValidator/index.ts
|
|
8
|
+
var v = (g, _) => {
|
|
9
|
+
let y = a(g, _), { errors: b, warnings: x, baseElementId: S } = y;
|
|
10
|
+
return h(y) ? (p(y), m(y), d(y), l(y), u(y), c(y), o(y), s(y), r(y), f(y), e(y, _?.sourceTypes), t(y), n(y), S || i(y, v), {
|
|
11
|
+
valid: b.length === 0,
|
|
12
|
+
errors: b,
|
|
13
|
+
warnings: x
|
|
14
|
+
}) : {
|
|
15
|
+
valid: !1,
|
|
16
|
+
errors: b,
|
|
17
|
+
warnings: x
|
|
18
|
+
};
|
|
19
|
+
}, y = (e, t) => v(e, t), b = (e, t, n) => {
|
|
20
|
+
let r = y(e, t);
|
|
21
|
+
if (!r.valid) {
|
|
22
|
+
let e = `Invalid schema${n ? ` (${n})` : ""}: ${r.errors.map((e) => e.message).join("; ")}`;
|
|
23
|
+
throw Error(e);
|
|
24
|
+
}
|
|
25
|
+
}, x = (e, t) => y(e, t).valid;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { g as REFERENCE_ERROR_CODES, b as assertSchemaValid, _ as isIntegrityError, x as isSchemaValid, y as validateSchema };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationContext } from './context';
|
|
2
|
+
/** Its roots: pages, layout shells and folders, and no element left hanging off none of them. */
|
|
3
|
+
export declare const validatePages: ({ errors, warnings, pages, getElement }: ValidationContext) => void;
|
|
4
|
+
export declare const validateRootConsistency: ({ errors, getElement, rootIds }: ValidationContext) => void;
|
|
5
|
+
export declare const validatePageFolders: ({ errors, pageFolders }: ValidationContext) => void;
|
|
6
|
+
export declare const validateLayoutCycles: ({ errors, getElement, layoutRootIds }: ValidationContext) => void;
|
|
7
|
+
export declare const validateOrphanedElements: ({ warnings, flat, baseElementId, getElement, rootIds }: ValidationContext) => void;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { findLayoutCycle as e } from "@plitzi/sdk-shared/schema";
|
|
2
|
+
//#region src/helpers/schemaValidator/pages.ts
|
|
3
|
+
var t = ({ errors: e, warnings: t, pages: n, getElement: r }) => {
|
|
4
|
+
let i = /* @__PURE__ */ new Set(), a = 0;
|
|
5
|
+
n.forEach((t, n) => {
|
|
6
|
+
i.has(t) && e.push({
|
|
7
|
+
code: "DUPLICATE_PAGE",
|
|
8
|
+
message: `Duplicate page ID "${t}" in pages array at index ${n}`,
|
|
9
|
+
elementId: t
|
|
10
|
+
}), i.add(t);
|
|
11
|
+
let o = r(t);
|
|
12
|
+
o ? (o.definition.type !== "page" && e.push({
|
|
13
|
+
code: "INVALID_PAGE_TYPE",
|
|
14
|
+
message: `Element "${t}" in pages array has type "${o.definition.type}" instead of "page"`,
|
|
15
|
+
elementId: t
|
|
16
|
+
}), o.definition.rootId !== t && e.push({
|
|
17
|
+
code: "PAGE_ROOT_ID_MISMATCH",
|
|
18
|
+
message: `Page "${t}" has rootId "${o.definition.rootId}" instead of its own ID`,
|
|
19
|
+
elementId: t
|
|
20
|
+
}), o.attributes?.default && a++) : e.push({
|
|
21
|
+
code: "INVALID_PAGE_REFERENCE",
|
|
22
|
+
message: `Page ID "${t}" in pages array doesn't exist in flat`,
|
|
23
|
+
elementId: t
|
|
24
|
+
});
|
|
25
|
+
}), a === 0 && n.length > 0 ? t.push({
|
|
26
|
+
code: "NO_DEFAULT_PAGE",
|
|
27
|
+
message: "No default page found in schema"
|
|
28
|
+
}) : a > 1 && t.push({
|
|
29
|
+
code: "MULTIPLE_DEFAULT_PAGES",
|
|
30
|
+
message: `Found ${a} default pages, expected 1`
|
|
31
|
+
});
|
|
32
|
+
}, n = ({ errors: e, getElement: t, rootIds: n }) => {
|
|
33
|
+
n().forEach((n) => {
|
|
34
|
+
let r = t(n);
|
|
35
|
+
if (!r) return;
|
|
36
|
+
let i = /* @__PURE__ */ new Set([n]), a = (r) => {
|
|
37
|
+
let o = t(r);
|
|
38
|
+
o?.definition && !i.has(r) && (i.add(r), o.definition.rootId !== n && e.push({
|
|
39
|
+
code: "ROOT_ID_MISMATCH",
|
|
40
|
+
message: `Element "${r}" has rootId "${o.definition.rootId}" but should be "${n}" (its root's ID)`,
|
|
41
|
+
elementId: r,
|
|
42
|
+
details: {
|
|
43
|
+
expectedRootId: n,
|
|
44
|
+
actualRootId: o.definition.rootId
|
|
45
|
+
}
|
|
46
|
+
}), o.definition.items && o.definition.items.forEach((e) => a(e)));
|
|
47
|
+
};
|
|
48
|
+
r.definition.items && r.definition.items.forEach((e) => a(e));
|
|
49
|
+
});
|
|
50
|
+
}, r = ({ errors: e, pageFolders: t }) => {
|
|
51
|
+
let n = /* @__PURE__ */ new Set();
|
|
52
|
+
t.forEach((t, r) => {
|
|
53
|
+
t.id ? (n.has(t.id) && e.push({
|
|
54
|
+
code: "DUPLICATE_FOLDER",
|
|
55
|
+
message: `Duplicate folder ID "${t.id}"`,
|
|
56
|
+
elementId: t.id
|
|
57
|
+
}), n.add(t.id), t.parentId && (n.has(t.parentId) || e.push({
|
|
58
|
+
code: "ORPHANED_FOLDER_PARENT",
|
|
59
|
+
message: `Folder "${t.id}" has non-existent parent "${t.parentId}"`,
|
|
60
|
+
elementId: t.id,
|
|
61
|
+
details: { parentId: t.parentId }
|
|
62
|
+
}))) : e.push({
|
|
63
|
+
code: "INVALID_FOLDER",
|
|
64
|
+
message: `Page folder at index ${r} has no id`
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}, i = ({ errors: t, getElement: n, layoutRootIds: r }) => {
|
|
68
|
+
let i = /* @__PURE__ */ new Set();
|
|
69
|
+
r().forEach((r) => {
|
|
70
|
+
let a = e(n, r);
|
|
71
|
+
a.length === 0 || i.has(a[0]) || (a.forEach((e) => i.add(e)), t.push({
|
|
72
|
+
code: "LAYOUT_CYCLE",
|
|
73
|
+
message: `Layouts sit inside each other in a circle: ${a.join(" → ")}`,
|
|
74
|
+
elementId: r,
|
|
75
|
+
details: { cycle: a }
|
|
76
|
+
}));
|
|
77
|
+
});
|
|
78
|
+
}, a = ({ warnings: e, flat: t, baseElementId: n, getElement: r, rootIds: i }) => {
|
|
79
|
+
let a = /* @__PURE__ */ new Set(), o = (e) => {
|
|
80
|
+
if (a.has(e)) return;
|
|
81
|
+
a.add(e);
|
|
82
|
+
let t = r(e);
|
|
83
|
+
t?.definition?.items && t.definition.items.forEach((e) => o(e));
|
|
84
|
+
}, s = /* @__PURE__ */ new Set(), c = (e) => {
|
|
85
|
+
if (s.has(e)) return;
|
|
86
|
+
s.add(e), o(e);
|
|
87
|
+
let t = r(e)?.attributes;
|
|
88
|
+
t?.layout && c(t.layout), t?.layoutContainer && o(t.layoutContainer);
|
|
89
|
+
};
|
|
90
|
+
n ? c(n) : i().forEach(c);
|
|
91
|
+
let l = new Set(i());
|
|
92
|
+
Object.keys(t).forEach((t) => {
|
|
93
|
+
let n = r(t);
|
|
94
|
+
n && !a.has(t) && n.definition.type !== "page" && !l.has(t) && e.push({
|
|
95
|
+
code: "ORPHANED_ELEMENT",
|
|
96
|
+
message: `Element "${t}" is not reachable from any page or layout`,
|
|
97
|
+
elementId: t
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
//#endregion
|
|
102
|
+
export { i as validateLayoutCycles, a as validateOrphanedElements, r as validatePageFolders, t as validatePages, n as validateRootConsistency };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationContext } from './context';
|
|
2
|
+
/** The document's shape: its fields, each element, parents and children agreeing, no cycle, ids that can be named. */
|
|
3
|
+
export declare const validateStructure: ({ errors, flat, pages, pageFolders, variables, components }: ValidationContext) => boolean;
|
|
4
|
+
export declare const validateElements: ({ errors, warnings, flat, elementExists }: ValidationContext) => void;
|
|
5
|
+
export declare const validateParentChildConsistency: ({ errors, flat, getElement }: ValidationContext) => void;
|
|
6
|
+
export declare const validateCircularReferences: ({ errors, flat, getElement }: ValidationContext) => void;
|
|
7
|
+
export declare const validateElementIds: ({ errors, flat }: ValidationContext) => void;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { isValidElementId as e } from "../elementId.mjs";
|
|
2
|
+
//#region src/helpers/schemaValidator/structure.ts
|
|
3
|
+
var t = ({ errors: e, flat: t, pages: n, pageFolders: r, variables: i, components: a }) => !t || typeof t != "object" ? (e.push({
|
|
4
|
+
code: "INVALID_FLAT",
|
|
5
|
+
message: "Schema.flat must be a valid Record<string, Element>"
|
|
6
|
+
}), !1) : Array.isArray(n) ? Array.isArray(r) ? Array.isArray(i) ? !a || typeof a != "object" ? (e.push({
|
|
7
|
+
code: "INVALID_COMPONENTS",
|
|
8
|
+
message: "Schema.components must be a Record<string, SpaceComponent>"
|
|
9
|
+
}), !1) : !0 : (e.push({
|
|
10
|
+
code: "INVALID_VARIABLES",
|
|
11
|
+
message: "Schema.variables must be an array"
|
|
12
|
+
}), !1) : (e.push({
|
|
13
|
+
code: "INVALID_PAGE_FOLDERS",
|
|
14
|
+
message: "Schema.pageFolders must be an array"
|
|
15
|
+
}), !1) : (e.push({
|
|
16
|
+
code: "INVALID_PAGES",
|
|
17
|
+
message: "Schema.pages must be an array of element IDs"
|
|
18
|
+
}), !1), n = ({ errors: e, warnings: t, flat: n, elementExists: r }) => {
|
|
19
|
+
Object.entries(n).forEach(([n, i]) => {
|
|
20
|
+
if (!i) {
|
|
21
|
+
e.push({
|
|
22
|
+
code: "NULL_ELEMENT",
|
|
23
|
+
message: `Element with id "${n}" is null or undefined`,
|
|
24
|
+
elementId: n
|
|
25
|
+
});
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (!i.id) {
|
|
29
|
+
e.push({
|
|
30
|
+
code: "MISSING_ID",
|
|
31
|
+
message: `Element at key "${n}" has no id property`
|
|
32
|
+
});
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (i.id !== n && e.push({
|
|
36
|
+
code: "ID_MISMATCH",
|
|
37
|
+
message: `Element id "${i.id}" doesn't match flat key "${n}"`,
|
|
38
|
+
elementId: n
|
|
39
|
+
}), !i.definition) {
|
|
40
|
+
e.push({
|
|
41
|
+
code: "MISSING_DEFINITION",
|
|
42
|
+
message: "Element missing definition",
|
|
43
|
+
elementId: n
|
|
44
|
+
});
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
let { definition: a, attributes: o } = i;
|
|
48
|
+
a.type || e.push({
|
|
49
|
+
code: "MISSING_TYPE",
|
|
50
|
+
message: "Element missing definition.type",
|
|
51
|
+
elementId: n
|
|
52
|
+
}), a.label === void 0 && e.push({
|
|
53
|
+
code: "MISSING_LABEL",
|
|
54
|
+
message: "Element missing definition.label",
|
|
55
|
+
elementId: n
|
|
56
|
+
}), a.rootId || e.push({
|
|
57
|
+
code: "MISSING_ROOT_ID",
|
|
58
|
+
message: "Element missing definition.rootId",
|
|
59
|
+
elementId: n
|
|
60
|
+
}), (!a.styleSelectors || typeof a.styleSelectors != "object") && e.push({
|
|
61
|
+
code: "MISSING_STYLE_SELECTORS",
|
|
62
|
+
message: "Element missing or invalid definition.styleSelectors",
|
|
63
|
+
elementId: n
|
|
64
|
+
}), o || t.push({
|
|
65
|
+
code: "MISSING_ATTRIBUTES",
|
|
66
|
+
message: "Element has no attributes",
|
|
67
|
+
elementId: n
|
|
68
|
+
}), a.items && (Array.isArray(a.items) ? a.items.forEach((t, i) => {
|
|
69
|
+
r(t) || e.push({
|
|
70
|
+
code: "ORPHANED_ITEM_REFERENCE",
|
|
71
|
+
message: `Element "${n}" references non-existent child "${t}" at items[${i}]`,
|
|
72
|
+
elementId: n,
|
|
73
|
+
details: {
|
|
74
|
+
childId: t,
|
|
75
|
+
index: i
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}) : e.push({
|
|
79
|
+
code: "INVALID_ITEMS",
|
|
80
|
+
message: "Element.definition.items must be an array",
|
|
81
|
+
elementId: n
|
|
82
|
+
})), a.parentId && !r(a.parentId) && e.push({
|
|
83
|
+
code: "ORPHANED_PARENT_REFERENCE",
|
|
84
|
+
message: `Element "${n}" has parentId "${a.parentId}" but parent doesn't exist in flat`,
|
|
85
|
+
elementId: n,
|
|
86
|
+
details: { parentId: a.parentId }
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}, r = ({ errors: e, flat: t, getElement: n }) => {
|
|
90
|
+
Object.values(t).forEach((t) => {
|
|
91
|
+
if (!t?.definition) return;
|
|
92
|
+
let { id: r, definition: i } = t;
|
|
93
|
+
if (i.items && i.items.forEach((t) => {
|
|
94
|
+
let i = n(t);
|
|
95
|
+
i?.definition && i.definition.parentId !== r && e.push({
|
|
96
|
+
code: "PARENT_CHILD_MISMATCH",
|
|
97
|
+
message: `Element "${t}" is in "${r}".items but has parentId "${i.definition.parentId}"`,
|
|
98
|
+
elementId: t,
|
|
99
|
+
details: {
|
|
100
|
+
expectedParent: r,
|
|
101
|
+
actualParent: i.definition.parentId
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}), i.parentId) {
|
|
105
|
+
let t = n(i.parentId);
|
|
106
|
+
if (!t?.definition) return;
|
|
107
|
+
let a = t.definition.items || [];
|
|
108
|
+
a.includes(r) || e.push({
|
|
109
|
+
code: "PARENT_CHILD_MISMATCH",
|
|
110
|
+
message: `Element "${r}" has parentId "${i.parentId}" but parent doesn't have it in items`,
|
|
111
|
+
elementId: r,
|
|
112
|
+
details: {
|
|
113
|
+
parentId: i.parentId,
|
|
114
|
+
parentItems: a
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}, i = ({ errors: e, flat: t, getElement: n }) => {
|
|
120
|
+
let r = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Set(), a = (t) => {
|
|
121
|
+
if (i.has(t)) return e.push({
|
|
122
|
+
code: "CIRCULAR_REFERENCE",
|
|
123
|
+
message: `Circular reference detected involving element "${t}"`,
|
|
124
|
+
elementId: t
|
|
125
|
+
}), !0;
|
|
126
|
+
if (r.has(t)) return !1;
|
|
127
|
+
r.add(t), i.add(t);
|
|
128
|
+
let o = n(t);
|
|
129
|
+
if (o?.definition?.items) {
|
|
130
|
+
for (let e of o.definition.items) if (a(e)) return i.delete(t), !0;
|
|
131
|
+
}
|
|
132
|
+
let s = /* @__PURE__ */ new Set(), c = o;
|
|
133
|
+
for (; c?.definition?.parentId;) {
|
|
134
|
+
if (s.has(c.id)) return e.push({
|
|
135
|
+
code: "CIRCULAR_PARENT_REFERENCE",
|
|
136
|
+
message: `Circular parent reference detected involving element "${c.id}"`,
|
|
137
|
+
elementId: c.id
|
|
138
|
+
}), !0;
|
|
139
|
+
s.add(c.id), c = n(c.definition.parentId);
|
|
140
|
+
}
|
|
141
|
+
return i.delete(t), !1;
|
|
142
|
+
};
|
|
143
|
+
Object.keys(t).forEach((e) => {
|
|
144
|
+
r.has(e) || a(e);
|
|
145
|
+
});
|
|
146
|
+
}, a = ({ errors: t, flat: n }) => {
|
|
147
|
+
Object.values(n).forEach((n) => {
|
|
148
|
+
n && (e(n.id) || t.push({
|
|
149
|
+
code: "INVALID_ELEMENT_ID",
|
|
150
|
+
message: `Element "${n.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)`,
|
|
151
|
+
elementId: n.id
|
|
152
|
+
}));
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
//#endregion
|
|
156
|
+
export { i as validateCircularReferences, a as validateElementIds, n as validateElements, r as validateParentChildConsistency, t as validateStructure };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Schema } from '@plitzi/sdk-shared';
|
|
2
1
|
export type SchemaValidationOptions = {
|
|
3
2
|
/** Validates `flat` as a fragment hung off this element — a component's tree — instead of as the pages' tree. */
|
|
4
3
|
baseElementId?: string;
|
|
@@ -40,6 +39,3 @@ export type SchemaValidationResult = {
|
|
|
40
39
|
export declare const REFERENCE_ERROR_CODES: ReadonlySet<string>;
|
|
41
40
|
/** Whether an error is the document itself being broken, as opposed to a reference an edit left dangling. */
|
|
42
41
|
export declare const isIntegrityError: (error: SchemaValidationError) => boolean;
|
|
43
|
-
export declare const validateSchema: (schema: Schema, options?: SchemaValidationOptions) => SchemaValidationResult;
|
|
44
|
-
export declare const assertSchemaValid: (schema: Schema, options?: SchemaValidationOptions, context?: string) => void;
|
|
45
|
-
export declare const isSchemaValid: (schema: Schema, options?: SchemaValidationOptions) => boolean;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/helpers/schemaValidator/types.ts
|
|
2
|
+
var e = /* @__PURE__ */ new Set([
|
|
3
|
+
"UNRESOLVED_BINDING_SOURCE",
|
|
4
|
+
"MISMATCHED_BINDING_SOURCE",
|
|
5
|
+
"UNRESOLVED_INTERACTION_TARGET",
|
|
6
|
+
"UNRESOLVED_COMPONENT",
|
|
7
|
+
"UNKNOWN_COMPONENT_SLOT"
|
|
8
|
+
]), t = (t) => !e.has(t.code);
|
|
9
|
+
//#endregion
|
|
10
|
+
export { e as REFERENCE_ERROR_CODES, t as isIntegrityError };
|
package/dist/index.mjs
CHANGED
|
@@ -4,6 +4,7 @@ import f from "./helpers/FlatMap.mjs";
|
|
|
4
4
|
import { RESERVED_PROP_NAMES as p, addComponent as m, componentNamed as h, detachInstance as g, documentIds as _, flatMapOf as v, instancesOf as y, isInstance as b, propNameProblem as x, removeComponent as S, renameElement as C, treeOf as w, treesOf as T, updateComponent as E } from "./helpers/components.mjs";
|
|
5
5
|
import D, { SchemaActions as O } from "./SchemaReducer.mjs";
|
|
6
6
|
import k from "./helpers/fitSnippet.mjs";
|
|
7
|
-
import { REFERENCE_ERROR_CODES as A,
|
|
7
|
+
import { REFERENCE_ERROR_CODES as A, isIntegrityError as j } from "./helpers/schemaValidator/types.mjs";
|
|
8
|
+
import { assertSchemaValid as M, isSchemaValid as N, validateSchema as P } from "./helpers/schemaValidator/index.mjs";
|
|
8
9
|
import { rendersNoTag as F, styleWithoutTag as I } from "./helpers/styleWithoutTag.mjs";
|
|
9
|
-
export { f as FlatMap, A as REFERENCE_ERROR_CODES, p as RESERVED_PROP_NAMES, O as SchemaActions, D as SchemaReducer, m as addComponent,
|
|
10
|
+
export { f as FlatMap, A as REFERENCE_ERROR_CODES, p as RESERVED_PROP_NAMES, O as SchemaActions, D as SchemaReducer, m as addComponent, M as assertSchemaValid, h as componentNamed, s as descendants, g as detachInstance, _ as documentIds, e as elementIdConflict, c as elementsByRoot, k as fitSnippet, v as flatMapOf, y as instancesOf, b as isInstance, j as isIntegrityError, N as isSchemaValid, t as isValidElementId, l as parentChain, n as positionalElementId, x as propNameProblem, r as randomElementId, S as removeComponent, C as renameElement, u as renderContext, F as rendersNoTag, i as repointIds, d as rootName, a as slugifyElementId, I as styleWithoutTag, w as treeOf, T as treesOf, o as uniqueElementId, E as updateComponent, P as validateSchema };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plitzi/sdk-schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.39.0",
|
|
4
4
|
"license": "AGPL-3.0",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"build:prod": "vite build"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@plitzi/plitzi-ui": "^1.6.
|
|
34
|
-
"@plitzi/sdk-shared": "0.
|
|
35
|
-
"@plitzi/sdk-style": "0.
|
|
33
|
+
"@plitzi/plitzi-ui": "^1.6.36",
|
|
34
|
+
"@plitzi/sdk-shared": "0.39.0",
|
|
35
|
+
"@plitzi/sdk-style": "0.39.0",
|
|
36
36
|
"immer": "^11.1.21"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|