@nolwenture/formik-jsonschema-form 0.7.0 → 0.8.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/README.md +52 -1
- package/dist/Form.stories.d.ts +1 -0
- package/dist/Form.stories.js +8 -0
- package/dist/Form.test.d.ts +1 -1
- package/dist/Form.test.js +3 -1
- package/dist/fields/ArrayField.js +1 -1
- package/dist/fields/MultipleChoiceField.d.ts +3 -0
- package/dist/fields/MultipleChoiceField.js +15 -0
- package/dist/fields/ObjectField.js +3 -1
- package/dist/fields/StringField.js +1 -1
- package/dist/fields/index.d.ts +1 -0
- package/dist/fields/index.js +1 -0
- package/dist/fields/stories/AnyField.stories.d.ts +5 -0
- package/dist/fields/stories/AnyField.stories.js +79 -0
- package/dist/fields/stories/ArrayField.stories.d.ts +7 -0
- package/dist/fields/stories/ArrayField.stories.js +191 -0
- package/dist/fields/stories/BooleanField.stories.d.ts +5 -0
- package/dist/fields/stories/BooleanField.stories.js +36 -0
- package/dist/fields/stories/DateField.stories.d.ts +5 -0
- package/dist/fields/stories/DateField.stories.js +34 -0
- package/dist/fields/stories/NullField.stories.d.ts +5 -0
- package/dist/fields/stories/NullField.stories.js +38 -0
- package/dist/fields/stories/NumberField.stories.d.ts +5 -0
- package/dist/fields/stories/NumberField.stories.js +33 -0
- package/dist/fields/stories/ObjectField.stories.d.ts +5 -0
- package/dist/fields/stories/ObjectField.stories.js +76 -0
- package/dist/fields/stories/RadioField.stories.d.ts +5 -0
- package/dist/fields/stories/RadioField.stories.js +42 -0
- package/dist/fields/stories/SchemaField.stories.d.ts +1673 -0
- package/dist/fields/stories/SchemaField.stories.js +39 -0
- package/dist/fields/stories/SearchMultiTagSelectField.stories.d.ts +5 -0
- package/dist/fields/stories/SearchMultiTagSelectField.stories.js +58 -0
- package/dist/fields/stories/StringField.stories.d.ts +7 -0
- package/dist/fields/stories/StringField.stories.js +75 -0
- package/dist/fields/stories/ToggleField.stories.d.ts +5 -0
- package/dist/fields/stories/ToggleField.stories.js +34 -0
- package/dist/hooks/__tests__/useExtractUiSchema.test.d.ts +1 -0
- package/dist/hooks/__tests__/useExtractUiSchema.test.js +45 -0
- package/dist/hooks/__tests__/useSetDefault.test.d.ts +1 -0
- package/dist/hooks/__tests__/useSetDefault.test.js +65 -0
- package/dist/hooks/useExtractUiSchema.js +7 -3
- package/dist/sampleData/multipleChoiceSchema.d.ts +3 -0
- package/dist/sampleData/multipleChoiceSchema.js +112 -0
- package/dist/sampleData/requiredInputSchema.js +0 -5
- package/dist/styledComponents.d.ts +2 -1
- package/dist/styledComponents.js +15 -9
- package/dist/types.d.ts +15 -0
- package/dist/utils/__tests/createInitialValues.test.d.ts +1 -0
- package/dist/utils/__tests/createInitialValues.test.js +72 -0
- package/dist/utils/__tests/deepOverlap.test.d.ts +1 -0
- package/dist/utils/__tests/deepOverlap.test.js +41 -0
- package/dist/utils/__tests/getEnums.test.js +11 -16
- package/dist/utils/__tests/getTitleAndDescription.test.d.ts +1 -0
- package/dist/utils/__tests/getTitleAndDescription.test.js +53 -0
- package/dist/utils/__tests/getTypes.test.d.ts +1 -0
- package/dist/utils/__tests/getTypes.test.js +62 -0
- package/dist/utils/__tests/mergeSchemas.test.d.ts +1 -0
- package/dist/utils/__tests/mergeSchemas.test.js +40 -0
- package/dist/utils/__tests/resolveSchema.test.js +28 -28
- package/dist/utils/createInitialValues.d.ts +4 -0
- package/dist/utils/createInitialValues.js +27 -0
- package/dist/utils/getStyle.d.ts +5 -0
- package/dist/utils/getStyle.js +18 -0
- package/dist/utils/getTypes.js +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/package.json +5 -2
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { createInitialValues } from "../createInitialValues";
|
|
11
|
+
const exampleSchema = {
|
|
12
|
+
title: "TITLE",
|
|
13
|
+
required: ["lastName", "requiredNestedObject", "requiredArray"],
|
|
14
|
+
properties: {
|
|
15
|
+
name: { type: "string", title: "Full name" },
|
|
16
|
+
number: { type: "number", title: "Phone number" },
|
|
17
|
+
feeling: { type: "boolean", title: "Feeling happy today?" },
|
|
18
|
+
arrayOfObject: {
|
|
19
|
+
type: "array",
|
|
20
|
+
title: "Array of objects",
|
|
21
|
+
items: {
|
|
22
|
+
type: "object",
|
|
23
|
+
title: "Favourites",
|
|
24
|
+
required: ["favouriteFood"],
|
|
25
|
+
properties: {
|
|
26
|
+
favouriteFood: { type: "string", title: "Favourite food" },
|
|
27
|
+
favouriteSong: { type: "string", title: "Favourite song" },
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
additionalItems: {
|
|
31
|
+
type: "object",
|
|
32
|
+
title: "Second favourites",
|
|
33
|
+
required: ["secondFavouriteSong"],
|
|
34
|
+
properties: {
|
|
35
|
+
anotherFavouriteFood: {
|
|
36
|
+
type: "string",
|
|
37
|
+
title: "Another favourite food",
|
|
38
|
+
},
|
|
39
|
+
anotherFavouriteSong: {
|
|
40
|
+
type: "string",
|
|
41
|
+
title: "Another favourite song",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
nestedObject: {
|
|
47
|
+
type: "object",
|
|
48
|
+
title: "Nested Object",
|
|
49
|
+
required: ["tasks"],
|
|
50
|
+
properties: {
|
|
51
|
+
hobbies: { type: "string", title: "Hobbies" },
|
|
52
|
+
tasks: { type: "string", title: "Tasks" },
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
describe("Create Initial Values", () => {
|
|
58
|
+
it("creates for an object type", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
const result = createInitialValues(exampleSchema);
|
|
60
|
+
const expected = {
|
|
61
|
+
name: "",
|
|
62
|
+
number: 0,
|
|
63
|
+
feeling: false,
|
|
64
|
+
arrayOfObject: [],
|
|
65
|
+
nestedObject: {
|
|
66
|
+
hobbies: "",
|
|
67
|
+
tasks: "",
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
expect(result).toEqual(expected);
|
|
71
|
+
}));
|
|
72
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { deepOverlap } from "../deepOverlap";
|
|
2
|
+
describe("deepOverlap", () => {
|
|
3
|
+
test("returns true for both empty objects", () => {
|
|
4
|
+
expect(deepOverlap({}, {})).toBe(true);
|
|
5
|
+
});
|
|
6
|
+
test("returns true for identical objects", () => {
|
|
7
|
+
const obj1 = { a: 1, b: 2 };
|
|
8
|
+
const obj2 = { a: 1, b: 2 };
|
|
9
|
+
expect(deepOverlap(obj1, obj2)).toBe(true);
|
|
10
|
+
});
|
|
11
|
+
test("returns true for nested objects that are identical", () => {
|
|
12
|
+
const obj1 = { a: { b: 2 } };
|
|
13
|
+
const obj2 = { a: { b: 2 } };
|
|
14
|
+
expect(deepOverlap(obj1, obj2)).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
test("handles arrays and non-object types correctly", () => {
|
|
17
|
+
const obj1 = { a: [1, 2, 3], b: "string", c: true };
|
|
18
|
+
const obj2 = { a: [1, 2, 3], b: "string", c: true };
|
|
19
|
+
expect(deepOverlap(obj1, obj2)).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
test("returns true for objects with keys in different orders", () => {
|
|
22
|
+
const obj1 = { a: 1, b: { c: 2 } };
|
|
23
|
+
const obj2 = { b: { c: 2 }, a: 1 };
|
|
24
|
+
expect(deepOverlap(obj1, obj2)).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
test("returns false for objects with different values", () => {
|
|
27
|
+
const obj1 = { a: 1 };
|
|
28
|
+
const obj2 = { a: 2 };
|
|
29
|
+
expect(deepOverlap(obj1, obj2)).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
test("returns false for nested objects that do not overlap", () => {
|
|
32
|
+
const obj1 = { a: { b: 1 } };
|
|
33
|
+
const obj2 = { a: { b: 2 } };
|
|
34
|
+
expect(deepOverlap(obj1, obj2)).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
test("returns false when one object is empty", () => {
|
|
37
|
+
const obj1 = { a: 1 };
|
|
38
|
+
const obj2 = {};
|
|
39
|
+
expect(deepOverlap(obj1, obj2)).toBe(false);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { getEnums } from "../getEnums";
|
|
2
2
|
describe("getEnums", () => {
|
|
3
|
+
test("returns undefined when no enum types are defined", () => {
|
|
4
|
+
const result = getEnums({ schema: {} });
|
|
5
|
+
expect(result).toBeUndefined();
|
|
6
|
+
});
|
|
7
|
+
test("throws error when more than one enum type is defined", () => {
|
|
8
|
+
expect(() => getEnums({ schema: { enum: [], oneOf: [], anyOf: [] } })).toThrow('Only one of "enum", "oneOf" or "anyOf" can be defined!');
|
|
9
|
+
});
|
|
3
10
|
test("enum works", () => {
|
|
4
11
|
const result = getEnums({ schema: { enum: ["one", "two", "three"] } });
|
|
5
12
|
expect(result).toEqual({
|
|
@@ -15,10 +22,7 @@ describe("getEnums", () => {
|
|
|
15
22
|
const result = getEnums({
|
|
16
23
|
schema: {
|
|
17
24
|
oneOf: [
|
|
18
|
-
{
|
|
19
|
-
const: "horse",
|
|
20
|
-
title: "Horse",
|
|
21
|
-
},
|
|
25
|
+
{ const: "horse", title: "Horse" },
|
|
22
26
|
{ const: "frog", title: "Frog" },
|
|
23
27
|
{ const: "bird", title: "Bird" },
|
|
24
28
|
],
|
|
@@ -27,10 +31,7 @@ describe("getEnums", () => {
|
|
|
27
31
|
expect(result).toEqual({
|
|
28
32
|
type: "select",
|
|
29
33
|
values: [
|
|
30
|
-
{
|
|
31
|
-
const: "horse",
|
|
32
|
-
title: "Horse",
|
|
33
|
-
},
|
|
34
|
+
{ const: "horse", title: "Horse" },
|
|
34
35
|
{ const: "frog", title: "Frog" },
|
|
35
36
|
{ const: "bird", title: "Bird" },
|
|
36
37
|
],
|
|
@@ -40,10 +41,7 @@ describe("getEnums", () => {
|
|
|
40
41
|
const result = getEnums({
|
|
41
42
|
schema: {
|
|
42
43
|
anyOf: [
|
|
43
|
-
{
|
|
44
|
-
const: "tangerine",
|
|
45
|
-
title: "Tangerine",
|
|
46
|
-
},
|
|
44
|
+
{ const: "tangerine", title: "Tangerine" },
|
|
47
45
|
{ const: "mandarin", title: "Mandarin" },
|
|
48
46
|
{ const: "bruno", title: "Bruno" },
|
|
49
47
|
],
|
|
@@ -52,10 +50,7 @@ describe("getEnums", () => {
|
|
|
52
50
|
expect(result).toEqual({
|
|
53
51
|
type: "multiSelect",
|
|
54
52
|
values: [
|
|
55
|
-
{
|
|
56
|
-
const: "tangerine",
|
|
57
|
-
title: "Tangerine",
|
|
58
|
-
},
|
|
53
|
+
{ const: "tangerine", title: "Tangerine" },
|
|
59
54
|
{ const: "mandarin", title: "Mandarin" },
|
|
60
55
|
{ const: "bruno", title: "Bruno" },
|
|
61
56
|
],
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { getTitle, getDescription } from "../getTitleAndDescription";
|
|
2
|
+
describe("getTitleAndDescription", () => {
|
|
3
|
+
describe("getTitle", () => {
|
|
4
|
+
test("returns title when provided", () => {
|
|
5
|
+
const title = "Test Title";
|
|
6
|
+
expect(getTitle({ title })).toBe(title);
|
|
7
|
+
});
|
|
8
|
+
test("returns title from uiSchema options when provided", () => {
|
|
9
|
+
const uiSchema = {
|
|
10
|
+
"ui:options": { title: "UI Schema Title" },
|
|
11
|
+
};
|
|
12
|
+
expect(getTitle({ uiSchema })).toBe("UI Schema Title");
|
|
13
|
+
});
|
|
14
|
+
test("returns title from uiSchema title when provided", () => {
|
|
15
|
+
const uiSchema = { "ui:title": "UI Schema Title" };
|
|
16
|
+
expect(getTitle({ uiSchema })).toBe("UI Schema Title");
|
|
17
|
+
});
|
|
18
|
+
test("returns title from schema when provided", () => {
|
|
19
|
+
const schema = { title: "Schema Title" };
|
|
20
|
+
expect(getTitle({ schema })).toBe("Schema Title");
|
|
21
|
+
});
|
|
22
|
+
test("returns last part of name when provided", () => {
|
|
23
|
+
const name = "nested.property";
|
|
24
|
+
expect(getTitle({ name })).toBe("property");
|
|
25
|
+
});
|
|
26
|
+
test("returns undefined when no title is provided", () => {
|
|
27
|
+
expect(getTitle({})).toBeUndefined();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
describe("getDescription", () => {
|
|
31
|
+
test("returns description when provided", () => {
|
|
32
|
+
const description = "Test Description";
|
|
33
|
+
expect(getDescription({ description })).toBe(description);
|
|
34
|
+
});
|
|
35
|
+
test("returns description from uiSchema options when provided", () => {
|
|
36
|
+
const uiSchema = {
|
|
37
|
+
"ui:options": { description: "UI Schema Description" },
|
|
38
|
+
};
|
|
39
|
+
expect(getDescription({ uiSchema })).toBe("UI Schema Description");
|
|
40
|
+
});
|
|
41
|
+
test("returns description from uiSchema description when provided", () => {
|
|
42
|
+
const uiSchema = { "ui:description": "UI Schema Description" };
|
|
43
|
+
expect(getDescription({ uiSchema })).toBe("UI Schema Description");
|
|
44
|
+
});
|
|
45
|
+
test("returns description from schema when provided", () => {
|
|
46
|
+
const schema = { description: "Schema Description" };
|
|
47
|
+
expect(getDescription({ schema })).toBe("Schema Description");
|
|
48
|
+
});
|
|
49
|
+
test("returns undefined when no description is provided", () => {
|
|
50
|
+
expect(getDescription({})).toBeUndefined();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { isObject, getDefaultValueForType, guessType, getSchemaType, } from "../getTypes";
|
|
2
|
+
describe("getTypes", () => {
|
|
3
|
+
describe("isObject", () => {
|
|
4
|
+
test("returns true for non-null objects", () => {
|
|
5
|
+
expect(isObject({})).toBe(true);
|
|
6
|
+
expect(isObject([])).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
test("returns false for null", () => {
|
|
9
|
+
expect(isObject(null)).toBe(false);
|
|
10
|
+
});
|
|
11
|
+
test("returns false for non-object types", () => {
|
|
12
|
+
expect(isObject(42)).toBe(false);
|
|
13
|
+
expect(isObject("string")).toBe(false);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
describe("getDefaultValueForType", () => {
|
|
17
|
+
test("returns empty array for 'array' type", () => {
|
|
18
|
+
expect(getDefaultValueForType("array")).toEqual([]);
|
|
19
|
+
});
|
|
20
|
+
test("returns empty object for 'object' type", () => {
|
|
21
|
+
expect(getDefaultValueForType("object")).toEqual({});
|
|
22
|
+
});
|
|
23
|
+
test("returns null for unspecified or complex types", () => {
|
|
24
|
+
expect(getDefaultValueForType("string")).toBeNull();
|
|
25
|
+
expect(getDefaultValueForType(["string", "null"])).toBeNull();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
describe("guessType", () => {
|
|
29
|
+
test("correctly guesses basic types", () => {
|
|
30
|
+
expect(guessType([])).toBe("array");
|
|
31
|
+
expect(guessType("test")).toBe("string");
|
|
32
|
+
expect(guessType(42)).toBe("number");
|
|
33
|
+
expect(guessType(true)).toBe("boolean");
|
|
34
|
+
expect(guessType(null)).toBe("null");
|
|
35
|
+
expect(guessType({})).toBe("object");
|
|
36
|
+
});
|
|
37
|
+
test("defaults to 'string' for unrecognized values", () => {
|
|
38
|
+
expect(guessType(() => { })).toBe("string");
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe("getSchemaType", () => {
|
|
42
|
+
test("returns the explicit type if provided", () => {
|
|
43
|
+
expect(getSchemaType({ type: "number" })).toBe("number");
|
|
44
|
+
});
|
|
45
|
+
test("guesses the type based on const values", () => {
|
|
46
|
+
expect(getSchemaType({ const: 42 })).toBe("number");
|
|
47
|
+
});
|
|
48
|
+
test("returns 'string' for enums", () => {
|
|
49
|
+
expect(getSchemaType({ enum: ["one", "two"] })).toBe("string");
|
|
50
|
+
});
|
|
51
|
+
test("returns 'object' for schemas with properties or additionalProperties", () => {
|
|
52
|
+
expect(getSchemaType({ properties: { a: { type: "number" } } })).toBe("object");
|
|
53
|
+
expect(getSchemaType({ additionalProperties: { type: "number" } })).toBe("object");
|
|
54
|
+
});
|
|
55
|
+
test('handles array types, excluding "null" when paired with another type', () => {
|
|
56
|
+
expect(getSchemaType({ type: ["number", "null"] })).toBe("number");
|
|
57
|
+
});
|
|
58
|
+
test("returns undefined for empty or invalid schemas", () => {
|
|
59
|
+
expect(getSchemaType({})).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { mergeSchemas } from "../mergeSchemas";
|
|
2
|
+
describe("mergeSchemas", () => {
|
|
3
|
+
test("merges simple objects without overlap", () => {
|
|
4
|
+
const obj1 = { a: 1 };
|
|
5
|
+
const obj2 = { b: 2 };
|
|
6
|
+
expect(mergeSchemas(obj1, obj2)).toEqual({ a: 1, b: 2 });
|
|
7
|
+
});
|
|
8
|
+
test("merges objects with nested objects", () => {
|
|
9
|
+
const obj1 = { a: { b: 1 } };
|
|
10
|
+
const obj2 = { a: { c: 2 }, d: 3 };
|
|
11
|
+
expect(mergeSchemas(obj1, obj2)).toEqual({ a: { b: 1, c: 2 }, d: 3 });
|
|
12
|
+
});
|
|
13
|
+
test("handles merging with overlapping keys", () => {
|
|
14
|
+
const obj1 = { a: 1, b: 2 };
|
|
15
|
+
const obj2 = { a: 3, c: 4 };
|
|
16
|
+
expect(mergeSchemas(obj1, obj2)).toEqual({ a: 3, b: 2, c: 4 });
|
|
17
|
+
});
|
|
18
|
+
test("merges schemas with arrays (ensuring arrays are replaced, not merged)", () => {
|
|
19
|
+
const obj1 = { a: [1, 2], b: "string" };
|
|
20
|
+
const obj2 = { a: [3, 4] };
|
|
21
|
+
expect(mergeSchemas(obj1, obj2)).toEqual({ a: [3, 4], b: "string" });
|
|
22
|
+
});
|
|
23
|
+
test("deep merges nested objects", () => {
|
|
24
|
+
const obj1 = { a: { b: { c: 1 } } };
|
|
25
|
+
const obj2 = { a: { b: { d: 2 }, e: 3 } };
|
|
26
|
+
expect(mergeSchemas(obj1, obj2)).toEqual({
|
|
27
|
+
a: { b: { c: 1, d: 2 }, e: 3 },
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
test("retains the more specific type when merging schemas with different types", () => {
|
|
31
|
+
const obj1 = { type: ["null", "number"] };
|
|
32
|
+
const obj2 = { type: "number" };
|
|
33
|
+
expect(mergeSchemas(obj1, obj2)).toEqual({ type: "number" });
|
|
34
|
+
});
|
|
35
|
+
test("returns the second object if the first one is null or undefined", () => {
|
|
36
|
+
const obj1 = null;
|
|
37
|
+
const obj2 = { a: 1 };
|
|
38
|
+
expect(mergeSchemas(obj1, obj2)).toEqual({ a: 1 });
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
import { resolveSchema } from "../resolveSchema";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
describe("resolveSchema", () => {
|
|
3
|
+
const simpleConditionalSchema = {
|
|
4
|
+
type: "object",
|
|
5
|
+
title: "Title",
|
|
6
|
+
properties: {
|
|
7
|
+
animal: {
|
|
8
|
+
type: "string",
|
|
9
|
+
enum: ["cat", "bird"],
|
|
10
|
+
},
|
|
9
11
|
},
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
allOf: [
|
|
13
|
+
{
|
|
14
|
+
if: {
|
|
15
|
+
properties: {
|
|
16
|
+
animal: {
|
|
17
|
+
const: "cat",
|
|
18
|
+
},
|
|
17
19
|
},
|
|
18
20
|
},
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
then: {
|
|
22
|
+
properties: {
|
|
23
|
+
food: {
|
|
24
|
+
type: "string",
|
|
25
|
+
enum: ["meat", "grass", "fish"],
|
|
26
|
+
},
|
|
25
27
|
},
|
|
28
|
+
required: ["food"],
|
|
26
29
|
},
|
|
27
|
-
required: ["food"],
|
|
28
30
|
},
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
describe("resolveSchema", () => {
|
|
33
|
-
test("with matching if value", () => {
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
test("adds conditional properties when condition is met", () => {
|
|
34
34
|
const value = { animal: "cat" };
|
|
35
35
|
const resolvedSchema = resolveSchema(simpleConditionalSchema, value);
|
|
36
36
|
expect(resolvedSchema).toEqual([
|
|
@@ -52,7 +52,7 @@ describe("resolveSchema", () => {
|
|
|
52
52
|
[],
|
|
53
53
|
]);
|
|
54
54
|
});
|
|
55
|
-
test("
|
|
55
|
+
test("does not add conditional properties when condition is not met", () => {
|
|
56
56
|
const value = { animal: "bird" };
|
|
57
57
|
const resolvedSchema = resolveSchema(simpleConditionalSchema, value);
|
|
58
58
|
expect(resolvedSchema).toEqual([
|
|
@@ -69,7 +69,7 @@ describe("resolveSchema", () => {
|
|
|
69
69
|
[],
|
|
70
70
|
]);
|
|
71
71
|
});
|
|
72
|
-
test("
|
|
72
|
+
test("does not add conditional properties when no value is provided", () => {
|
|
73
73
|
const value = {};
|
|
74
74
|
const resolvedSchema = resolveSchema(simpleConditionalSchema, value);
|
|
75
75
|
expect(resolvedSchema).toEqual([
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function createInitialValues(schema) {
|
|
2
|
+
if (!schema) {
|
|
3
|
+
return {};
|
|
4
|
+
}
|
|
5
|
+
if (schema.properties) {
|
|
6
|
+
return Object.keys(schema === null || schema === void 0 ? void 0 : schema.properties).reduce((acc, key) => {
|
|
7
|
+
var _a;
|
|
8
|
+
const value = (_a = schema === null || schema === void 0 ? void 0 : schema.properties) === null || _a === void 0 ? void 0 : _a[key];
|
|
9
|
+
return Object.assign(Object.assign({}, acc), { [key]: createInitialValues(value) });
|
|
10
|
+
}, {});
|
|
11
|
+
}
|
|
12
|
+
const type = schema.type;
|
|
13
|
+
switch (type) {
|
|
14
|
+
case "array":
|
|
15
|
+
return [];
|
|
16
|
+
case "boolean":
|
|
17
|
+
return false;
|
|
18
|
+
case "null":
|
|
19
|
+
return "";
|
|
20
|
+
case "number":
|
|
21
|
+
return 0;
|
|
22
|
+
case "string":
|
|
23
|
+
return "";
|
|
24
|
+
default:
|
|
25
|
+
throw new Error("Unsupported schema type");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function getStyle(key) {
|
|
2
|
+
const style = {};
|
|
3
|
+
if (key === "inline") {
|
|
4
|
+
style.display = "inline-grid";
|
|
5
|
+
}
|
|
6
|
+
if (key === "oneColumn") {
|
|
7
|
+
style.display = "grid";
|
|
8
|
+
}
|
|
9
|
+
if (key === "twoColumns") {
|
|
10
|
+
style.display = "grid";
|
|
11
|
+
style["grid-template-columns"] = "1fr 1fr";
|
|
12
|
+
}
|
|
13
|
+
if (key === "threeColumns") {
|
|
14
|
+
style.display = "grid";
|
|
15
|
+
style["grid-template-columns"] = "1fr 1fr 1fr";
|
|
16
|
+
}
|
|
17
|
+
return style;
|
|
18
|
+
}
|
package/dist/utils/getTypes.js
CHANGED
package/dist/utils/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export * from "./getTitleAndDescription";
|
|
|
4
4
|
export * from "./getTypes";
|
|
5
5
|
export * from "./mergeSchemas";
|
|
6
6
|
export * from "./resolveSchema";
|
|
7
|
+
export * from "./createInitialValues";
|
|
7
8
|
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 "./createInitialValues";
|
|
7
8
|
//TODO: REMOVE WHEN ALL SUPPORTED
|
|
8
9
|
export const warnFromUnsupportedKeys = (componentName, data, supportedKeys) => {
|
|
9
10
|
Object.keys(data).forEach((key) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nolwenture/formik-jsonschema-form",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Formik form generation from json schema",
|
|
5
5
|
"author": "Antti Vikman <antti.vikman@nolwenture.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
"build": "tsc --build",
|
|
32
32
|
"clean": "tsc --build --clean",
|
|
33
33
|
"test": "jest",
|
|
34
|
-
"storybook": "storybook dev -p 6006"
|
|
34
|
+
"storybook": "storybook dev -p 6006",
|
|
35
|
+
"test-storybook": "test-storybook"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
38
|
"formik": "^2.4.5",
|
|
@@ -50,6 +51,7 @@
|
|
|
50
51
|
"@storybook/react": "^8.0.5",
|
|
51
52
|
"@storybook/react-webpack5": "^8.0.5",
|
|
52
53
|
"@storybook/test": "^8.0.5",
|
|
54
|
+
"@storybook/test-runner": "^0.17.0",
|
|
53
55
|
"@testing-library/jest-dom": "^6.4.2",
|
|
54
56
|
"@testing-library/react": "^14.2.2",
|
|
55
57
|
"@testing-library/user-event": "^14.5.2",
|
|
@@ -59,6 +61,7 @@
|
|
|
59
61
|
"@types/styled-components": "^5.1.34",
|
|
60
62
|
"babel-plugin-named-exports-order": "^0.0.2",
|
|
61
63
|
"jest": "^29.7.0",
|
|
64
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
62
65
|
"storybook": "^8.0.5",
|
|
63
66
|
"ts-jest": "^29.1.2",
|
|
64
67
|
"webpack": "^5.91.0"
|