-
Notifications
You must be signed in to change notification settings - Fork 9
/
formalistic.d.ts
249 lines (215 loc) · 9 KB
/
formalistic.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
export type Item = Field<any> | MapForm<any> | ListForm<any>;
/*
* Fields
*/
export interface CreateFieldOpts<VALUE_TYPE> {
value: VALUE_TYPE;
isEqual?: (a: VALUE_TYPE, b: VALUE_TYPE) => boolean;
touched?: boolean;
validator?: (value: VALUE_TYPE) => ValidationResult;
}
export interface Field<VALUE_TYPE> {
readonly value: VALUE_TYPE;
readonly touched: boolean;
readonly hierarchyTouched: boolean;
readonly messages: ValidationMessage[];
readonly maxSeverity: Severity;
readonly valid: boolean;
readonly maxSeverityOfHierarchy: Severity;
readonly hierarchyValid: boolean;
setValue(newValue: VALUE_TYPE): Field<VALUE_TYPE>;
setTouched(touched: boolean): Field<VALUE_TYPE>;
getAllMessagesInHierarchy(): ValidationMessage[];
toJS(): VALUE_TYPE;
map<T>(mapper: (field: Field<VALUE_TYPE>) => T): T;
}
export function createField<VALUE_TYPE>(opts: CreateFieldOpts<VALUE_TYPE>): Field<VALUE_TYPE>;
/*
* MapForm
*/
export interface MapFormItems {
[path: string]: Item;
}
export interface CreateMapFormOpts<VALUE_TYPE extends MapFormItems> {
items?: VALUE_TYPE;
touched?: boolean;
validator?: (form: VALUE_TYPE) => ValidationResult;
}
export interface SetTouchedOptions {
recurse: boolean;
}
export interface MapForm<VALUE_TYPE extends MapFormItems> {
readonly touched: boolean;
readonly messages: ValidationMessage[];
readonly maxSeverity: Severity;
readonly valid: boolean;
readonly maxSeverityOfHierarchy: Severity;
readonly hierarchyTouched: boolean;
readonly hierarchyValid: boolean;
put<P extends string, I extends Item>(path: P, item: I): UpdatedMapForm<VALUE_TYPE, P, I>
get<P extends keyof VALUE_TYPE>(path: P): VALUE_TYPE[P];
getIn<P extends Path<VALUE_TYPE>>(path: P): NestedValueType<VALUE_TYPE, P>;
remove<P extends keyof VALUE_TYPE>(path: P): MapForm<Omit<VALUE_TYPE, P>>;
reduce<R>(reducer: (acc: R, cur: VALUE_TYPE[keyof VALUE_TYPE], key: keyof VALUE_TYPE) => R, seed: R): R;
containsKey(key: keyof VALUE_TYPE): boolean;
updateIn<P extends Path<VALUE_TYPE>, I extends Item>(path: P, updater: (item: NestedValueType<VALUE_TYPE, P>) => I): NestedUpdatedMapForm<VALUE_TYPE, P, I>;
setTouched(touched: boolean, opts?: SetTouchedOptions): MapForm<VALUE_TYPE>;
getAllMessagesInHierarchy(): ValidationMessage[];
toJS(): ItemValueType<MapForm<VALUE_TYPE>>;
}
export function createMapForm<VALUE_TYPE extends MapFormItems>(opts?: CreateMapFormOpts<VALUE_TYPE>): MapForm<VALUE_TYPE>;
/*
* ListForm
*/
export interface CreateListFormOpts<VALUE_TYPE extends Item[]> {
items?: VALUE_TYPE;
touched?: boolean;
validator?: (form: VALUE_TYPE) => ValidationResult;
}
export interface ListForm<VALUE_TYPE extends Item[]> {
readonly size: number;
readonly touched: boolean;
readonly messages: ValidationMessage[];
readonly maxSeverity: Severity;
readonly valid: boolean;
readonly maxSeverityOfHierarchy: Severity;
readonly hierarchyTouched: boolean;
readonly hierarchyValid: boolean;
push(item: VALUE_TYPE[number]): ListForm<VALUE_TYPE>;
insert(index: number, item: VALUE_TYPE[number]): ListForm<VALUE_TYPE>;
set(index: number, item: VALUE_TYPE[number]): ListForm<VALUE_TYPE>;
unshift(item: VALUE_TYPE[number]): ListForm<VALUE_TYPE>;
remove(index: number): ListForm<VALUE_TYPE>;
get(index: number): VALUE_TYPE[number] | undefined;
getIn<P extends Path<VALUE_TYPE>>(path: P): NestedValueType<VALUE_TYPE, P>;
updateIn<P extends Path<VALUE_TYPE>, I extends Item>(path: P, updater: (item: NestedValueType<VALUE_TYPE, P>) => I): NestedUpdatedListForm<VALUE_TYPE, P, I>;
setTouched(touched: boolean, opts?: SetTouchedOptions): ListForm<VALUE_TYPE>;
getAllMessagesInHierarchy(): ValidationMessage[];
map<M>(mapper: (item: VALUE_TYPE[number]) => M): M[];
reduce<R>(reducer: (acc: R, cur: VALUE_TYPE[number], index: number) => R, seed: R): R;
moveUp(index: number): ListForm<VALUE_TYPE>;
moveDown(index: number): ListForm<VALUE_TYPE>;
toJS(): ItemValueType<VALUE_TYPE[number]>[];
}
export function createListForm<VALUE_TYPE extends Item[]>(opts: CreateListFormOpts<VALUE_TYPE>): ListForm<VALUE_TYPE>;
/*
* Validation
*/
export type Severity = 'error' | 'warning' | 'info' | 'ok';
export type ValidationResult = ValidationMessage[] | null | undefined;
export interface ValidationMessage {
severity: Severity;
message?: string;
// A JSON path describing the path to the form item. This
// field gets automatically populated when calling
// getAllMessagesInHierarchy().
path?: string;
}
export function notBlankValidator(s?: string): ValidationResult;
export function alwaysValidValidator(v: any): ValidationResult;
export function composeValidators(...validators: ((v: any) => ValidationResult)[]): (v: any) => ValidationResult;
/*
* Internal Types
*/
/*
* Paths
*/
type Path<VALUE_TYPE extends MapFormItems | Item[]> = VALUE_TYPE extends MapFormItems
? MapPath<VALUE_TYPE>
: VALUE_TYPE extends Item[] ? ListPath<VALUE_TYPE> : never;
type MapPath<VALUE_TYPE extends MapFormItems> = {
[Key in keyof VALUE_TYPE]: VALUE_TYPE[Key] extends MapForm<infer MIT>
? [Key] | [Key, ...MapPath<MIT>]
: VALUE_TYPE[Key] extends ListForm<infer LIT>
? [Key] | [Key, ...ListPath<LIT>]
: [Key]
}[keyof VALUE_TYPE];
type ListPath<VALUE_TYPE extends Item[]> = VALUE_TYPE[number] extends ListForm<infer LIT>
? [number] | [number, ...ListPath<LIT>]
: VALUE_TYPE[number] extends MapForm<infer MIT>
? [number] | [number, ...MapPath<MIT>]
: [number];
/*
* Value Types
*/
type ItemValueType<I extends Item> = I extends Field<infer FT>
? FT
: I extends MapForm<infer MIT>
? { [Key in keyof MIT]: ItemValueType<MIT[Key]> }
: I extends ListForm<infer LIT>
? ItemValueType<LIT[number]>[]
: never;
type NestedValueType<BASE_VALUE_TYPE extends MapFormItems | Item[], PATH extends Path<BASE_VALUE_TYPE>> = BASE_VALUE_TYPE extends MapFormItems
? NestedMapValueType<BASE_VALUE_TYPE, PATH>
: BASE_VALUE_TYPE extends Item[]
? NestedListValueType<BASE_VALUE_TYPE, PATH>
: never;
type NestedMapValueType<BASE_VALUE_TYPE extends MapFormItems, PATH extends Path<BASE_VALUE_TYPE>> = PATH extends [infer Key, ...infer Rest]
? Key extends string
? BASE_VALUE_TYPE[Key] extends infer Value_Type
? Rest extends []
? Value_Type
: Value_Type extends MapForm<infer MIT>
? Rest extends Path<MIT>
? NestedValueType<MIT, Rest>
: never
: Value_Type extends ListForm<infer LIT>
? Rest extends Path<LIT>
? NestedValueType<LIT, Rest>
: never
: never
: never
: never
: never;
type NestedListValueType<BASE_VALUE_TYPE extends Item[], PATH extends Path<BASE_VALUE_TYPE>> = PATH extends [number, ...infer Rest]
? BASE_VALUE_TYPE[number] extends infer Value_Type
? Rest extends []
? Value_Type
: Value_Type extends MapForm<infer MIT>
? Rest extends Path<MIT>
? NestedValueType<MIT, Rest>
: never
: Value_Type extends ListForm<infer LIT>
? Rest extends Path<LIT>
? NestedValueType<LIT, Rest>
: never
: never
: never
: never;
/*
* Updates
*/
type UpdatedMapForm<BASE_VALUE_TYPE extends MapFormItems, PATH extends string, I extends Item> = MapForm<Omit<BASE_VALUE_TYPE, PATH> & { [Path in PATH]: I}>;
type UpdatedListForm<BASE_VALUE_TYPE extends Item[], I extends Item> = I extends BASE_VALUE_TYPE[number] ? ListForm<BASE_VALUE_TYPE> : ListForm<(BASE_VALUE_TYPE[number] | I)[]>;
type NestedUpdatedMapForm<BASE_VALUE_TYPE extends MapFormItems, PATH extends Path<BASE_VALUE_TYPE>, UPDATED_NESTED_VALUE_TYPE extends Item> = PATH extends [infer Key, ...infer Rest]
? Key extends string
? BASE_VALUE_TYPE[Key] extends infer Value_Type
? Rest extends []
? UpdatedMapForm<BASE_VALUE_TYPE, Key, UPDATED_NESTED_VALUE_TYPE>
: Value_Type extends MapForm<infer MIT>
? Rest extends Path<MIT>
? UpdatedMapForm<BASE_VALUE_TYPE, Key, NestedUpdatedMapForm<MIT, Rest, UPDATED_NESTED_VALUE_TYPE>>
: never
: Value_Type extends ListForm<infer LIT>
? Rest extends Path<LIT>
? UpdatedMapForm<BASE_VALUE_TYPE, Key, NestedUpdatedListForm<LIT, Rest, UPDATED_NESTED_VALUE_TYPE>>
: never
: never
: never
: never
: never;
type NestedUpdatedListForm<BASE_VALUE_TYPE extends Item[], PATH extends Path<BASE_VALUE_TYPE>, UPDATED_NESTED_VALUE_TYPE extends Item> = PATH extends [number, ...infer Rest]
? BASE_VALUE_TYPE[number] extends infer Value_Type
? Rest extends []
? UpdatedListForm<BASE_VALUE_TYPE, UPDATED_NESTED_VALUE_TYPE>
: Value_Type extends MapForm<infer MIT>
? Rest extends Path<MIT>
? UpdatedListForm<BASE_VALUE_TYPE, NestedUpdatedMapForm<MIT, Rest, UPDATED_NESTED_VALUE_TYPE>>
: never
: Value_Type extends ListForm<infer LIT>
? Rest extends Path<LIT>
? UpdatedListForm<BASE_VALUE_TYPE, NestedUpdatedListForm<LIT, Rest, UPDATED_NESTED_VALUE_TYPE>>
: never
: never
: never
: never;