@nolwenture/formik-jsonschema-form 0.8.0 → 0.9.1
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/README.md +124 -0
- package/dist/Form.js +2 -1
- package/dist/Form.stories.d.ts +1 -0
- package/dist/Form.stories.js +7 -0
- package/dist/fields/ArrayField.js +2 -1
- package/dist/fields/MultipleChoiceField.js +0 -2
- package/dist/fields/SchemaField.js +7 -5
- package/dist/fields/stories/AnyField.stories.js +1 -1
- package/dist/fields/stories/ArrayField.stories.js +1 -1
- package/dist/fields/stories/BooleanField.stories.js +1 -1
- package/dist/fields/stories/DateField.stories.js +1 -1
- package/dist/fields/stories/NullField.stories.js +1 -1
- package/dist/fields/stories/NumberField.stories.js +1 -1
- package/dist/fields/stories/ObjectField.stories.js +1 -1
- package/dist/fields/stories/RadioField.stories.js +1 -1
- package/dist/fields/stories/SchemaField.stories.d.ts +43 -0
- package/dist/fields/stories/SchemaField.stories.js +47 -1
- package/dist/fields/stories/SearchMultiTagSelectField.stories.js +1 -1
- package/dist/fields/stories/StringField.stories.js +1 -1
- package/dist/fields/stories/ToggleField.stories.js +1 -1
- package/dist/formEditorSchemas.js +177 -21
- package/dist/sampleData/ageFieldDefinition.d.ts +2 -0
- package/dist/sampleData/ageFieldDefinition.js +5 -0
- package/dist/sampleData/refDefSchema.d.ts +3 -0
- package/dist/sampleData/refDefSchema.js +219 -0
- package/dist/styledComponents.js +0 -2
- package/dist/types.d.ts +4 -2
- package/dist/utils/__tests/createInitialValues.test.js +6 -0
- package/dist/utils/__tests/resolveSchema.test.js +40 -30
- package/dist/utils/__tests/solveReferences.test.d.ts +1 -0
- package/dist/utils/__tests/solveReferences.test.js +12 -0
- package/dist/utils/__tests/testSchemas.d.ts +4 -0
- package/dist/utils/__tests/testSchemas.js +67 -0
- package/dist/utils/createInitialValues.js +3 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/resolveSchema.d.ts +1 -1
- package/dist/utils/resolveSchema.js +88 -39
- package/dist/utils/solveReferences.d.ts +2 -0
- package/dist/utils/solveReferences.js +8 -0
- package/package.json +1 -1
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { ageFieldDefinition } from "./ageFieldDefinition";
|
|
2
|
+
export const refDefSchema = {
|
|
3
|
+
title: "References and Definitions",
|
|
4
|
+
type: "object",
|
|
5
|
+
required: ["name", "field"],
|
|
6
|
+
properties: {
|
|
7
|
+
name: {
|
|
8
|
+
$ref: "#/definitions/name",
|
|
9
|
+
},
|
|
10
|
+
age: {
|
|
11
|
+
$ref: "#/definitions/age",
|
|
12
|
+
},
|
|
13
|
+
creditCard: {
|
|
14
|
+
type: "number",
|
|
15
|
+
title: "Credit card number",
|
|
16
|
+
description: "Giving input to this field will trigger its dependent field 'billing_address'",
|
|
17
|
+
},
|
|
18
|
+
fieldType: {
|
|
19
|
+
type: "string",
|
|
20
|
+
description: "Selecting an option in this field will render the correct dependent field from the provided oneOf-array",
|
|
21
|
+
enum: ["no type", "string", "object", "array", "object and string"],
|
|
22
|
+
},
|
|
23
|
+
boolean: {
|
|
24
|
+
type: "boolean",
|
|
25
|
+
title: "I already had coffee today",
|
|
26
|
+
},
|
|
27
|
+
animal: {
|
|
28
|
+
type: "string",
|
|
29
|
+
oneOf: [
|
|
30
|
+
{ const: "cat", title: "Cat" },
|
|
31
|
+
{ const: "fish", title: "Fish" },
|
|
32
|
+
{ const: "dog", title: "Dog" },
|
|
33
|
+
{ const: "horse", title: "Horse" },
|
|
34
|
+
{ const: "hamster", title: "Hamster" },
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
dependencies: {
|
|
39
|
+
creditCard: {
|
|
40
|
+
properties: {
|
|
41
|
+
billing_address: {
|
|
42
|
+
type: "object",
|
|
43
|
+
description: "This object depends on creditCard field input",
|
|
44
|
+
properties: {
|
|
45
|
+
street: { type: "string", title: "Street" },
|
|
46
|
+
city: { type: "string", title: "City" },
|
|
47
|
+
postalCode: { type: "number", title: "Postal code" },
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
required: ["billing_address"],
|
|
52
|
+
},
|
|
53
|
+
fieldType: {
|
|
54
|
+
oneOf: [
|
|
55
|
+
{
|
|
56
|
+
properties: {
|
|
57
|
+
fieldType: {
|
|
58
|
+
enum: ["no type"],
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
properties: {
|
|
64
|
+
fieldType: {
|
|
65
|
+
enum: ["string"],
|
|
66
|
+
},
|
|
67
|
+
stringField: {
|
|
68
|
+
type: "string",
|
|
69
|
+
title: "StringField",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
properties: {
|
|
75
|
+
fieldType: {
|
|
76
|
+
enum: ["object"],
|
|
77
|
+
},
|
|
78
|
+
objectField: {
|
|
79
|
+
type: "object",
|
|
80
|
+
properties: {
|
|
81
|
+
objectString: {
|
|
82
|
+
type: "string",
|
|
83
|
+
title: "StringField in Object",
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
properties: {
|
|
91
|
+
fieldType: {
|
|
92
|
+
enum: ["array"],
|
|
93
|
+
},
|
|
94
|
+
arrayField: {
|
|
95
|
+
type: "array",
|
|
96
|
+
additionalItems: { type: "string" },
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
properties: {
|
|
102
|
+
fieldType: {
|
|
103
|
+
enum: ["object and string"],
|
|
104
|
+
},
|
|
105
|
+
stringField: {
|
|
106
|
+
type: "string",
|
|
107
|
+
title: "StringField",
|
|
108
|
+
},
|
|
109
|
+
objectField: {
|
|
110
|
+
type: "object",
|
|
111
|
+
properties: {
|
|
112
|
+
objectString: {
|
|
113
|
+
type: "string",
|
|
114
|
+
title: "StringField in Object",
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
boolean: {
|
|
123
|
+
oneOf: [
|
|
124
|
+
{
|
|
125
|
+
properties: { boolean: false },
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
properties: {
|
|
129
|
+
boolean: true,
|
|
130
|
+
something: { type: "string" },
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
allOf: [
|
|
137
|
+
{
|
|
138
|
+
if: {
|
|
139
|
+
properties: {
|
|
140
|
+
animal: {
|
|
141
|
+
const: "cat",
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
then: {
|
|
146
|
+
properties: {
|
|
147
|
+
food: {
|
|
148
|
+
type: "string",
|
|
149
|
+
enum: ["meat", "grass", "fish"],
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
required: ["food"],
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
if: {
|
|
157
|
+
properties: {
|
|
158
|
+
animal: {
|
|
159
|
+
const: "fish",
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
then: {
|
|
164
|
+
properties: {
|
|
165
|
+
food: {
|
|
166
|
+
type: "string",
|
|
167
|
+
enum: ["insect", "worms"],
|
|
168
|
+
},
|
|
169
|
+
water: {
|
|
170
|
+
type: "string",
|
|
171
|
+
enum: ["lake", "sea"],
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
required: ["food", "water"],
|
|
175
|
+
},
|
|
176
|
+
// else: {
|
|
177
|
+
// properties: {
|
|
178
|
+
// likesWalkiesWhere: {
|
|
179
|
+
// type: "string",
|
|
180
|
+
// enum: ["park", "forest", "garden"],
|
|
181
|
+
// },
|
|
182
|
+
// },
|
|
183
|
+
// },
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
required: ["animal"],
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
definitions: {
|
|
190
|
+
name: {
|
|
191
|
+
type: "string",
|
|
192
|
+
title: "Name",
|
|
193
|
+
description: "The schema for this field comes from the 'definitions' object inside the main schema",
|
|
194
|
+
},
|
|
195
|
+
age: ageFieldDefinition,
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
export const refDefUiSchema = {
|
|
199
|
+
"ui:order": [
|
|
200
|
+
"name",
|
|
201
|
+
"age",
|
|
202
|
+
"creditCard",
|
|
203
|
+
"billing_address",
|
|
204
|
+
"fieldType",
|
|
205
|
+
"stringField",
|
|
206
|
+
"objectField",
|
|
207
|
+
"arrayField",
|
|
208
|
+
"boolean",
|
|
209
|
+
"something",
|
|
210
|
+
"animal",
|
|
211
|
+
"food",
|
|
212
|
+
"water",
|
|
213
|
+
],
|
|
214
|
+
fieldType: {
|
|
215
|
+
"ui:placeholderOptionOptions": {
|
|
216
|
+
props: { disabled: false, hidden: false },
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
};
|
package/dist/styledComponents.js
CHANGED
|
@@ -295,8 +295,6 @@ const MultipleChoiceFieldElement = styled(RadioButtonFieldElement) ``;
|
|
|
295
295
|
const MultipleChoiceFieldInput = styled(RadioButtonFieldInput) ``;
|
|
296
296
|
const MultipleChoiceFieldLabel = styled(ButtonLabel) ``;
|
|
297
297
|
export const MultipleChoiceField = ({ inputProps, name, className, enums, style, }) => {
|
|
298
|
-
console.log("style", style);
|
|
299
|
-
console.log("function", getStyle(style !== null && style !== void 0 ? style : ""));
|
|
300
298
|
return (_jsx(MultipleChoiceFieldContainer, { className: className, style: getStyle(style !== null && style !== void 0 ? style : ""), children: enums.values.map((v) => (_jsxs(MultipleChoiceFieldElement, { children: [_jsx(MultipleChoiceFieldInput, Object.assign({}, inputProps, { name: name, type: enums.type === "multiSelect" ? "checkbox" : "radio", value: typeof v === "string" || typeof v === "number" ? v : v.const })), _jsx(MultipleChoiceFieldLabel, { children: typeof v === "string" || typeof v === "number" ? v : v.title })] }, typeof v === "string" || typeof v === "number" ? v : v.const))) }));
|
|
301
299
|
};
|
|
302
300
|
RadioButtonField.styles = {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { JSONSchema7 } from "json-schema";
|
|
1
|
+
import type { JSONSchema7, JSONSchema7Definition } from "json-schema";
|
|
2
2
|
import React from "react";
|
|
3
3
|
export type FormProps<T> = React.PropsWithChildren<{
|
|
4
4
|
/** If true, disabled prop is passed down to each field on the form */
|
|
@@ -48,7 +48,9 @@ export type FormRegistryContextValue = {
|
|
|
48
48
|
fields: Record<string, Field>;
|
|
49
49
|
templates: TemplatesType;
|
|
50
50
|
components: ComponentsType;
|
|
51
|
-
definitions:
|
|
51
|
+
definitions: {
|
|
52
|
+
[key: string]: JSONSchema7Definition;
|
|
53
|
+
} | undefined;
|
|
52
54
|
formContext: Record<string, any>;
|
|
53
55
|
useItemsAsAdditionalItems: boolean;
|
|
54
56
|
hideUnmarkedAdditionalProperties: boolean;
|
|
@@ -7,12 +7,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
+
import { ageFieldDefinition } from "../../sampleData/ageFieldDefinition";
|
|
10
11
|
import { createInitialValues } from "../createInitialValues";
|
|
11
12
|
const exampleSchema = {
|
|
12
13
|
title: "TITLE",
|
|
13
14
|
required: ["lastName", "requiredNestedObject", "requiredArray"],
|
|
14
15
|
properties: {
|
|
15
16
|
name: { type: "string", title: "Full name" },
|
|
17
|
+
age: { $ref: "#/definitions/age" },
|
|
16
18
|
number: { type: "number", title: "Phone number" },
|
|
17
19
|
feeling: { type: "boolean", title: "Feeling happy today?" },
|
|
18
20
|
arrayOfObject: {
|
|
@@ -53,12 +55,16 @@ const exampleSchema = {
|
|
|
53
55
|
},
|
|
54
56
|
},
|
|
55
57
|
},
|
|
58
|
+
definitions: {
|
|
59
|
+
age: ageFieldDefinition,
|
|
60
|
+
},
|
|
56
61
|
};
|
|
57
62
|
describe("Create Initial Values", () => {
|
|
58
63
|
it("creates for an object type", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
64
|
const result = createInitialValues(exampleSchema);
|
|
60
65
|
const expected = {
|
|
61
66
|
name: "",
|
|
67
|
+
age: 0,
|
|
62
68
|
number: 0,
|
|
63
69
|
feeling: false,
|
|
64
70
|
arrayOfObject: [],
|
|
@@ -1,35 +1,6 @@
|
|
|
1
1
|
import { resolveSchema } from "../resolveSchema";
|
|
2
|
+
import { schemaWithDependenciesAndOneOf, simpleConditionalSchema, } from "./testSchemas";
|
|
2
3
|
describe("resolveSchema", () => {
|
|
3
|
-
const simpleConditionalSchema = {
|
|
4
|
-
type: "object",
|
|
5
|
-
title: "Title",
|
|
6
|
-
properties: {
|
|
7
|
-
animal: {
|
|
8
|
-
type: "string",
|
|
9
|
-
enum: ["cat", "bird"],
|
|
10
|
-
},
|
|
11
|
-
},
|
|
12
|
-
allOf: [
|
|
13
|
-
{
|
|
14
|
-
if: {
|
|
15
|
-
properties: {
|
|
16
|
-
animal: {
|
|
17
|
-
const: "cat",
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
then: {
|
|
22
|
-
properties: {
|
|
23
|
-
food: {
|
|
24
|
-
type: "string",
|
|
25
|
-
enum: ["meat", "grass", "fish"],
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
required: ["food"],
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
],
|
|
32
|
-
};
|
|
33
4
|
test("adds conditional properties when condition is met", () => {
|
|
34
5
|
const value = { animal: "cat" };
|
|
35
6
|
const resolvedSchema = resolveSchema(simpleConditionalSchema, value);
|
|
@@ -103,4 +74,43 @@ describe("resolveSchema", () => {
|
|
|
103
74
|
["food"],
|
|
104
75
|
]);
|
|
105
76
|
});
|
|
77
|
+
test("selects the correct oneOf schema based on the type dependency", () => {
|
|
78
|
+
const valueCar = { type: "car" };
|
|
79
|
+
const resolvedCarSchema = resolveSchema(schemaWithDependenciesAndOneOf, valueCar);
|
|
80
|
+
expect(resolvedCarSchema).toEqual([
|
|
81
|
+
{
|
|
82
|
+
type: "object",
|
|
83
|
+
properties: {
|
|
84
|
+
type: { type: "string", enum: ["car", "bike"] },
|
|
85
|
+
wheels: { type: "number", enum: [4] },
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
[],
|
|
89
|
+
]);
|
|
90
|
+
const valueBike = { type: "bike" };
|
|
91
|
+
const resolvedBikeSchema = resolveSchema(schemaWithDependenciesAndOneOf, valueBike);
|
|
92
|
+
expect(resolvedBikeSchema).toEqual([
|
|
93
|
+
{
|
|
94
|
+
type: "object",
|
|
95
|
+
properties: {
|
|
96
|
+
type: { type: "string", enum: ["car", "bike"] },
|
|
97
|
+
wheels: { type: "number", enum: [2] },
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
[],
|
|
101
|
+
]);
|
|
102
|
+
});
|
|
103
|
+
test("handles missing dependencies", () => {
|
|
104
|
+
const valueEmpty = {};
|
|
105
|
+
const resolvedEmptySchema = resolveSchema(schemaWithDependenciesAndOneOf, valueEmpty);
|
|
106
|
+
expect(resolvedEmptySchema).toEqual([
|
|
107
|
+
{
|
|
108
|
+
type: "object",
|
|
109
|
+
properties: {
|
|
110
|
+
type: { type: "string", enum: ["car", "bike"] },
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
[],
|
|
114
|
+
]);
|
|
115
|
+
});
|
|
106
116
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { solveReferences } from "../solveReferences";
|
|
2
|
+
import { refDefSchema } from "./testSchemas";
|
|
3
|
+
describe("solveReferences", () => {
|
|
4
|
+
it("should resolve simple references correctly", () => {
|
|
5
|
+
var _a;
|
|
6
|
+
const schemaSolvedForReferences = solveReferences((_a = refDefSchema === null || refDefSchema === void 0 ? void 0 : refDefSchema.properties) === null || _a === void 0 ? void 0 : _a.name, refDefSchema.definitions);
|
|
7
|
+
expect(schemaSolvedForReferences).toEqual({
|
|
8
|
+
type: "string",
|
|
9
|
+
title: "Name",
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export const refDefSchema = {
|
|
2
|
+
title: "References and Definitions",
|
|
3
|
+
type: "object",
|
|
4
|
+
required: ["string"],
|
|
5
|
+
properties: {
|
|
6
|
+
name: {
|
|
7
|
+
$ref: "#/definitions/name",
|
|
8
|
+
},
|
|
9
|
+
},
|
|
10
|
+
definitions: {
|
|
11
|
+
name: { type: "string", title: "Name" },
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
export const simpleConditionalSchema = {
|
|
15
|
+
type: "object",
|
|
16
|
+
title: "Title",
|
|
17
|
+
properties: {
|
|
18
|
+
animal: {
|
|
19
|
+
type: "string",
|
|
20
|
+
enum: ["cat", "bird"],
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
allOf: [
|
|
24
|
+
{
|
|
25
|
+
if: {
|
|
26
|
+
properties: {
|
|
27
|
+
animal: {
|
|
28
|
+
const: "cat",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
then: {
|
|
33
|
+
properties: {
|
|
34
|
+
food: {
|
|
35
|
+
type: "string",
|
|
36
|
+
enum: ["meat", "grass", "fish"],
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
required: ["food"],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
export const schemaWithDependenciesAndOneOf = {
|
|
45
|
+
type: "object",
|
|
46
|
+
properties: {
|
|
47
|
+
type: { type: "string", enum: ["car", "bike"] },
|
|
48
|
+
},
|
|
49
|
+
dependencies: {
|
|
50
|
+
type: {
|
|
51
|
+
oneOf: [
|
|
52
|
+
{
|
|
53
|
+
properties: {
|
|
54
|
+
type: { enum: ["car"] },
|
|
55
|
+
wheels: { type: "number", enum: [4] },
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
properties: {
|
|
60
|
+
type: { enum: ["bike"] },
|
|
61
|
+
wheels: { type: "number", enum: [2] },
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { solveReferences } from "./solveReferences";
|
|
1
2
|
export function createInitialValues(schema) {
|
|
2
3
|
if (!schema) {
|
|
3
4
|
return {};
|
|
@@ -6,7 +7,8 @@ export function createInitialValues(schema) {
|
|
|
6
7
|
return Object.keys(schema === null || schema === void 0 ? void 0 : schema.properties).reduce((acc, key) => {
|
|
7
8
|
var _a;
|
|
8
9
|
const value = (_a = schema === null || schema === void 0 ? void 0 : schema.properties) === null || _a === void 0 ? void 0 : _a[key];
|
|
9
|
-
|
|
10
|
+
const solvedValue = solveReferences(value, schema.definitions);
|
|
11
|
+
return Object.assign(Object.assign({}, acc), { [key]: createInitialValues(solvedValue) });
|
|
10
12
|
}, {});
|
|
11
13
|
}
|
|
12
14
|
const type = schema.type;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -4,5 +4,6 @@ export * from "./getTitleAndDescription";
|
|
|
4
4
|
export * from "./getTypes";
|
|
5
5
|
export * from "./mergeSchemas";
|
|
6
6
|
export * from "./resolveSchema";
|
|
7
|
+
export * from "./solveReferences";
|
|
7
8
|
export * from "./createInitialValues";
|
|
8
9
|
export declare const warnFromUnsupportedKeys: (componentName: string, data: Record<string, any>, supportedKeys: string[]) => void;
|
package/dist/utils/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export * from "./getTitleAndDescription";
|
|
|
4
4
|
export * from "./getTypes";
|
|
5
5
|
export * from "./mergeSchemas";
|
|
6
6
|
export * from "./resolveSchema";
|
|
7
|
+
export * from "./solveReferences";
|
|
7
8
|
export * from "./createInitialValues";
|
|
8
9
|
//TODO: REMOVE WHEN ALL SUPPORTED
|
|
9
10
|
export const warnFromUnsupportedKeys = (componentName, data, supportedKeys) => {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Schema } from "../types";
|
|
2
|
-
export declare const resolveSchema: ({ allOf, ...schema }: Schema, value: Record<string, any>) => [Schema, string[]];
|
|
2
|
+
export declare const resolveSchema: ({ allOf, dependencies, ...schema }: Schema, value: Record<string, any>) => [Schema, string[]];
|
|
@@ -19,50 +19,99 @@ const resolveEquality = (value, expression) => {
|
|
|
19
19
|
};
|
|
20
20
|
return deepOverlap(expression, newObject);
|
|
21
21
|
};
|
|
22
|
+
function resolveOneOfValue(value) {
|
|
23
|
+
var _a;
|
|
24
|
+
if (typeof value !== "boolean" && ((_a = value.enum) === null || _a === void 0 ? void 0 : _a[0]) !== undefined) {
|
|
25
|
+
return value.enum[0];
|
|
26
|
+
}
|
|
27
|
+
else if (value === true || value === false) {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
else
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const resolveSelectedOneOfValue = (key, value, oneOf) => {
|
|
34
|
+
const update = oneOf.findIndex((one) => resolveOneOfValue(one.properties[key]) === value[key]);
|
|
35
|
+
const manipulatedUpdateValue = Object.keys(oneOf[update].properties)
|
|
36
|
+
.filter((objKey) => objKey !== key)
|
|
37
|
+
.reduce((newObj, innerKey) => {
|
|
38
|
+
newObj[innerKey] = oneOf[update].properties[innerKey];
|
|
39
|
+
return newObj;
|
|
40
|
+
}, {});
|
|
41
|
+
return { properties: manipulatedUpdateValue };
|
|
42
|
+
};
|
|
22
43
|
export const resolveSchema = (_a, value) => {
|
|
23
|
-
var _b;
|
|
24
|
-
var { allOf } = _a, schema = __rest(_a, ["allOf"]);
|
|
25
|
-
|
|
26
|
-
|
|
44
|
+
var _b, _c;
|
|
45
|
+
var { allOf, dependencies } = _a, schema = __rest(_a, ["allOf", "dependencies"]);
|
|
46
|
+
let schemaCopy = schema;
|
|
47
|
+
let valuesToRemoveDeps = [];
|
|
48
|
+
let valuesToRemoveAllOf = [];
|
|
49
|
+
if (dependencies) {
|
|
50
|
+
schemaCopy = Object.entries(dependencies).reduce((schema, [key, dependency]) => {
|
|
51
|
+
if (value[key] !== undefined && value[key] !== "") {
|
|
52
|
+
const oneOf = dependency.oneOf;
|
|
53
|
+
if (Array.isArray(oneOf) && oneOf !== undefined) {
|
|
54
|
+
const selectedOneOf = resolveSelectedOneOfValue(key, value, oneOf);
|
|
55
|
+
return mergeSchemas(schema, selectedOneOf);
|
|
56
|
+
}
|
|
57
|
+
return mergeSchemas(schema, dependency);
|
|
58
|
+
}
|
|
59
|
+
return schema;
|
|
60
|
+
}, schemaCopy);
|
|
27
61
|
}
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
if (!
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
const { if: expression, then, else: otherwise } = current, resolvedSchemaLessConditional = __rest(current, ["if", "then", "else"]);
|
|
34
|
-
if (!expression || typeof expression === "boolean") {
|
|
35
|
-
return {
|
|
36
|
-
resolvedSchema: mergeSchemas(resolvedSchema, resolvedSchemaLessConditional),
|
|
37
|
-
notAddedProperties,
|
|
38
|
-
};
|
|
62
|
+
const usedProperties = Object.keys((_b = schemaCopy.properties) !== null && _b !== void 0 ? _b : {});
|
|
63
|
+
Object.keys(value).map((key) => {
|
|
64
|
+
if (!usedProperties.includes(key)) {
|
|
65
|
+
valuesToRemoveDeps.push(key);
|
|
39
66
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
67
|
+
});
|
|
68
|
+
if (!allOf && dependencies) {
|
|
69
|
+
return [schemaCopy, valuesToRemoveDeps];
|
|
70
|
+
}
|
|
71
|
+
if (allOf) {
|
|
72
|
+
const { resolvedSchema, notAddedProperties } = allOf.reduce(({ resolvedSchema, notAddedProperties }, current) => {
|
|
73
|
+
var _a, _b;
|
|
74
|
+
if (!current || typeof current === "boolean") {
|
|
75
|
+
return { resolvedSchema, notAddedProperties };
|
|
43
76
|
}
|
|
44
|
-
if
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
77
|
+
const { if: expression, then, else: otherwise } = current, resolvedSchemaLessConditional = __rest(current, ["if", "then", "else"]);
|
|
78
|
+
if (!expression || typeof expression === "boolean") {
|
|
79
|
+
return {
|
|
80
|
+
resolvedSchema: mergeSchemas(resolvedSchema, resolvedSchemaLessConditional),
|
|
81
|
+
notAddedProperties,
|
|
82
|
+
};
|
|
49
83
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
84
|
+
if (resolveEquality(value, expression)) {
|
|
85
|
+
if (then && typeof then !== "boolean") {
|
|
86
|
+
resolvedSchema = mergeSchemas(resolvedSchema, then);
|
|
87
|
+
}
|
|
88
|
+
if (otherwise && typeof otherwise !== "boolean") {
|
|
89
|
+
notAddedProperties = [
|
|
90
|
+
...notAddedProperties,
|
|
91
|
+
...Object.keys((_a = otherwise.properties) !== null && _a !== void 0 ? _a : {}),
|
|
92
|
+
];
|
|
93
|
+
}
|
|
57
94
|
}
|
|
58
|
-
|
|
59
|
-
|
|
95
|
+
else {
|
|
96
|
+
if (then && typeof then !== "boolean") {
|
|
97
|
+
notAddedProperties = [
|
|
98
|
+
...notAddedProperties,
|
|
99
|
+
...Object.keys((_b = then.properties) !== null && _b !== void 0 ? _b : {}),
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
if (otherwise && typeof otherwise !== "boolean") {
|
|
103
|
+
resolvedSchema = mergeSchemas(resolvedSchema, otherwise);
|
|
104
|
+
}
|
|
60
105
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
106
|
+
return { resolvedSchema, notAddedProperties };
|
|
107
|
+
}, { resolvedSchema: schemaCopy, notAddedProperties: [] });
|
|
108
|
+
//Values that do not exists due to conditions
|
|
109
|
+
const usedPropertiesAllOf = Object.keys((_c = resolvedSchema.properties) !== null && _c !== void 0 ? _c : {});
|
|
110
|
+
valuesToRemoveAllOf = [...new Set([...notAddedProperties])].filter((key) => !usedPropertiesAllOf.includes(key) && typeof value[key] !== "undefined");
|
|
111
|
+
const valuesToRemove = [
|
|
112
|
+
...new Set([...valuesToRemoveDeps, ...valuesToRemoveAllOf]),
|
|
113
|
+
];
|
|
114
|
+
return [resolvedSchema, valuesToRemove];
|
|
115
|
+
}
|
|
116
|
+
return [schemaCopy, []];
|
|
68
117
|
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function solveReferences(schema, definitions) {
|
|
2
|
+
var _a, _b;
|
|
3
|
+
if (typeof schema === "object" && schema["$ref"]) {
|
|
4
|
+
const schemaKey = (_b = (_a = schema["$ref"]) === null || _a === void 0 ? void 0 : _a.substring(14)) !== null && _b !== void 0 ? _b : "";
|
|
5
|
+
return definitions[schemaKey];
|
|
6
|
+
}
|
|
7
|
+
return schema;
|
|
8
|
+
}
|