@satoshibits/react-json-schema-builder 1.1.0 → 1.1.2
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/dist/components/ui/button.d.ts.map +1 -1
- package/dist/index.cjs10.js +1 -1
- package/dist/index.cjs11.js +1 -1
- package/dist/index.cjs11.js.map +1 -1
- package/dist/index.cjs2.js +1 -1
- package/dist/index.cjs2.js.map +1 -1
- package/dist/index.cjs23.js +1 -1
- package/dist/index.cjs24.js +1 -1
- package/dist/index.cjs25.js +1 -1
- package/dist/index.cjs26.js +1 -1
- package/dist/index.cjs27.js +1 -1
- package/dist/index.cjs3.js +1 -1
- package/dist/index.cjs3.js.map +1 -1
- package/dist/index.cjs30.js +1 -1
- package/dist/index.cjs31.js +1 -1
- package/dist/index.cjs34.js +1 -1
- package/dist/index.cjs36.js +1 -1
- package/dist/index.cjs37.js +1 -1
- package/dist/index.cjs38.js +1 -1
- package/dist/index.cjs39.js +1 -1
- package/dist/index.cjs40.js +1 -1
- package/dist/index.cjs41.js +1 -1
- package/dist/index.cjs43.js +1 -1
- package/dist/index.cjs47.js +1 -1
- package/dist/index.cjs49.js +6 -1
- package/dist/index.cjs49.js.map +1 -1
- package/dist/index.cjs50.js +6 -1
- package/dist/index.cjs50.js.map +1 -1
- package/dist/index.cjs51.js +2 -2
- package/dist/index.cjs51.js.map +1 -1
- package/dist/index.cjs52.js +2 -2
- package/dist/index.cjs52.js.map +1 -1
- package/dist/index.cjs53.js +1 -6
- package/dist/index.cjs53.js.map +1 -1
- package/dist/index.cjs54.js +1 -6
- package/dist/index.cjs54.js.map +1 -1
- package/dist/index.cjs55.js +1 -5
- package/dist/index.cjs55.js.map +1 -1
- package/dist/index.cjs56.js +1 -1
- package/dist/index.cjs56.js.map +1 -1
- package/dist/index.cjs57.js +1 -1
- package/dist/index.cjs57.js.map +1 -1
- package/dist/index.cjs58.js +1 -1
- package/dist/index.cjs58.js.map +1 -1
- package/dist/index.cjs59.js +1 -1
- package/dist/index.cjs59.js.map +1 -1
- package/dist/index.cjs60.js +1 -1
- package/dist/index.cjs60.js.map +1 -1
- package/dist/index.cjs61.js +1 -1
- package/dist/index.cjs61.js.map +1 -1
- package/dist/index.cjs62.js +1 -1
- package/dist/index.cjs62.js.map +1 -1
- package/dist/index.cjs63.js +1 -1
- package/dist/index.cjs63.js.map +1 -1
- package/dist/index.cjs64.js +1 -1
- package/dist/index.cjs64.js.map +1 -1
- package/dist/index.cjs65.js +1 -1
- package/dist/index.cjs65.js.map +1 -1
- package/dist/index.cjs66.js +1 -1
- package/dist/index.cjs66.js.map +1 -1
- package/dist/index.cjs67.js +1 -1
- package/dist/index.cjs67.js.map +1 -1
- package/dist/index.cjs68.js +1 -1
- package/dist/index.cjs68.js.map +1 -1
- package/dist/index.cjs69.js +1 -1
- package/dist/index.cjs69.js.map +1 -1
- package/dist/index.cjs70.js +1 -1
- package/dist/index.cjs70.js.map +1 -1
- package/dist/index.cjs71.js +1 -1
- package/dist/index.cjs71.js.map +1 -1
- package/dist/index.cjs72.js +1 -1
- package/dist/index.cjs72.js.map +1 -1
- package/dist/index.cjs73.js +1 -1
- package/dist/index.cjs73.js.map +1 -1
- package/dist/index.cjs74.js +1 -1
- package/dist/index.cjs74.js.map +1 -1
- package/dist/index.cjs75.js +1 -1
- package/dist/index.cjs75.js.map +1 -1
- package/dist/index.cjs76.js +5 -1
- package/dist/index.cjs76.js.map +1 -1
- package/dist/index.cjs77.js +1 -1
- package/dist/index.cjs77.js.map +1 -1
- package/dist/index.cjs78.js +1 -1
- package/dist/index.cjs78.js.map +1 -1
- package/dist/index.cjs79.js +1 -1
- package/dist/index.cjs79.js.map +1 -1
- package/dist/index.cjs80.js +1 -1
- package/dist/index.cjs80.js.map +1 -1
- package/dist/index.cjs81.js +1 -1
- package/dist/index.cjs81.js.map +1 -1
- package/dist/index.cjs82.js +1 -1
- package/dist/index.cjs82.js.map +1 -1
- package/dist/index.cjs87.js +1 -1
- package/dist/index.cjs89.js +1 -1
- package/dist/index.cjs94.js +38 -1
- package/dist/index.cjs94.js.map +1 -1
- package/dist/index.cjs95.js +1 -38
- package/dist/index.cjs95.js.map +1 -1
- package/dist/index.cjs96.js +1 -1
- package/dist/index.cjs96.js.map +1 -1
- package/dist/index.cjs97.js +1 -1
- package/dist/index.cjs97.js.map +1 -1
- package/dist/index.cjs98.js +1 -1
- package/dist/index.cjs98.js.map +1 -1
- package/dist/index.es10.js +1 -1
- package/dist/index.es11.js +4 -3
- package/dist/index.es11.js.map +1 -1
- package/dist/index.es2.js +73 -71
- package/dist/index.es2.js.map +1 -1
- package/dist/index.es23.js +1 -1
- package/dist/index.es24.js +1 -1
- package/dist/index.es25.js +1 -1
- package/dist/index.es26.js +1 -1
- package/dist/index.es27.js +1 -1
- package/dist/index.es3.js +131 -134
- package/dist/index.es3.js.map +1 -1
- package/dist/index.es30.js +1 -1
- package/dist/index.es31.js +1 -1
- package/dist/index.es34.js +1 -1
- package/dist/index.es36.js +7 -7
- package/dist/index.es37.js +20 -20
- package/dist/index.es38.js +1 -1
- package/dist/index.es39.js +1 -1
- package/dist/index.es40.js +1 -1
- package/dist/index.es41.js +1 -1
- package/dist/index.es43.js +1 -1
- package/dist/index.es47.js +4 -4
- package/dist/index.es49.js +21 -8
- package/dist/index.es49.js.map +1 -1
- package/dist/index.es50.js +9 -5
- package/dist/index.es50.js.map +1 -1
- package/dist/index.es51.js +32 -15
- package/dist/index.es51.js.map +1 -1
- package/dist/index.es52.js +12 -3
- package/dist/index.es52.js.map +1 -1
- package/dist/index.es53.js +8 -38
- package/dist/index.es53.js.map +1 -1
- package/dist/index.es54.js +5 -18
- package/dist/index.es54.js.map +1 -1
- package/dist/index.es55.js +30 -252
- package/dist/index.es55.js.map +1 -1
- package/dist/index.es56.js +6 -21
- package/dist/index.es56.js.map +1 -1
- package/dist/index.es57.js +25 -5
- package/dist/index.es57.js.map +1 -1
- package/dist/index.es58.js +62 -23
- package/dist/index.es58.js.map +1 -1
- package/dist/index.es59.js +27 -63
- package/dist/index.es59.js.map +1 -1
- package/dist/index.es60.js +5 -29
- package/dist/index.es60.js.map +1 -1
- package/dist/index.es61.js +24 -5
- package/dist/index.es61.js.map +1 -1
- package/dist/index.es62.js +3 -24
- package/dist/index.es62.js.map +1 -1
- package/dist/index.es63.js +45 -32
- package/dist/index.es63.js.map +1 -1
- package/dist/index.es64.js +7 -3
- package/dist/index.es64.js.map +1 -1
- package/dist/index.es65.js +123 -44
- package/dist/index.es65.js.map +1 -1
- package/dist/index.es66.js +14 -7
- package/dist/index.es66.js.map +1 -1
- package/dist/index.es67.js +125 -116
- package/dist/index.es67.js.map +1 -1
- package/dist/index.es68.js +10 -14
- package/dist/index.es68.js.map +1 -1
- package/dist/index.es69.js +209 -127
- package/dist/index.es69.js.map +1 -1
- package/dist/index.es70.js +12 -10
- package/dist/index.es70.js.map +1 -1
- package/dist/index.es71.js +7 -215
- package/dist/index.es71.js.map +1 -1
- package/dist/index.es72.js +4 -13
- package/dist/index.es72.js.map +1 -1
- package/dist/index.es73.js +27 -7
- package/dist/index.es73.js.map +1 -1
- package/dist/index.es74.js +49 -3
- package/dist/index.es74.js.map +1 -1
- package/dist/index.es75.js +9 -28
- package/dist/index.es75.js.map +1 -1
- package/dist/index.es76.js +253 -48
- package/dist/index.es76.js.map +1 -1
- package/dist/index.es77.js +21 -8
- package/dist/index.es77.js.map +1 -1
- package/dist/index.es78.js +11 -67
- package/dist/index.es78.js.map +1 -1
- package/dist/index.es79.js +203 -10
- package/dist/index.es79.js.map +1 -1
- package/dist/index.es80.js +20 -203
- package/dist/index.es80.js.map +1 -1
- package/dist/index.es81.js +392 -21
- package/dist/index.es81.js.map +1 -1
- package/dist/index.es82.js +61 -386
- package/dist/index.es82.js.map +1 -1
- package/dist/index.es87.js +1 -1
- package/dist/index.es89.js +4 -4
- package/dist/index.es94.js +69 -65
- package/dist/index.es94.js.map +1 -1
- package/dist/index.es95.js +8 -68
- package/dist/index.es95.js.map +1 -1
- package/dist/index.es96.js +14 -9
- package/dist/index.es96.js.map +1 -1
- package/dist/index.es97.js +62 -14
- package/dist/index.es97.js.map +1 -1
- package/dist/index.es98.js +64 -61
- package/dist/index.es98.js.map +1 -1
- package/dist/packages/builder/context.d.ts +3 -1
- package/dist/packages/builder/context.d.ts.map +1 -1
- package/dist/packages/builder/schema-builder.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/index.es2.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { createContext as
|
|
3
|
-
import { a as
|
|
4
|
-
import { a as
|
|
5
|
-
const
|
|
1
|
+
import { jsx as x } from "react/jsx-runtime";
|
|
2
|
+
import { createContext as v, useState as y, useContext as C } from "react";
|
|
3
|
+
import { a as R } from "./index.es9.js";
|
|
4
|
+
import { a as m } from "./index.es10.js";
|
|
5
|
+
const q = v(void 0), A = {
|
|
6
6
|
$schema: "http://json-schema.org/draft-07/schema#",
|
|
7
7
|
type: "object",
|
|
8
8
|
title: "title",
|
|
@@ -10,52 +10,54 @@ const y = x(void 0), R = {
|
|
|
10
10
|
properties: {},
|
|
11
11
|
required: []
|
|
12
12
|
};
|
|
13
|
-
function
|
|
14
|
-
children:
|
|
15
|
-
initialSchema:
|
|
13
|
+
function V({
|
|
14
|
+
children: p,
|
|
15
|
+
initialSchema: f
|
|
16
16
|
}) {
|
|
17
|
-
const [
|
|
18
|
-
|
|
19
|
-
), [
|
|
20
|
-
return /* @__PURE__ */
|
|
17
|
+
const [c, n] = y(
|
|
18
|
+
f ?? A
|
|
19
|
+
), [u, l] = y([]);
|
|
20
|
+
return /* @__PURE__ */ x(q.Provider, { value: { schema: c, setSchema: n, path: u, setPath: l }, children: p });
|
|
21
21
|
}
|
|
22
|
-
function
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
function K({
|
|
23
|
+
onSchemaChange: p
|
|
24
|
+
} = {}) {
|
|
25
|
+
const f = C(q);
|
|
26
|
+
if (f === void 0)
|
|
25
27
|
throw new Error("useSchema must be used within a SchemaProvider");
|
|
26
|
-
const { path:
|
|
27
|
-
let
|
|
28
|
+
const { path: c, schema: n, setSchema: u } = f, l = (t, e) => {
|
|
29
|
+
let r = t, i = null, o = null;
|
|
28
30
|
for (const s of e)
|
|
29
|
-
|
|
31
|
+
r.type === "array" && r.items && !Array.isArray(r.items) && typeof r.items == "object" && r.items.type === "object" && typeof r.items.properties?.[s] == "object" ? (o = r.items, i = r.items.properties[s], r = r.items) : r.type === "object" && r.properties && !Array.isArray(r.properties) && typeof r.properties[s] == "object" && (o = r, i = r.properties[s], r = r.properties[s]);
|
|
30
32
|
return { propertySchema: i, parentSchema: o };
|
|
31
|
-
},
|
|
32
|
-
const { propertySchema: e } =
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
}, a = (t) => {
|
|
34
|
+
const { propertySchema: e } = l(
|
|
35
|
+
t ?? n,
|
|
36
|
+
c
|
|
35
37
|
);
|
|
36
|
-
return e ? e.type === "array" ? e.items : e :
|
|
37
|
-
},
|
|
38
|
-
const { propertySchema:
|
|
39
|
-
return
|
|
40
|
-
},
|
|
41
|
-
const
|
|
42
|
-
if (i.properties?.[
|
|
38
|
+
return e ? e.type === "array" ? e.items : e : t ?? n;
|
|
39
|
+
}, b = () => {
|
|
40
|
+
const { propertySchema: t } = l(n, c);
|
|
41
|
+
return t;
|
|
42
|
+
}, j = (t, e) => {
|
|
43
|
+
const r = m(n), i = a(r);
|
|
44
|
+
if (i.properties?.[t]) {
|
|
43
45
|
const o = Object.entries(i.properties).map(
|
|
44
|
-
([s, g]) => [s ===
|
|
46
|
+
([s, g]) => [s === t ? e : s, g]
|
|
45
47
|
);
|
|
46
48
|
i.properties = Object.fromEntries(
|
|
47
49
|
o
|
|
48
50
|
);
|
|
49
51
|
}
|
|
50
|
-
i.required?.includes(
|
|
51
|
-
(o) => o ===
|
|
52
|
-
)), u(
|
|
53
|
-
},
|
|
54
|
-
const
|
|
55
|
-
typeof e == "object" && "isRequired" in e && (i.type === "object" && (i.required || (i.required = []), e.isRequired ? i.required.includes(
|
|
52
|
+
i.required?.includes(t) && (i.required = i.required.map(
|
|
53
|
+
(o) => o === t ? e : o
|
|
54
|
+
)), u(r), p?.(r);
|
|
55
|
+
}, P = (t, e) => {
|
|
56
|
+
const r = m(n), i = a(r);
|
|
57
|
+
typeof e == "object" && "isRequired" in e && (i.type === "object" && (i.required || (i.required = []), e.isRequired ? i.required.includes(t) || i.required.push(t) : i.required = i.required.filter((s) => s !== t)), delete e.isRequired);
|
|
56
58
|
let o = !1;
|
|
57
59
|
if (i.type === "object" && i.properties) {
|
|
58
|
-
const s = i.properties[
|
|
60
|
+
const s = i.properties[t];
|
|
59
61
|
typeof s != "boolean" && s && typeof e != "boolean" && (e.type && e.type !== s.type ? e = {
|
|
60
62
|
title: s.title,
|
|
61
63
|
description: s.description,
|
|
@@ -65,53 +67,53 @@ function V() {
|
|
|
65
67
|
title: s.title,
|
|
66
68
|
description: s.description,
|
|
67
69
|
...e
|
|
68
|
-
}) : e = { ...s, ...e }, i.properties[
|
|
70
|
+
}) : e = { ...s, ...e }, i.properties[t] = e), o = !0;
|
|
69
71
|
}
|
|
70
|
-
if (u(
|
|
71
|
-
return { status: "success", property: i.properties[
|
|
72
|
-
},
|
|
73
|
-
const i =
|
|
74
|
-
return o.type !== "object" && (o.type = "object"), o.properties || (o.properties = {}), o.properties[
|
|
72
|
+
if (u(r), p?.(r), o)
|
|
73
|
+
return { status: "success", property: i.properties[t] };
|
|
74
|
+
}, d = (t, e, r) => {
|
|
75
|
+
const i = m(n), o = a(i);
|
|
76
|
+
return o.type !== "object" && (o.type = "object"), o.properties || (o.properties = {}), o.properties[t] ? {
|
|
75
77
|
status: "error",
|
|
76
78
|
code: "key-exists",
|
|
77
|
-
message: `Property with key ${
|
|
78
|
-
} : (e.enum && (delete e.type, delete e.items, delete e.properties), o.properties[
|
|
79
|
+
message: `Property with key ${t} already exists`
|
|
80
|
+
} : (e.enum && (delete e.type, delete e.items, delete e.properties), o.properties[t] = e, r && (o.required || (o.required = []), o.required.includes(t) || o.required.push(t)), u(i), p?.(i), {
|
|
79
81
|
status: "success",
|
|
80
|
-
property: o.properties[
|
|
82
|
+
property: o.properties[t]
|
|
81
83
|
});
|
|
82
|
-
}, S = (
|
|
83
|
-
const e =
|
|
84
|
-
|
|
85
|
-
},
|
|
86
|
-
const e =
|
|
87
|
-
if (!e ||
|
|
84
|
+
}, S = (t) => {
|
|
85
|
+
const e = m(n), r = a(e);
|
|
86
|
+
r.type === "object" && r.required && (r.required = r.required.filter((i) => i !== t)), r.type === "object" && r.properties && delete r.properties[t], u(e), p?.(e);
|
|
87
|
+
}, w = (t) => {
|
|
88
|
+
const e = a().properties;
|
|
89
|
+
if (!e || R(e) || typeof e[t] == "boolean")
|
|
88
90
|
return;
|
|
89
|
-
const
|
|
90
|
-
return
|
|
91
|
-
},
|
|
92
|
-
if (
|
|
93
|
-
const { propertySchema: e } =
|
|
94
|
-
return e?.type === "array" && e.items && typeof e.items != "boolean" && !Array.isArray(e.items) && e.items?.type === "object" ? !!e.items?.required?.includes(
|
|
91
|
+
const r = e[t], i = `${t}_copy`, o = m(r);
|
|
92
|
+
return d(i, o, h(t));
|
|
93
|
+
}, h = (t) => {
|
|
94
|
+
if (c.length) {
|
|
95
|
+
const { propertySchema: e } = l(n, c);
|
|
96
|
+
return e?.type === "array" && e.items && typeof e.items != "boolean" && !Array.isArray(e.items) && e.items?.type === "object" ? !!e.items?.required?.includes(t) : !!e?.required?.includes(t);
|
|
95
97
|
}
|
|
96
|
-
return !!n?.required?.includes(
|
|
98
|
+
return !!n?.required?.includes(t);
|
|
97
99
|
};
|
|
98
100
|
return {
|
|
99
|
-
...
|
|
100
|
-
getCurrentSchema:
|
|
101
|
-
getCurrentProperty:
|
|
102
|
-
handlePropertyChange:
|
|
103
|
-
handleAddProperty:
|
|
104
|
-
handleKeyChange:
|
|
105
|
-
navigateToCorrectNesting:
|
|
101
|
+
...f,
|
|
102
|
+
getCurrentSchema: a,
|
|
103
|
+
getCurrentProperty: b,
|
|
104
|
+
handlePropertyChange: P,
|
|
105
|
+
handleAddProperty: d,
|
|
106
|
+
handleKeyChange: j,
|
|
107
|
+
navigateToCorrectNesting: l,
|
|
106
108
|
handleDeleteProperty: S,
|
|
107
|
-
isPropertyRequired:
|
|
109
|
+
isPropertyRequired: h,
|
|
108
110
|
getValue: () => n,
|
|
109
|
-
handleDuplicateProperty:
|
|
111
|
+
handleDuplicateProperty: w
|
|
110
112
|
};
|
|
111
113
|
}
|
|
112
114
|
export {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
115
|
+
q as SchemaContext,
|
|
116
|
+
V as SchemaProvider,
|
|
117
|
+
K as useSchema
|
|
116
118
|
};
|
|
117
119
|
//# sourceMappingURL=index.es2.js.map
|
package/dist/index.es2.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es2.js","sources":["../src/packages/builder/context.tsx"],"sourcesContent":["import { createContext, useContext, useState } from \"react\";\n\nimport type { PropsWithChildren, SetStateAction } from \"react\";\nimport type { JSONSchema7, JSONSchema7Definition } from \"json-schema\";\nimport { clone, isEmpty } from \"remeda\";\nimport type { OperationError } from \"./shared-types\";\n\ninterface ContextState<T extends JSONSchema7> {\n schema: T;\n setSchema: React.Dispatch<T>;\n path: string[];\n setPath: React.Dispatch<SetStateAction<string[]>>;\n}\n\nexport const SchemaContext = createContext<\n ContextState<JSONSchema7> | undefined\n>(undefined);\n\nconst defaultSchema: JSONSchema7 = {\n $schema: \"http://json-schema.org/draft-07/schema#\",\n type: \"object\",\n title: \"title\",\n description: \"\",\n properties: {},\n required: [],\n};\n\nfunction SchemaProvider({\n children,\n initialSchema,\n}: PropsWithChildren<{ initialSchema?: JSONSchema7 }>) {\n const [schema, setSchema] = useState<JSONSchema7>(\n initialSchema ?? defaultSchema,\n );\n\n /**\n * Path represents the path to a nested property in the schema.\n * It should only contain property keys.\n * An empty path means the current schema is the root schema since\n * the path only represents nested properties.\n */\n const [path, setPath] = useState<string[]>([]);\n\n // NOTE: you *might* need to memoize your values\n // Learn more in http://kcd.im/optimize-context\n\n return (\n <SchemaContext.Provider value={{ schema, setSchema, path, setPath }}>\n {children}\n </SchemaContext.Provider>\n );\n}\n\nexport type PropertyAddError = OperationError & {\n status: \"error\";\n};\n\nexport interface PropertyAddSuccess {\n status: \"success\";\n property: JSONSchema7;\n}\n\nexport interface PropertyChangeSuccess {\n status: \"success\";\n property: JSONSchema7Definition;\n}\n\nfunction useSchema() {\n const context = useContext(SchemaContext);\n\n if (context === undefined) {\n throw new Error(\"useSchema must be used within a SchemaProvider\");\n }\n\n const { path, schema, setSchema } = context;\n\n /**\n * Navigates to the correct property schema of the latest key in the provided path.\n *\n * It will traverse both \"properties\" and \"items\" to find the correct property schema.\n *\n * It will only return a property schema if the provided path is valid i.e. it\n * correctly follows the schema's nesting structure.\n *\n * It will only return a parent schema if the provided path is valid i.e. it\n * correctly follows the schema's nesting structure.\n */\n const navigateToCorrectNesting = (\n providedSchema: JSONSchema7,\n providedPath: string[],\n ) => {\n let current = providedSchema;\n let propertySchema: JSONSchema7 | null = null;\n // Should be the schema of type \"object\" containing the property in its properties key\n let parentSchema: JSONSchema7 | null = null;\n\n for (const p of providedPath) {\n if (\n current.type === \"array\" &&\n current.items &&\n !Array.isArray(current.items) &&\n typeof current.items === \"object\" &&\n current.items.type === \"object\" &&\n typeof current.items.properties?.[p] === \"object\"\n ) {\n parentSchema = current.items;\n propertySchema = current.items.properties[p];\n current = current.items;\n } else if (\n current.type === \"object\" &&\n current.properties &&\n !Array.isArray(current.properties) &&\n typeof current.properties[p] === \"object\"\n ) {\n parentSchema = current;\n propertySchema = current.properties[p];\n current = current.properties[p];\n }\n }\n\n return { propertySchema, parentSchema };\n };\n\n /**\n * Gets the current schema of the latest key in the path.\n * If path is empty, it will return the root schema.\n *\n * It should always return the \"object\" schema of the current path.\n */\n const getCurrentSchema = (initialSchema?: JSONSchema7) => {\n // Gets the correct object schema if under nesting\n // Otherwise, return the root schema\n const { propertySchema } = navigateToCorrectNesting(\n initialSchema ?? schema,\n path,\n );\n\n if (propertySchema) {\n if (propertySchema.type === \"array\") {\n return propertySchema.items as JSONSchema7;\n }\n\n return propertySchema;\n }\n\n return initialSchema ?? schema;\n };\n\n /**\n * Gets the correct property schema of the latest key in the path.\n * If no path is provided, it will return null.\n */\n const getCurrentProperty = () => {\n const { propertySchema } = navigateToCorrectNesting(schema, path);\n\n return propertySchema;\n };\n\n /**\n * Change the key of a property in the schema. It expects\n * the find the key at the current path.\n */\n const handleKeyChange = (key: string, newKey: string) => {\n const newSchema = clone(schema);\n\n // The current schema containing the property to change\n const current: JSONSchema7 = getCurrentSchema(newSchema);\n\n if (current.properties?.[key]) {\n //Important: maintain order as to not confuse users\n const newProperties = Object.entries(current.properties).map(\n ([k, val]) => {\n return [k === key ? newKey : k, val];\n },\n );\n\n current.properties = Object.fromEntries(\n newProperties,\n ) as JSONSchema7[\"properties\"];\n }\n\n if (current.required?.includes(key)) {\n current.required = current.required.map((item) =>\n item === key ? newKey : item,\n );\n }\n\n setSchema(newSchema);\n };\n\n const handlePropertyChange = (\n key: string,\n updates: Partial<JSONSchema7Definition> & { isRequired?: boolean },\n ): PropertyChangeSuccess | undefined => {\n const newSchema = clone(schema);\n\n // The current schema containing the property to change\n const current: JSONSchema7 = getCurrentSchema(newSchema);\n\n // Handle required field updates\n if (typeof updates === \"object\" && \"isRequired\" in updates) {\n if (current.type === \"object\") {\n if (!current.required) {\n current.required = [];\n }\n if (updates.isRequired) {\n if (!current.required.includes(key)) {\n current.required.push(key);\n }\n } else {\n current.required = current.required.filter((item) => item !== key);\n }\n }\n delete updates.isRequired;\n }\n\n let isUpdated = false;\n\n // Update the property\n if (current.type === \"object\" && current.properties) {\n const originalProperty = current.properties[key];\n // Ensure the property exists and is not a boolean\n if (typeof originalProperty !== \"boolean\" && originalProperty) {\n if (typeof updates !== \"boolean\") {\n //if updates changes the type of the property,\n //only the title, description, and default properties should remain\n //from the original property\n if (updates.type && updates.type !== originalProperty.type) {\n updates = {\n title: originalProperty.title,\n description: originalProperty.description,\n default: originalProperty.default,\n ...updates,\n };\n } else if (updates.enum) {\n //if the property is an enum, only the title and description should remain\n //from the original property\n delete updates.type;\n delete updates.items;\n delete updates.properties;\n updates = {\n title: originalProperty.title,\n description: originalProperty.description,\n ...updates,\n };\n } else {\n updates = { ...originalProperty, ...updates };\n }\n\n current.properties[key] = updates;\n }\n }\n isUpdated = true;\n }\n\n setSchema(newSchema);\n\n if (isUpdated) {\n return { status: \"success\", property: current.properties![key]! };\n }\n };\n\n /**\n * Adds a sibling property to the current schema.\n */\n const handleAddProperty = (\n key: string,\n propertyToAdd: JSONSchema7,\n isRequired: boolean,\n ) => {\n const newSchema = clone(schema);\n\n // The current schema containing the property to add\n const current: JSONSchema7 = getCurrentSchema(newSchema);\n\n // Ensure the current schema has a properties object\n if (current.type !== \"object\") {\n current.type = \"object\";\n }\n if (!current.properties) {\n current.properties = {};\n }\n\n //Ensure there's no existing property with the same key\n if (current.properties[key]) {\n return {\n status: \"error\",\n code: \"key-exists\" as const,\n message: `Property with key ${key} already exists`,\n } satisfies PropertyAddError;\n }\n\n if (propertyToAdd.enum) {\n //ensure that only the enum property is present\n delete propertyToAdd.type;\n delete propertyToAdd.items;\n delete propertyToAdd.properties;\n }\n\n // Add the new property\n current.properties[key] = propertyToAdd;\n\n // Handle required field\n if (isRequired) {\n if (!current.required) {\n current.required = [];\n }\n if (!current.required.includes(key)) {\n current.required.push(key);\n }\n }\n\n setSchema(newSchema);\n\n return {\n status: \"success\" as const,\n property: current.properties[key],\n } satisfies PropertyAddSuccess;\n };\n\n const handleDeleteProperty = (key: string) => {\n const newSchema = clone(schema);\n\n // The current schema containing the property to delete\n const current: JSONSchema7 = getCurrentSchema(newSchema);\n\n // Handle required field updates\n if (current.type === \"object\") {\n if (current.required) {\n current.required = current.required.filter((item) => item !== key);\n }\n }\n\n // Delete the property\n if (current.type === \"object\" && current.properties) {\n delete current.properties[key];\n }\n\n setSchema(newSchema);\n };\n\n const handleDuplicateProperty = (key: string) => {\n const currentProperties = getCurrentSchema().properties;\n if (\n !currentProperties ||\n isEmpty(currentProperties) ||\n typeof currentProperties[key] === \"boolean\"\n ) {\n return;\n }\n\n const propertyToDuplicate = currentProperties[key]!;\n const newKey = `${key}_copy`;\n const newProperty = clone(propertyToDuplicate);\n\n // Add the duplicated property\n return handleAddProperty(newKey, newProperty, isPropertyRequired(key));\n };\n\n const isPropertyRequired = (key: string) => {\n if (path.length) {\n const { propertySchema } = navigateToCorrectNesting(schema, path);\n\n if (\n propertySchema?.type === \"array\" &&\n propertySchema.items &&\n typeof propertySchema.items !== \"boolean\" &&\n !Array.isArray(propertySchema.items) &&\n propertySchema.items?.type === \"object\"\n ) {\n return Boolean(propertySchema.items?.required?.includes(key));\n } else {\n return Boolean(propertySchema?.required?.includes(key));\n }\n }\n\n return Boolean(schema?.required?.includes(key));\n };\n\n /**\n * Programatically get the current schema value\n */\n const getValue = () => {\n return schema;\n };\n\n return {\n ...context,\n getCurrentSchema,\n getCurrentProperty,\n handlePropertyChange,\n handleAddProperty,\n handleKeyChange,\n navigateToCorrectNesting,\n handleDeleteProperty,\n isPropertyRequired,\n getValue,\n handleDuplicateProperty,\n };\n}\n\nexport { SchemaProvider, useSchema };\n"],"names":["SchemaContext","createContext","defaultSchema","SchemaProvider","children","initialSchema","schema","setSchema","useState","path","setPath","jsx","useSchema","context","useContext","navigateToCorrectNesting","providedSchema","providedPath","current","propertySchema","parentSchema","p","getCurrentSchema","getCurrentProperty","handleKeyChange","key","newKey","newSchema","clone","newProperties","k","val","item","handlePropertyChange","updates","isUpdated","originalProperty","handleAddProperty","propertyToAdd","isRequired","handleDeleteProperty","handleDuplicateProperty","currentProperties","isEmpty","propertyToDuplicate","newProperty","isPropertyRequired"],"mappings":";;;;AAca,MAAAA,IAAgBC,EAE3B,MAAS,GAELC,IAA6B;AAAA,EACjC,SAAS;AAAA,EACT,MAAM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,IACA,iBAAAb;AAAA,IACA,0BAAAT;AAAA,IACA,sBAAAyB;AAAA,IACA,oBAAAM;AAAA,IACA,UAde,MACRxC;AAAA,IAcP,yBAAAmC;AAAA,EACF;AACF;"}
|
|
1
|
+
{"version":3,"file":"index.es2.js","sources":["../src/packages/builder/context.tsx"],"sourcesContent":["import { createContext, useContext, useState } from \"react\";\n\nimport type { PropsWithChildren, SetStateAction } from \"react\";\nimport type { JSONSchema7, JSONSchema7Definition } from \"json-schema\";\nimport { clone, isEmpty } from \"remeda\";\nimport type { OperationError } from \"./shared-types\";\n\ninterface ContextState<T extends JSONSchema7> {\n schema: T;\n setSchema: React.Dispatch<T>;\n path: string[];\n setPath: React.Dispatch<SetStateAction<string[]>>;\n}\n\nexport const SchemaContext = createContext<\n ContextState<JSONSchema7> | undefined\n>(undefined);\n\nconst defaultSchema: JSONSchema7 = {\n $schema: \"http://json-schema.org/draft-07/schema#\",\n type: \"object\",\n title: \"title\",\n description: \"\",\n properties: {},\n required: [],\n};\n\nfunction SchemaProvider({\n children,\n initialSchema,\n}: PropsWithChildren<{ initialSchema?: JSONSchema7 }>) {\n const [schema, setSchema] = useState<JSONSchema7>(\n initialSchema ?? defaultSchema,\n );\n\n /**\n * Path represents the path to a nested property in the schema.\n * It should only contain property keys.\n * An empty path means the current schema is the root schema since\n * the path only represents nested properties.\n */\n const [path, setPath] = useState<string[]>([]);\n\n // NOTE: you *might* need to memoize your values\n // Learn more in http://kcd.im/optimize-context\n\n return (\n <SchemaContext.Provider value={{ schema, setSchema, path, setPath }}>\n {children}\n </SchemaContext.Provider>\n );\n}\n\nexport type PropertyAddError = OperationError & {\n status: \"error\";\n};\n\nexport interface PropertyAddSuccess {\n status: \"success\";\n property: JSONSchema7;\n}\n\nexport interface PropertyChangeSuccess {\n status: \"success\";\n property: JSONSchema7Definition;\n}\n\nfunction useSchema({\n onSchemaChange,\n}: {\n onSchemaChange?: (schema: JSONSchema7) => void;\n} = {}) {\n const context = useContext(SchemaContext);\n\n if (context === undefined) {\n throw new Error(\"useSchema must be used within a SchemaProvider\");\n }\n\n const { path, schema, setSchema } = context;\n\n /**\n * Navigates to the correct property schema of the latest key in the provided path.\n *\n * It will traverse both \"properties\" and \"items\" to find the correct property schema.\n *\n * It will only return a property schema if the provided path is valid i.e. it\n * correctly follows the schema's nesting structure.\n *\n * It will only return a parent schema if the provided path is valid i.e. it\n * correctly follows the schema's nesting structure.\n */\n const navigateToCorrectNesting = (\n providedSchema: JSONSchema7,\n providedPath: string[],\n ) => {\n let current = providedSchema;\n let propertySchema: JSONSchema7 | null = null;\n // Should be the schema of type \"object\" containing the property in its properties key\n let parentSchema: JSONSchema7 | null = null;\n\n for (const p of providedPath) {\n if (\n current.type === \"array\" &&\n current.items &&\n !Array.isArray(current.items) &&\n typeof current.items === \"object\" &&\n current.items.type === \"object\" &&\n typeof current.items.properties?.[p] === \"object\"\n ) {\n parentSchema = current.items;\n propertySchema = current.items.properties[p];\n current = current.items;\n } else if (\n current.type === \"object\" &&\n current.properties &&\n !Array.isArray(current.properties) &&\n typeof current.properties[p] === \"object\"\n ) {\n parentSchema = current;\n propertySchema = current.properties[p];\n current = current.properties[p];\n }\n }\n\n return { propertySchema, parentSchema };\n };\n\n /**\n * Gets the current schema of the latest key in the path.\n * If path is empty, it will return the root schema.\n *\n * It should always return the \"object\" schema of the current path.\n */\n const getCurrentSchema = (initialSchema?: JSONSchema7) => {\n // Gets the correct object schema if under nesting\n // Otherwise, return the root schema\n const { propertySchema } = navigateToCorrectNesting(\n initialSchema ?? schema,\n path,\n );\n\n if (propertySchema) {\n if (propertySchema.type === \"array\") {\n return propertySchema.items as JSONSchema7;\n }\n\n return propertySchema;\n }\n\n return initialSchema ?? schema;\n };\n\n /**\n * Gets the correct property schema of the latest key in the path.\n * If no path is provided, it will return null.\n */\n const getCurrentProperty = () => {\n const { propertySchema } = navigateToCorrectNesting(schema, path);\n\n return propertySchema;\n };\n\n /**\n * Change the key of a property in the schema. It expects\n * the find the key at the current path.\n */\n const handleKeyChange = (key: string, newKey: string) => {\n const newSchema = clone(schema);\n\n // The current schema containing the property to change\n const current: JSONSchema7 = getCurrentSchema(newSchema);\n\n if (current.properties?.[key]) {\n //Important: maintain order as to not confuse users\n const newProperties = Object.entries(current.properties).map(\n ([k, val]) => {\n return [k === key ? newKey : k, val];\n },\n );\n\n current.properties = Object.fromEntries(\n newProperties,\n ) as JSONSchema7[\"properties\"];\n }\n\n if (current.required?.includes(key)) {\n current.required = current.required.map((item) =>\n item === key ? newKey : item,\n );\n }\n\n setSchema(newSchema);\n onSchemaChange?.(newSchema);\n };\n\n const handlePropertyChange = (\n key: string,\n updates: Partial<JSONSchema7Definition> & { isRequired?: boolean },\n ): PropertyChangeSuccess | undefined => {\n const newSchema = clone(schema);\n\n // The current schema containing the property to change\n const current: JSONSchema7 = getCurrentSchema(newSchema);\n\n // Handle required field updates\n if (typeof updates === \"object\" && \"isRequired\" in updates) {\n if (current.type === \"object\") {\n if (!current.required) {\n current.required = [];\n }\n if (updates.isRequired) {\n if (!current.required.includes(key)) {\n current.required.push(key);\n }\n } else {\n current.required = current.required.filter((item) => item !== key);\n }\n }\n delete updates.isRequired;\n }\n\n let isUpdated = false;\n\n // Update the property\n if (current.type === \"object\" && current.properties) {\n const originalProperty = current.properties[key];\n // Ensure the property exists and is not a boolean\n if (typeof originalProperty !== \"boolean\" && originalProperty) {\n if (typeof updates !== \"boolean\") {\n //if updates changes the type of the property,\n //only the title, description, and default properties should remain\n //from the original property\n if (updates.type && updates.type !== originalProperty.type) {\n updates = {\n title: originalProperty.title,\n description: originalProperty.description,\n default: originalProperty.default,\n ...updates,\n };\n } else if (updates.enum) {\n //if the property is an enum, only the title and description should remain\n //from the original property\n delete updates.type;\n delete updates.items;\n delete updates.properties;\n updates = {\n title: originalProperty.title,\n description: originalProperty.description,\n ...updates,\n };\n } else {\n updates = { ...originalProperty, ...updates };\n }\n\n current.properties[key] = updates;\n }\n }\n isUpdated = true;\n }\n\n setSchema(newSchema);\n onSchemaChange?.(newSchema);\n\n if (isUpdated) {\n return { status: \"success\", property: current.properties![key]! };\n }\n };\n\n /**\n * Adds a sibling property to the current schema.\n */\n const handleAddProperty = (\n key: string,\n propertyToAdd: JSONSchema7,\n isRequired: boolean,\n ) => {\n const newSchema = clone(schema);\n\n // The current schema containing the property to add\n const current: JSONSchema7 = getCurrentSchema(newSchema);\n\n // Ensure the current schema has a properties object\n if (current.type !== \"object\") {\n current.type = \"object\";\n }\n if (!current.properties) {\n current.properties = {};\n }\n\n //Ensure there's no existing property with the same key\n if (current.properties[key]) {\n return {\n status: \"error\",\n code: \"key-exists\" as const,\n message: `Property with key ${key} already exists`,\n } satisfies PropertyAddError;\n }\n\n if (propertyToAdd.enum) {\n //ensure that only the enum property is present\n delete propertyToAdd.type;\n delete propertyToAdd.items;\n delete propertyToAdd.properties;\n }\n\n // Add the new property\n current.properties[key] = propertyToAdd;\n\n // Handle required field\n if (isRequired) {\n if (!current.required) {\n current.required = [];\n }\n if (!current.required.includes(key)) {\n current.required.push(key);\n }\n }\n\n setSchema(newSchema);\n onSchemaChange?.(newSchema);\n\n return {\n status: \"success\" as const,\n property: current.properties[key],\n } satisfies PropertyAddSuccess;\n };\n\n const handleDeleteProperty = (key: string) => {\n const newSchema = clone(schema);\n\n // The current schema containing the property to delete\n const current: JSONSchema7 = getCurrentSchema(newSchema);\n\n // Handle required field updates\n if (current.type === \"object\") {\n if (current.required) {\n current.required = current.required.filter((item) => item !== key);\n }\n }\n\n // Delete the property\n if (current.type === \"object\" && current.properties) {\n delete current.properties[key];\n }\n\n setSchema(newSchema);\n onSchemaChange?.(newSchema);\n };\n\n const handleDuplicateProperty = (key: string) => {\n const currentProperties = getCurrentSchema().properties;\n if (\n !currentProperties ||\n isEmpty(currentProperties) ||\n typeof currentProperties[key] === \"boolean\"\n ) {\n return;\n }\n\n const propertyToDuplicate = currentProperties[key]!;\n const newKey = `${key}_copy`;\n const newProperty = clone(propertyToDuplicate);\n\n // Add the duplicated property\n return handleAddProperty(newKey, newProperty, isPropertyRequired(key));\n };\n\n const isPropertyRequired = (key: string) => {\n if (path.length) {\n const { propertySchema } = navigateToCorrectNesting(schema, path);\n\n if (\n propertySchema?.type === \"array\" &&\n propertySchema.items &&\n typeof propertySchema.items !== \"boolean\" &&\n !Array.isArray(propertySchema.items) &&\n propertySchema.items?.type === \"object\"\n ) {\n return Boolean(propertySchema.items?.required?.includes(key));\n } else {\n return Boolean(propertySchema?.required?.includes(key));\n }\n }\n\n return Boolean(schema?.required?.includes(key));\n };\n\n /**\n * Programatically get the current schema value\n */\n const getValue = () => {\n return schema;\n };\n\n return {\n ...context,\n getCurrentSchema,\n getCurrentProperty,\n handlePropertyChange,\n handleAddProperty,\n handleKeyChange,\n navigateToCorrectNesting,\n handleDeleteProperty,\n isPropertyRequired,\n getValue,\n handleDuplicateProperty,\n };\n}\n\nexport { SchemaProvider, useSchema };\n"],"names":["SchemaContext","createContext","defaultSchema","SchemaProvider","children","initialSchema","schema","setSchema","useState","path","setPath","jsx","useSchema","onSchemaChange","context","useContext","navigateToCorrectNesting","providedSchema","providedPath","current","propertySchema","parentSchema","p","getCurrentSchema","getCurrentProperty","handleKeyChange","key","newKey","newSchema","clone","newProperties","k","val","item","handlePropertyChange","updates","isUpdated","originalProperty","handleAddProperty","propertyToAdd","isRequired","handleDeleteProperty","handleDuplicateProperty","currentProperties","isEmpty","propertyToDuplicate","newProperty","isPropertyRequired"],"mappings":";;;;AAca,MAAAA,IAAgBC,EAE3B,MAAS,GAELC,IAA6B;AAAA,EACjC,SAAS;AAAA,EACT,MAAM;AAAA,EACN,OAAO;AAAA,EACP,aAAa;AAAA,EACb,YAAY,CAAC;AAAA,EACb,UAAU,CAAA;AACZ;AAEA,SAASC,EAAe;AAAA,EACtB,UAAAC;AAAA,EACA,eAAAC;AACF,GAAuD;AAC/C,QAAA,CAACC,GAAQC,CAAS,IAAIC;AAAA,IAC1BH,KAAiBH;AAAA,EACnB,GAQM,CAACO,GAAMC,CAAO,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;AACF;"}
|
package/dist/index.es23.js
CHANGED
package/dist/index.es24.js
CHANGED
package/dist/index.es25.js
CHANGED
package/dist/index.es26.js
CHANGED
package/dist/index.es27.js
CHANGED