@nolwenture/formik-jsonschema-form 0.9.2 → 0.9.3

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.
@@ -74,5 +74,40 @@ describe("Create Initial Values", () => {
74
74
  },
75
75
  };
76
76
  expect(result).toEqual(expected);
77
+ // Just to test types
78
+ const typeTest = {
79
+ // @ts-expect-error
80
+ // Should only accept empty string
81
+ stringIncorrect: "something",
82
+ stringCorrect: "",
83
+ // @ts-expect-error
84
+ // Should only accept 0
85
+ numberIncorrect: 123,
86
+ // @ts-expect-error
87
+ numberIncorrect2: -123,
88
+ numberCorrect: 0,
89
+ // @ts-expect-error
90
+ // Should only accept empty array
91
+ arrayIncorrect: [""],
92
+ // @ts-expect-error
93
+ arrayIncorrect2: [1],
94
+ arrayCorrect: [],
95
+ // @ts-expect-error
96
+ // Should only accept boolean false
97
+ booleanIncorrect: true,
98
+ booleanCorrect: false,
99
+ object: {
100
+ string: "",
101
+ number: 0,
102
+ array: [],
103
+ boolean: false,
104
+ nested: {
105
+ string: "",
106
+ number: 0,
107
+ array: [],
108
+ boolean: false,
109
+ },
110
+ },
111
+ };
77
112
  }));
78
113
  });
@@ -1,4 +1,7 @@
1
1
  import { JSONSchema7 } from "json-schema";
2
- type ReturnType = 0 | "" | 1 | boolean | Array<any> | object;
3
- export declare function createInitialValues(schema: JSONSchema7): ReturnType;
2
+ type PropertyValue = 0 | "" | false | never[] | InitialValuesType;
3
+ export interface InitialValuesType {
4
+ [key: string]: PropertyValue;
5
+ }
6
+ export declare function createInitialValues(schema: JSONSchema7, definitions?: JSONSchema7): InitialValuesType;
4
7
  export {};
@@ -1,33 +1,34 @@
1
1
  import { solveReferences } from "./solveReferences";
2
- let schemaDefinitions = {};
3
- export function createInitialValues(schema) {
4
- if (!schema) {
2
+ export function createInitialValues(schema, definitions = {}) {
3
+ var _a;
4
+ if (!schema || !schema.properties) {
5
5
  return {};
6
6
  }
7
- if (schema.definitions) {
8
- schemaDefinitions = schema.definitions;
9
- }
10
- if (schema.properties) {
11
- return Object.keys(schema === null || schema === void 0 ? void 0 : schema.properties).reduce((acc, key) => {
12
- var _a;
13
- const value = (_a = schema === null || schema === void 0 ? void 0 : schema.properties) === null || _a === void 0 ? void 0 : _a[key];
14
- const solvedValue = solveReferences(value, schemaDefinitions);
15
- return Object.assign(Object.assign({}, acc), { [key]: createInitialValues(solvedValue) });
16
- }, {});
17
- }
18
- const type = schema.type;
19
- switch (type) {
20
- case "array":
21
- return [];
22
- case "boolean":
23
- return false;
24
- case "null":
25
- return "";
26
- case "number":
27
- return 0;
28
- case "string":
29
- return "";
30
- default:
31
- throw new Error("Unsupported schema type");
7
+ const schemaDefinitions = Object.assign(Object.assign({}, definitions), ((_a = schema.definitions) !== null && _a !== void 0 ? _a : {}));
8
+ function getValue(schema) {
9
+ if (schema.properties) {
10
+ return createInitialValues(schema, schemaDefinitions);
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
+ }
32
27
  }
28
+ return Object.keys(schema === null || schema === void 0 ? void 0 : schema.properties).reduce((acc, key) => {
29
+ var _a;
30
+ const value = (_a = schema === null || schema === void 0 ? void 0 : schema.properties) === null || _a === void 0 ? void 0 : _a[key];
31
+ const solvedValue = solveReferences(value, schemaDefinitions);
32
+ return Object.assign(Object.assign({}, acc), { [key]: getValue(solvedValue) });
33
+ }, {});
33
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "Antti Vikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [