-
Notifications
You must be signed in to change notification settings - Fork 7
/
json-schema-form.d.ts
140 lines (131 loc) · 3.61 KB
/
json-schema-form.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Shorthand to lookup for keys with `x-jsf-*` preffix.
*/
export function pickXKey(node: Object, key: 'presentation' | 'errorMessage'): Object | undefined;
type ValidationTypes =
| 'type'
| 'minimum'
| 'maximum'
| 'minLength'
| 'maxLength'
| 'pattern'
| 'maxFileSize'
| 'accept'
| 'required';
type JSFConfig = {
/**
* Initial json values to prefill the form fields.
* This influences the initial visibility of conditional fields.
*/
initialValues?: Record<string, unknown>;
/**
* It inforces all json properties (fields) to have x-jsf-presentation.inputType.
@default true
*/
strictInputType?: boolean;
/**
* Customize the output of a given named Field.
* Useful if you want to enhance the UI/UX of a particular field.
* @example
* { has_pet: { "optionsDirection": "horizontal" } }
*/
customProperties?: {
description?: (description: string, field: $TSFixMe) => string | string;
[key: string]: unknown;
};
/**
* Customize the config for each inputType
*/
inputTypes?: {
/**
* Customize the error error message of this input type.
* @example
* { required: "This number is required." }
*/
errorMessage?: Partial<Record<ValidationTypes, string>>;
};
};
type Fields = Record<string, unknown>[]; //TODO: Type the field based on the given JSON Schema properties.
type $TSFixMe = any;
/**
* Returns the Yup schema structure of given fields.
* These fields must be the same from
* const { fields } = createHeadlessForm()
*/
export function buildCompleteYupSchema(fields: Fields, config: JSFConfig): $TSFixMe; //TODO: We need to update Yup to 1.0 which supports TS.
type HeadlessFormOutput = {
/**
* List of Fields. Each Field corresponds to a form input from the json schema.
* @example
* [
{
name: "has_pet",
label: "Has Pet",
options: [
{ label: "Yes", value: "yes" },
{ label: "No", value: "no" }
],
required: true,
inputType: "radio",
jsonType: "string",
description: "Do you have a pet?",
errorMessage: {},
isVisible: true
},
{
type: "text",
label: "Pet's name",
description: "What's your pet's name?",
required: false,
inputType: "text",
jsonType: "string",
isVisible: false
}
* ]
*/
fields: Fields;
/**
* Validates given values and returns the respective errors.
* @example
* const { formErrors } = handleValidation({ has_pet: "yes" });
* console.log(formErrors) // { pet_name: "Required field." }
*/
handleValidation: (values: Record<string, unknown>) => {
yupError: $TSFixMe;
formErrors: $TSFixMe;
};
isError: boolean;
error?: Error;
};
type JSONSchemaObjectType = Record<string, unknown>;
/**
* Generates the Headless form based on the provided JSON schema
*/
export function createHeadlessForm(
/** A JSON Schema of type object */
jsonSchema: JSONSchemaObjectType,
customConfig?: JSFConfig
): HeadlessFormOutput;
type MutationsConfig = {
/**
* An object with the list of fields to be mutated.
*/
fields: Record<string, unknown>;
/**
* a callback function that applies a mutation to all the fields.
* @param fieldName
* @param fieldAttrs
* @returns
*/
allFields: (fieldName: string, fieldAttrs: Record<string, unknown>) => Record<string, unknown>;
};
export function modify(
/**
* The original JSON schema to be modified.
*/
originalSchema: JSONSchemaObjectType,
/**
* The configuration object containing the fields and allFields properties.
*/
config?: MutationsConfig
);