/
MobxZodField.ts
348 lines (331 loc) · 9.46 KB
/
MobxZodField.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import {
ZodString,
ZodNumber,
ParsePath,
ZodBoolean,
ZodObject,
ZodArray,
ZodIssue,
ZodTypeAny,
SafeParseReturnType,
ZodDiscriminatedUnionOption,
ZodLiteral,
ZodError,
ZodEnum,
ZodOptional,
ZodNullable,
ZodDiscriminatedUnion,
ZodEffects,
} from "zod";
import { MobxFatalError } from "./errors";
import { FormMeta, SafeDecodeResult } from "./FormMeta";
import { getPathId } from "./js-utils";
import {
MobxZodArrayFieldImpl,
MobxZodBaseFieldImpl,
MobxZodDiscriminatedUnionFieldImpl,
MobxZodObjectFieldImpl,
MobxZodOmittableFieldImpl,
} from "./MobxZodFieldImpl";
import { MobxZodForm, InputSetActionOptions } from "./MobxZodForm";
import { Expand, IdxOf, IsAny } from "./type-utils";
import type {
MobxOmittableTypes,
MobxZodArray,
MobxZodDiscriminatedUnion,
MobxZodEffects,
MobxZodLiteral,
MobxZodObject,
MobxZodTypes,
} from "./types";
import { DiscriminatorType, MobxZodBox } from "./zod-extra";
export interface MobxZodField<T extends ZodTypeAny> {
/**
* The Zod type associated with the field
*/
type: T;
/**
* The optional HTMLElement associated with the field.
* Typically set in frontend framework.
*/
element: HTMLElement | null;
/**
* An id uniquely identifies the field across the form.
* Useful for SSR and tracking array field elements.
*/
readonly uniqueId: string;
/**
* See `FormMeta`.
*/
readonly formMeta: FormMeta;
/**
* The form containing the field.
*/
readonly form: MobxZodForm<ZodTypeAny>;
/**
* The path of the field. Only contains numbers for array indices and strings for object keys.
* For example, the first field `a` in `items` in `{ items: [{ a: 'text' }] }` would be `['items', 0, 'a']`
*/
path: ParsePath;
/**
* The original input, e.g. from DOM string.
*/
rawInput: unknown;
/**
* The input converted from rawInput, e.g. for a number field, "01234" is converted to 1234
*/
decodeResult: SafeDecodeResult<unknown, T["_input"]>;
/**
* Set the raw input, triggering the form validation
*/
setRawInput(value: unknown): void;
/**
* Set the output, encode it into the form, trigger the form validation
*/
setOutput(output: T["_output"]): void;
/**
* @internal
* When this field's value has changed involuntarily, e.g. the parent field has called 'setRawInput'
*/
_onInputChange(): void;
/**
* Issues associated with this field.
*/
issues: readonly ZodIssue[];
/**
* @internal
*/
_issues: ZodIssue[];
/**
* Whether user has interacted with the field.
*/
touched: boolean;
/**
* @internal
*/
_touched: boolean;
/**
* Manually set whether the user has interacted with the field.
* @param touched
* @returns
*/
setTouched: (touched: boolean) => void;
/**
* Error messages associated with the field.
*/
errorMessages: readonly string[];
/**
* @internal
*/
_errorMessages: string[];
/**
* @internal
*/
_updatePath: (newPath: ParsePath) => void;
/**
* @internal
*/
_walk: (handler: (f: MobxZodField<any>) => void) => void;
/**
* Extra error messages associated with the field,
* i.e. errors not from the schema.
* Extra error messages are merely for the user's infomration,
* and does not block submission by default.
* Extra errror messages are removed from the field once the input changes or the form is submitted.
*/
extraErrorMessages: readonly string[];
/**
* @internal
*/
_extraErrorMessages: string[];
setExtraErrorMessages: (e: string[]) => void;
}
export type FieldWithEffects<
F extends MobxZodField<ZodTypeAny>,
E extends MobxZodEffects,
> = Expand<Omit<F, "effects"> & { effects: E }>;
export type MapZodTypeToField<T extends MobxZodTypes> = true extends IsAny<T>
? MobxZodField<T>
: T extends
| ZodString
| ZodNumber
| ZodBoolean
| ZodEnum<[string, ...string[]]>
| MobxZodLiteral
? MobxZodField<T>
: T extends ZodOptional<ZodTypeAny>
? MobxZodOptionalField<T>
: T extends ZodNullable<ZodTypeAny>
? MobxZodNullableField<T>
: T extends MobxZodObject
? MobxZodObjectField<T>
: T extends MobxZodArray
? MobxZodArrayField<T>
: T extends MobxZodDiscriminatedUnion
? MobxZodDiscriminatedUnionField<T>
: T extends MobxZodEffects
? FieldWithEffects<MapZodTypeToField<T["_def"]["schema"]>, T>
: T extends MobxZodBox<ZodTypeAny>
? MobxZodField<T>
: never;
export type MobxZodObjectFieldFields<T extends MobxZodObject> = {
[K in keyof T["shape"]]: MapZodTypeToField<T["shape"][K]>;
};
export interface MobxZodObjectField<T extends MobxZodObject>
extends MobxZodField<T> {
/**
* The child fields of this field, matching the object's shape.
*/
fields: MobxZodObjectFieldFields<T>;
}
export interface MobxZodArrayField<T extends MobxZodArray>
extends MobxZodField<T> {
_elementOutput: T["element"]["_output"];
/**
* The child fields of this field.
*/
elements: readonly MapZodTypeToField<T["element"]>[];
/**
* The field array's length.
*/
length: number;
/**
* Array.pop for the field array.
*/
pop(): unknown;
/**
* Array.push for the field array.
*/
push(...items: this["_elementOutput"][]): number;
/**
* Array.shift for the field array
*/
shift(): unknown;
// TODO: sort the fields in place
/**
* Array.splice for the field array. See MDN for more.
* @param start
* @param deleteCount
* @param values
* @param options
* @returns
*/
splice: (
start: number,
deleteCount: number,
values: this["_elementOutput"][],
options?: InputSetActionOptions,
) => void;
}
// Omit the discriminator field because it is special in forms
export type MobxZodDiscriminatedUnionFieldFieldsSuccess<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<string>[],
> = {
[K in IdxOf<Options>]: Options[K] extends MobxZodObject
? Omit<MobxZodObjectField<Options[K]>["fields"], Discriminator> & {
discriminator: Options[K]["shape"][Discriminator]["_output"];
}
: never;
}[IdxOf<Options>];
export type MobxZodDiscriminatedUnionFieldsResult<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<string>[],
> =
| {
success: false;
error: ZodError<unknown>;
}
| {
success: true;
fields: MobxZodDiscriminatedUnionFieldFieldsSuccess<
Discriminator,
Options
>;
};
/**
* Various computed types for DiscriminatedUnion
*/
export interface MobxZodDiscriminatedUnionFieldTypes<
T extends MobxZodDiscriminatedUnion,
> {
_discriminator: T["discriminator"];
_discriminatorType: DiscriminatorType<T>;
_discriminatorInput: this["_discriminatorType"]["_input"];
_discriminatorOutput: this["_discriminatorType"]["_output"];
_discriminatorParsedResult: SafeParseReturnType<
this["_discriminatorOutput"],
this["_discriminatorInput"]
>;
_fieldsResult: MobxZodDiscriminatedUnionFieldsResult<
this["_discriminator"],
T["options"]
>;
}
export interface MobxZodDiscriminatedUnionField<
T extends MobxZodDiscriminatedUnion,
> extends MobxZodField<T> {
_types: MobxZodDiscriminatedUnionFieldTypes<T>;
discriminatorField: MobxZodField<DiscriminatorType<T>>;
fieldsResult: this["_types"]["_fieldsResult"];
}
export type MobxZodOmittableFieldTypes<T extends MobxOmittableTypes> = {
_innerField: MapZodTypeToField<T["_def"]["innerType"]>;
};
export interface MobxZodOmittableField<T extends MobxOmittableTypes>
extends MobxZodField<T> {
_types: MobxZodOmittableFieldTypes<T>;
/**
* If the input is null or undefined, innerField will be undefined;
* else, it will be the field that its inner type is mapped to.
* For ZodString and ZodNumber inner type,
* it is always not undefined,
* because null or undefined will be encoded to empty string.
*/
innerField: this["_types"]["_innerField"] | undefined;
}
export interface MobxZodOptionalField<T extends MobxOmittableTypes>
extends MobxZodOmittableField<T> {}
export interface MobxZodNullableField<T extends MobxOmittableTypes>
extends MobxZodOmittableField<T> {}
export const createFieldForType = <T extends MobxZodTypes>(
type: T,
form: MobxZodForm<any>,
path: ParsePath,
): MapZodTypeToField<T> => {
if (
type instanceof ZodString ||
type instanceof ZodNumber ||
type instanceof ZodBoolean ||
type instanceof ZodEnum ||
type instanceof ZodLiteral
) {
return new MobxZodBaseFieldImpl<typeof type>(type, form, path) as any;
} else if (type instanceof ZodOptional || type instanceof ZodNullable) {
return new MobxZodOmittableFieldImpl<typeof type>(type, form, path) as any;
} else if (type instanceof ZodObject) {
return new MobxZodObjectFieldImpl<typeof type>(type, form, path) as any;
} else if (type instanceof ZodArray) {
return new MobxZodArrayFieldImpl<typeof type>(type, form, path) as any;
} else if (type instanceof ZodDiscriminatedUnion) {
return new MobxZodDiscriminatedUnionFieldImpl(type, form, path) as any;
} else if (type instanceof ZodEffects) {
// Where TypeScript breaks down
const field = createFieldForType<ZodTypeAny>(
type.sourceType(),
form,
path,
) as any;
field.effects = type;
return field;
} else if (type instanceof MobxZodBox) {
return new MobxZodBaseFieldImpl<typeof type>(type, form, path) as any;
}
throw new MobxFatalError(
`type ${
type.constructor.name
} is not handled. Check the type at ${JSON.stringify(
getPathId(path),
)}. For a list of supported Zod types, see TODO`,
);
};