@nolwenture/formik-jsonschema-form 0.4.2 → 0.6.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.
Files changed (88) hide show
  1. package/README.md +187 -0
  2. package/dist/Form.d.ts +2 -2
  3. package/dist/Form.js +15 -26
  4. package/dist/Form.stories.d.ts +12 -11
  5. package/dist/Form.stories.js +134 -101
  6. package/dist/Form.test.js +3 -3
  7. package/dist/FormEditor.d.ts +3 -3
  8. package/dist/FormEditor.js +14 -59
  9. package/dist/FormEditor.stories.d.ts +5 -4
  10. package/dist/FormEditor.stories.js +3 -16
  11. package/dist/fields/AnyField.d.ts +3 -3
  12. package/dist/fields/AnyField.js +12 -24
  13. package/dist/fields/ArrayField.d.ts +3 -3
  14. package/dist/fields/ArrayField.js +42 -64
  15. package/dist/fields/BooleanField.d.ts +3 -3
  16. package/dist/fields/BooleanField.js +7 -18
  17. package/dist/fields/DateField.d.ts +3 -3
  18. package/dist/fields/DateField.js +7 -18
  19. package/dist/fields/NullField.d.ts +3 -3
  20. package/dist/fields/NullField.js +5 -16
  21. package/dist/fields/NumberField.d.ts +3 -3
  22. package/dist/fields/NumberField.js +3 -14
  23. package/dist/fields/ObjectField.d.ts +3 -3
  24. package/dist/fields/ObjectField.js +43 -46
  25. package/dist/fields/RadioField.d.ts +3 -0
  26. package/dist/fields/RadioField.js +11 -0
  27. package/dist/fields/SchemaField.d.ts +3 -3
  28. package/dist/fields/SchemaField.js +13 -23
  29. package/dist/fields/SearchMultiTagSelectField.d.ts +3 -0
  30. package/dist/fields/SearchMultiTagSelectField.js +14 -0
  31. package/dist/fields/StringField.d.ts +3 -3
  32. package/dist/fields/StringField.js +13 -51
  33. package/dist/fields/ToggleField.d.ts +3 -0
  34. package/dist/fields/ToggleField.js +11 -0
  35. package/dist/fields/index.d.ts +3 -0
  36. package/dist/fields/index.js +3 -0
  37. package/dist/fields/types.js +1 -1
  38. package/dist/formEditorSchemas.d.ts +1 -1
  39. package/dist/formEditorSchemas.js +3 -3
  40. package/dist/hooks/index.d.ts +2 -0
  41. package/dist/hooks/index.js +2 -0
  42. package/dist/hooks/useExtractUiSchema.d.ts +8 -0
  43. package/dist/hooks/useExtractUiSchema.js +22 -0
  44. package/dist/hooks/useSetDefault.d.ts +2 -0
  45. package/dist/hooks/useSetDefault.js +16 -0
  46. package/dist/sampleData/completeSchema.d.ts +3 -0
  47. package/dist/{_sampleData.js → sampleData/completeSchema.js} +28 -97
  48. package/dist/sampleData/conditionsSchema.d.ts +3 -0
  49. package/dist/sampleData/conditionsSchema.js +80 -0
  50. package/dist/sampleData/generatedSchema.d.ts +2 -0
  51. package/dist/sampleData/generatedSchema.js +19 -0
  52. package/dist/sampleData/index.d.ts +5 -0
  53. package/dist/sampleData/index.js +5 -0
  54. package/dist/sampleData/requiredInputSchema.d.ts +3 -0
  55. package/dist/sampleData/requiredInputSchema.js +72 -0
  56. package/dist/sampleData/simpleData.d.ts +7 -0
  57. package/dist/sampleData/simpleData.js +16 -0
  58. package/dist/styledComponents.d.ts +193 -47
  59. package/dist/styledComponents.js +342 -138
  60. package/dist/templates/ArrayFieldTemplate.d.ts +3 -3
  61. package/dist/templates/ArrayFieldTemplate.js +11 -23
  62. package/dist/templates/FieldTemplate.d.ts +3 -3
  63. package/dist/templates/FieldTemplate.js +11 -23
  64. package/dist/templates/ObjectFieldTemplate.d.ts +3 -3
  65. package/dist/templates/ObjectFieldTemplate.js +13 -23
  66. package/dist/types.d.ts +34 -3
  67. package/dist/utils/__tests/getEnums.test.d.ts +1 -0
  68. package/dist/utils/__tests/getEnums.test.js +64 -0
  69. package/dist/utils/__tests/resolveSchema.test.d.ts +1 -0
  70. package/dist/utils/__tests/resolveSchema.test.js +106 -0
  71. package/dist/utils/deepEqual.d.ts +1 -0
  72. package/dist/utils/deepEqual.js +18 -0
  73. package/dist/utils/getEnums.d.ts +10 -0
  74. package/dist/utils/getEnums.js +30 -0
  75. package/dist/utils/getTitleAndDescription.d.ts +12 -0
  76. package/dist/utils/getTitleAndDescription.js +15 -0
  77. package/dist/utils/getTypes.d.ts +5 -0
  78. package/dist/utils/getTypes.js +53 -0
  79. package/dist/utils/index.d.ts +7 -0
  80. package/dist/utils/index.js +14 -0
  81. package/dist/utils/mergeSchemas.d.ts +1 -0
  82. package/dist/utils/mergeSchemas.js +26 -0
  83. package/dist/utils/resolveSchema.d.ts +2 -0
  84. package/dist/utils/resolveSchema.js +68 -0
  85. package/package.json +32 -37
  86. package/dist/_sampleData.d.ts +0 -11
  87. package/dist/utils.d.ts +0 -25
  88. package/dist/utils.js +0 -125
@@ -1,27 +1,15 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
2
  import { useContext } from "react";
14
3
  import { FormRegistry } from "../Form";
15
- import { useExtractUiSchema, getDescription, getTitle } from "../utils";
16
- export function FieldTemplate(props) {
17
- var _a = useContext(FormRegistry).components, FieldWrapper = _a.FieldWrapper, FieldTitle = _a.FieldTitle, FieldDescription = _a.FieldDescription, ErrorMessage = _a.ErrorMessage, FieldHelp = _a.FieldHelp, RequiredIndicator = _a.RequiredIndicator, InfoFieldButton = _a.InfoFieldButton;
18
- var name = props.name, uiSchema = props.uiSchema, children = props.children;
19
- var title = getTitle(props);
20
- var description = getDescription(props);
21
- var _b = useExtractUiSchema(uiSchema), help = _b.help, info = _b.info,
22
- //TODO: This is not right
23
- _c = _b.props,
4
+ import { getDescription, getTitle } from "../utils";
5
+ import { useExtractUiSchema } from "../hooks";
6
+ export const FieldTemplate = (props) => {
7
+ const { components: { FieldWrapper, FieldTitle, FieldDescription, ErrorMessage, FieldHelp, RequiredIndicator, InfoFieldButton, }, } = useContext(FormRegistry);
8
+ const { name, uiSchema, children } = props;
9
+ const title = getTitle(props);
10
+ const description = getDescription(props);
11
+ const { help, info,
24
12
  //TODO: This is not right
25
- _d = _c === void 0 ? {} : _c, wrappertProps = _d.wrapper, titleProps = _d.title, descriptionProps = _d.description, helpProps = _d.help, errorProps = _d.error;
26
- return (_jsxs(FieldWrapper, __assign({}, wrappertProps, { children: [title && (_jsxs(FieldTitle, __assign({}, titleProps, { htmlFor: name }, { children: [title, props.required && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, __assign({}, info)), description && (_jsx(FieldDescription, __assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, __assign({}, helpProps, { children: help })), _jsx(ErrorMessage, __assign({}, errorProps, { name: name }))] })));
27
- }
13
+ props: { wrapper: wrapperProps, title: titleProps, description: descriptionProps, help: helpProps, error: errorProps, } = {}, } = useExtractUiSchema(uiSchema);
14
+ return (_jsxs(FieldWrapper, Object.assign({}, wrapperProps, { children: [title && (_jsxs(FieldTitle, Object.assign({}, titleProps, { htmlFor: name, children: [title, props.required && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, Object.assign({}, info)), description && (_jsx(FieldDescription, Object.assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, Object.assign({}, helpProps, { children: help })), _jsx(ErrorMessage, Object.assign({}, errorProps, { name: name }))] })));
15
+ };
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- import { ObjectFieldTemplateProps } from "../types";
3
- export declare function ObjectFieldTemplate(props: ObjectFieldTemplateProps): JSX.Element;
1
+ import type { ObjectFieldTemplateProps } from "../types";
2
+ import React from "react";
3
+ export declare const ObjectFieldTemplate: React.FC<ObjectFieldTemplateProps>;
@@ -1,28 +1,18 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
2
  import { useContext } from "react";
14
3
  import { FormRegistry } from "../Form";
15
- import { getDescription, getTitle, useExtractUiSchema } from "../utils";
4
+ import { getDescription, getTitle } from "../utils";
5
+ import { useExtractUiSchema } from "../hooks";
16
6
  import { RequiredIndicator } from "../styledComponents";
17
- export function ObjectFieldTemplate(props) {
18
- var _a = useContext(FormRegistry).components, FieldDescription = _a.FieldDescription, ObjectFieldTitle = _a.ObjectFieldTitle, ObjectFieldWrapper = _a.ObjectFieldWrapper, FieldHelp = _a.FieldHelp, InfoFieldButton = _a.InfoFieldButton;
19
- var children = props.children, uiSchema = props.uiSchema;
20
- var title = getTitle(props);
21
- var description = getDescription(props);
22
- var _b = useExtractUiSchema(uiSchema), help = _b.help, info = _b.info,
23
- //TODO: This is not right
24
- _c = _b.props,
7
+ export const ObjectFieldTemplate = (props) => {
8
+ const { components: { FieldDescription, ObjectFieldTitle, ObjectFieldWrapper, FieldHelp, InfoFieldButton, }, } = useContext(FormRegistry);
9
+ const { children, uiSchema } = props;
10
+ const title = getTitle(props);
11
+ const description = getDescription(props);
12
+ const { help, info,
25
13
  //TODO: This is not right
26
- _d = _c === void 0 ? {} : _c, wrapperProps = _d.wrapper, titleProps = _d.title, descriptionProps = _d.description, helpProps = _d.help;
27
- return (_jsxs(ObjectFieldWrapper, __assign({}, wrapperProps, { children: [title && (_jsxs(ObjectFieldTitle, __assign({}, titleProps, { children: [title, (props === null || props === void 0 ? void 0 : props.required) && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, __assign({}, info)), description && (_jsx(FieldDescription, __assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, __assign({}, helpProps, { children: help }))] })));
28
- }
14
+ props: { wrapper: wrapperProps, title: titleProps, description: descriptionProps, help: helpProps,
15
+ // error: errorProps,
16
+ } = {}, } = useExtractUiSchema(uiSchema);
17
+ return (_jsxs(ObjectFieldWrapper, Object.assign({}, wrapperProps, { children: [title && (_jsxs(ObjectFieldTitle, Object.assign({}, titleProps, { children: [title, (props === null || props === void 0 ? void 0 : props.required) && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, Object.assign({}, info)), description && (_jsx(FieldDescription, Object.assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, Object.assign({}, helpProps, { children: help }))] })));
18
+ };
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import * as React from "react";
2
- import { JSONSchema7 } from "json-schema";
1
+ import type { JSONSchema7 } from "json-schema";
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 */
5
5
  disabled?: boolean | undefined;
@@ -37,6 +37,11 @@ export type UiSchema = {
37
37
  };
38
38
  "ui:help"?: React.ReactNode | React.ReactNode[];
39
39
  "ui:info"?: InfoFieldButtonProps;
40
+ "ui:placeholderOptionOptions"?: {
41
+ props: {
42
+ [key: string]: any;
43
+ };
44
+ };
40
45
  [name: string]: any;
41
46
  };
42
47
  export type FormRegistryContextValue = {
@@ -121,6 +126,9 @@ export type ComponentsType = {
121
126
  ErrorMessage: React.FunctionComponent<any>;
122
127
  FieldInput: React.FunctionComponent<any>;
123
128
  RequiredIndicator: React.FunctionComponent<any>;
129
+ RadioButtonField: React.FunctionComponent<any>;
130
+ ToggleSwitchField: React.FunctionComponent<any>;
131
+ SearchableMultiTagSelectField: React.FunctionComponent<any>;
124
132
  };
125
133
  export declare enum ObjectFieldButtonPurpose {
126
134
  ADD = 0,
@@ -147,10 +155,33 @@ export type ArrayFieldButtonProps = {
147
155
  export type InfoFieldButtonProps = {
148
156
  label?: React.ReactNode;
149
157
  content: React.ReactNode;
150
- position?: 'top' | 'right' | 'bottom' | 'left';
158
+ position?: "top" | "right" | "bottom" | "left";
151
159
  className?: string;
152
160
  props: any;
153
161
  };
162
+ type RadioButtonLabelObject = {
163
+ true?: string;
164
+ false?: string;
165
+ };
166
+ export type RadioButtonFieldProps = {
167
+ label?: RadioButtonLabelObject;
168
+ inputProps?: any;
169
+ name?: React.ReactNode;
170
+ className?: string;
171
+ };
172
+ export type ToggleSwitchFieldProps = {
173
+ inputProps: any;
174
+ name: React.ReactNode;
175
+ className?: string;
176
+ };
177
+ export type SelectFieldProps = {
178
+ name: string;
179
+ enums: any;
180
+ emptySearchMessage?: string;
181
+ placeholder?: string;
182
+ closeElement?: React.ReactNode;
183
+ className?: string;
184
+ };
154
185
  /*** ***/
155
186
  export type AjvError = {
156
187
  message: string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,64 @@
1
+ import { getEnums } from "../getEnums";
2
+ describe("getEnums", () => {
3
+ test("enum works", () => {
4
+ const result = getEnums({ schema: { enum: ["one", "two", "three"] } });
5
+ expect(result).toEqual({
6
+ type: "select",
7
+ values: [
8
+ { const: "one", title: "one" },
9
+ { const: "two", title: "two" },
10
+ { const: "three", title: "three" },
11
+ ],
12
+ });
13
+ });
14
+ test("oneOf works", () => {
15
+ const result = getEnums({
16
+ schema: {
17
+ oneOf: [
18
+ {
19
+ const: "horse",
20
+ title: "Horse",
21
+ },
22
+ { const: "frog", title: "Frog" },
23
+ { const: "bird", title: "Bird" },
24
+ ],
25
+ },
26
+ });
27
+ expect(result).toEqual({
28
+ type: "select",
29
+ values: [
30
+ {
31
+ const: "horse",
32
+ title: "Horse",
33
+ },
34
+ { const: "frog", title: "Frog" },
35
+ { const: "bird", title: "Bird" },
36
+ ],
37
+ });
38
+ });
39
+ test("anyOf works", () => {
40
+ const result = getEnums({
41
+ schema: {
42
+ anyOf: [
43
+ {
44
+ const: "tangerine",
45
+ title: "Tangerine",
46
+ },
47
+ { const: "mandarin", title: "Mandarin" },
48
+ { const: "bruno", title: "Bruno" },
49
+ ],
50
+ },
51
+ });
52
+ expect(result).toEqual({
53
+ type: "multiSelect",
54
+ values: [
55
+ {
56
+ const: "tangerine",
57
+ title: "Tangerine",
58
+ },
59
+ { const: "mandarin", title: "Mandarin" },
60
+ { const: "bruno", title: "Bruno" },
61
+ ],
62
+ });
63
+ });
64
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,106 @@
1
+ import { resolveSchema } from "../resolveSchema";
2
+ const simpleConditionalSchema = {
3
+ type: "object",
4
+ title: "Title",
5
+ properties: {
6
+ animal: {
7
+ type: "string",
8
+ enum: ["cat", "bird"],
9
+ },
10
+ },
11
+ allOf: [
12
+ {
13
+ if: {
14
+ properties: {
15
+ animal: {
16
+ const: "cat",
17
+ },
18
+ },
19
+ },
20
+ then: {
21
+ properties: {
22
+ food: {
23
+ type: "string",
24
+ enum: ["meat", "grass", "fish"],
25
+ },
26
+ },
27
+ required: ["food"],
28
+ },
29
+ },
30
+ ],
31
+ };
32
+ describe("resolveSchema", () => {
33
+ test("with matching if value", () => {
34
+ const value = { animal: "cat" };
35
+ const resolvedSchema = resolveSchema(simpleConditionalSchema, value);
36
+ expect(resolvedSchema).toEqual([
37
+ {
38
+ type: "object",
39
+ title: "Title",
40
+ properties: {
41
+ animal: {
42
+ type: "string",
43
+ enum: ["cat", "bird"],
44
+ },
45
+ food: {
46
+ type: "string",
47
+ enum: ["meat", "grass", "fish"],
48
+ },
49
+ },
50
+ required: ["food"],
51
+ },
52
+ [],
53
+ ]);
54
+ });
55
+ test("with non-matching if value", () => {
56
+ const value = { animal: "bird" };
57
+ const resolvedSchema = resolveSchema(simpleConditionalSchema, value);
58
+ expect(resolvedSchema).toEqual([
59
+ {
60
+ type: "object",
61
+ title: "Title",
62
+ properties: {
63
+ animal: {
64
+ type: "string",
65
+ enum: ["cat", "bird"],
66
+ },
67
+ },
68
+ },
69
+ [],
70
+ ]);
71
+ });
72
+ test("with empty values", () => {
73
+ const value = {};
74
+ const resolvedSchema = resolveSchema(simpleConditionalSchema, value);
75
+ expect(resolvedSchema).toEqual([
76
+ {
77
+ type: "object",
78
+ title: "Title",
79
+ properties: {
80
+ animal: {
81
+ type: "string",
82
+ enum: ["cat", "bird"],
83
+ },
84
+ },
85
+ },
86
+ [],
87
+ ]);
88
+ });
89
+ test("with value changed", () => {
90
+ const value = { animal: "bird", food: "meat" };
91
+ const resolvedSchema = resolveSchema(simpleConditionalSchema, value);
92
+ expect(resolvedSchema).toEqual([
93
+ {
94
+ type: "object",
95
+ title: "Title",
96
+ properties: {
97
+ animal: {
98
+ type: "string",
99
+ enum: ["cat", "bird"],
100
+ },
101
+ },
102
+ },
103
+ ["food"],
104
+ ]);
105
+ });
106
+ });
@@ -0,0 +1 @@
1
+ export declare const deepEqual: (object1: any, object2: any) => boolean;
@@ -0,0 +1,18 @@
1
+ import { isObject } from "./getTypes";
2
+ export const deepEqual = (object1, object2) => {
3
+ const keys1 = Object.keys(object1);
4
+ const keys2 = Object.keys(object2);
5
+ if (keys1.length !== keys2.length) {
6
+ return false;
7
+ }
8
+ for (const key of keys1) {
9
+ const val1 = object1[key];
10
+ const val2 = object2[key];
11
+ const areObjects = isObject(val1) && isObject(val2);
12
+ if ((areObjects && !deepEqual(val1, val2)) ||
13
+ (!areObjects && val1 !== val2)) {
14
+ return false;
15
+ }
16
+ }
17
+ return true;
18
+ };
@@ -0,0 +1,10 @@
1
+ import type { FieldProps } from "../types";
2
+ export declare const getEnums: ({ schema }: {
3
+ schema: FieldProps["schema"];
4
+ }) => {
5
+ type: string;
6
+ values: {
7
+ const: any;
8
+ title: string;
9
+ }[];
10
+ } | undefined;
@@ -0,0 +1,30 @@
1
+ export const getEnums = ({ schema }) => {
2
+ const numberOfDefined = [schema.enum, schema.oneOf, schema.anyOf].filter((x) => x).length;
3
+ if (numberOfDefined === 0) {
4
+ return;
5
+ }
6
+ if (numberOfDefined > 1) {
7
+ throw new Error('Only one of "enum", "oneOf" or "anyOf" can be defined!');
8
+ }
9
+ if (schema.enum) {
10
+ return {
11
+ type: "select",
12
+ values: schema.enum.map((option) => ({
13
+ const: option,
14
+ title: `${option}`,
15
+ })),
16
+ };
17
+ }
18
+ if (schema.oneOf) {
19
+ return {
20
+ type: "select",
21
+ values: schema.oneOf,
22
+ };
23
+ }
24
+ if (schema.anyOf) {
25
+ return {
26
+ type: "multiSelect",
27
+ values: schema.anyOf,
28
+ };
29
+ }
30
+ };
@@ -0,0 +1,12 @@
1
+ import type { Schema, UiSchema } from "../types";
2
+ export declare const getTitle: ({ title, uiSchema, schema, name, }: {
3
+ title?: string | undefined;
4
+ uiSchema?: UiSchema | undefined;
5
+ schema?: import("json-schema").JSONSchema7 | undefined;
6
+ name?: string | undefined;
7
+ }) => any;
8
+ export declare const getDescription: ({ description, uiSchema, schema, }: {
9
+ description?: string | undefined;
10
+ uiSchema?: UiSchema | undefined;
11
+ schema?: import("json-schema").JSONSchema7 | undefined;
12
+ }) => any;
@@ -0,0 +1,15 @@
1
+ export const getTitle = ({ title, uiSchema, schema, name, }) => {
2
+ var _a;
3
+ return (title ||
4
+ ((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:options"]) === null || _a === void 0 ? void 0 : _a.title) ||
5
+ (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:title"]) ||
6
+ (schema === null || schema === void 0 ? void 0 : schema.title) ||
7
+ (name === null || name === void 0 ? void 0 : name.split(".")[(name === null || name === void 0 ? void 0 : name.split(".").length) - 1]));
8
+ };
9
+ export const getDescription = ({ description, uiSchema, schema, }) => {
10
+ var _a;
11
+ return (description ||
12
+ ((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:options"]) === null || _a === void 0 ? void 0 : _a.description) ||
13
+ (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:description"]) ||
14
+ (schema === null || schema === void 0 ? void 0 : schema.description));
15
+ };
@@ -0,0 +1,5 @@
1
+ import type { Schema } from "../types";
2
+ export declare const isObject: (object: any) => boolean;
3
+ export declare const getDefaultValueForType: (type: Schema["type"]) => {} | null;
4
+ export declare const guessType: (value: any) => "string" | "number" | "boolean" | "object" | "array" | "null";
5
+ export declare const getSchemaType: (schema: Schema) => import("json-schema").JSONSchema7TypeName | import("json-schema").JSONSchema7TypeName[] | undefined;
@@ -0,0 +1,53 @@
1
+ export const isObject = (object) => {
2
+ return object != null && typeof object === "object";
3
+ };
4
+ export const getDefaultValueForType = (type) => {
5
+ if (type && !Array.isArray(type)) {
6
+ if (type === "array")
7
+ return [];
8
+ if (type === "object")
9
+ return {};
10
+ }
11
+ return null;
12
+ };
13
+ export const guessType = (value) => {
14
+ if (Array.isArray(value)) {
15
+ return "array";
16
+ }
17
+ else if (typeof value === "string") {
18
+ return "string";
19
+ }
20
+ else if (value == null) {
21
+ return "null";
22
+ }
23
+ else if (typeof value === "boolean") {
24
+ return "boolean";
25
+ }
26
+ else if (!isNaN(value)) {
27
+ return "number";
28
+ }
29
+ else if (typeof value === "object") {
30
+ return "object";
31
+ }
32
+ // Default to string if we can't figure it out
33
+ return "string";
34
+ };
35
+ export const getSchemaType = (schema) => {
36
+ if (!schema) {
37
+ return undefined;
38
+ }
39
+ let { type } = schema;
40
+ if (!type && schema.const) {
41
+ return guessType(schema.const);
42
+ }
43
+ if (!type && schema.enum) {
44
+ return "string";
45
+ }
46
+ if (!type && (schema.properties || schema.additionalProperties)) {
47
+ return "object";
48
+ }
49
+ if (type instanceof Array && type.length === 2 && type.includes("null")) {
50
+ return type.find((type) => type !== "null");
51
+ }
52
+ return type;
53
+ };
@@ -0,0 +1,7 @@
1
+ export * from "./deepEqual";
2
+ export * from "./getEnums";
3
+ export * from "./getTitleAndDescription";
4
+ export * from "./getTypes";
5
+ export * from "./mergeSchemas";
6
+ export * from "./resolveSchema";
7
+ export declare const warnFromUnsupportedKeys: (componentName: string, data: Record<string, any>, supportedKeys: string[]) => void;
@@ -0,0 +1,14 @@
1
+ export * from "./deepEqual";
2
+ export * from "./getEnums";
3
+ export * from "./getTitleAndDescription";
4
+ export * from "./getTypes";
5
+ export * from "./mergeSchemas";
6
+ export * from "./resolveSchema";
7
+ //TODO: REMOVE WHEN ALL SUPPORTED
8
+ export const warnFromUnsupportedKeys = (componentName, data, supportedKeys) => {
9
+ Object.keys(data).forEach((key) => {
10
+ if (!supportedKeys.includes(key)) {
11
+ console.warn(`Component "${componentName}" does not support (yet) key "${key}"`);
12
+ }
13
+ });
14
+ };
@@ -0,0 +1 @@
1
+ export declare const mergeSchemas: (obj1: any, obj2: any) => any;
@@ -0,0 +1,26 @@
1
+ import { getSchemaType } from "./getTypes";
2
+ export const mergeSchemas = (obj1, obj2) => {
3
+ const acc = Object.assign({}, obj1); // Prevent mutation of source object.
4
+ return Object.keys(obj2).reduce((acc, key) => {
5
+ const left = obj1 ? obj1[key] : {}, right = obj2[key];
6
+ if (obj1 &&
7
+ key in obj1 &&
8
+ typeof right === "object" &&
9
+ !Array.isArray(right)) {
10
+ acc[key] = mergeSchemas(left, right);
11
+ }
12
+ else if (obj1 &&
13
+ obj2 &&
14
+ (getSchemaType(obj1) === "object" || getSchemaType(obj2) === "object") &&
15
+ key === "required" &&
16
+ Array.isArray(left) &&
17
+ Array.isArray(right)) {
18
+ // Don't include duplicate values when merging 'required' fields.
19
+ acc[key] = [...new Set([...left, ...right])];
20
+ }
21
+ else {
22
+ acc[key] = right;
23
+ }
24
+ return acc;
25
+ }, acc);
26
+ };
@@ -0,0 +1,2 @@
1
+ import type { Schema } from "../types";
2
+ export declare const resolveSchema: ({ allOf, ...schema }: Schema, value: Record<string, any>) => [Schema, string[]];
@@ -0,0 +1,68 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { deepEqual } from "./deepEqual";
13
+ import { mergeSchemas } from "./mergeSchemas";
14
+ const resolveEquality = (value, expression) => {
15
+ const keyOfValue = Object.keys(value);
16
+ const valueOfValue = Object.values(value);
17
+ const newObject = {
18
+ properties: { [keyOfValue[0]]: { const: valueOfValue[0] } },
19
+ };
20
+ return deepEqual(newObject, expression);
21
+ };
22
+ export const resolveSchema = (_a, value) => {
23
+ var _b;
24
+ var { allOf } = _a, schema = __rest(_a, ["allOf"]);
25
+ if (!allOf) {
26
+ return [schema, []];
27
+ }
28
+ const { resolvedSchema, notAddedProperties } = allOf.reduce(({ resolvedSchema, notAddedProperties }, current) => {
29
+ var _a, _b;
30
+ if (!current || typeof current === "boolean") {
31
+ return { resolvedSchema, notAddedProperties };
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
+ };
39
+ }
40
+ if (resolveEquality(value, expression)) {
41
+ if (then && typeof then !== "boolean") {
42
+ resolvedSchema = mergeSchemas(resolvedSchema, then);
43
+ }
44
+ if (otherwise && typeof otherwise !== "boolean") {
45
+ notAddedProperties = [
46
+ ...notAddedProperties,
47
+ ...Object.keys((_a = otherwise.properties) !== null && _a !== void 0 ? _a : {}),
48
+ ];
49
+ }
50
+ }
51
+ else {
52
+ if (then && typeof then !== "boolean") {
53
+ notAddedProperties = [
54
+ ...notAddedProperties,
55
+ ...Object.keys((_b = then.properties) !== null && _b !== void 0 ? _b : {}),
56
+ ];
57
+ }
58
+ if (otherwise && typeof otherwise !== "boolean") {
59
+ resolvedSchema = mergeSchemas(resolvedSchema, otherwise);
60
+ }
61
+ }
62
+ return { resolvedSchema, notAddedProperties };
63
+ }, { resolvedSchema: schema, notAddedProperties: [] });
64
+ //Values that do not exists due to conditions
65
+ const usedProperties = Object.keys((_b = resolvedSchema.properties) !== null && _b !== void 0 ? _b : {});
66
+ const valuesToRemove = [...new Set([...notAddedProperties])].filter((key) => !usedProperties.includes(key) && typeof value[key] !== "undefined");
67
+ return [resolvedSchema, valuesToRemove];
68
+ };