-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv3.ts
More file actions
379 lines (292 loc) · 8.27 KB
/
Copy pathv3.ts
File metadata and controls
379 lines (292 loc) · 8.27 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import cloneDeep from 'lodash/cloneDeep';
import isPlainObject from 'lodash/isPlainObject';
import { z } from 'zod/v3';
type DeepPartial<T> = T extends (infer U)[] ? DeepPartial<U>[] : T extends object ? { [P in keyof T]?: DeepPartial<T[P]> } : T;
const defaultInstance = <T extends z.ZodSchema>(
schema: T,
source: DeepPartial<z.input<T>> = {} as DeepPartial<z.input<T>>
): z.output<T> => {
const getDefaultValue = (schema: z.ZodTypeAny): any => {
if (schema instanceof z.ZodDefault) {
const d = schema._def.defaultValue();
if (isPlainObject(d)) {
return cloneDeep(d);
}
return d;
}
if (schema instanceof z.ZodAny) {
return undefined;
}
if (schema instanceof z.ZodArray) {
return [];
}
if (schema instanceof z.ZodBoolean) {
return false;
}
if (schema instanceof z.ZodDate) {
return null;
}
if (schema instanceof z.ZodDiscriminatedUnion) {
return defaultInstance(schema.options[0]);
}
if (schema instanceof z.ZodEffects) {
const innerValue = getDefaultValue(schema.innerType());
return processEffect(schema, innerValue);
}
if (schema instanceof z.ZodEnum) {
return schema.options[0];
}
if (schema instanceof z.ZodFunction) {
return () => null;
}
if (schema instanceof z.ZodIntersection) {
return defaultInstance(schema._def.left, source);
}
if (schema instanceof z.ZodLiteral) {
return schema.value;
}
if (schema instanceof z.ZodMap) {
return new Map();
}
if (schema instanceof z.ZodNativeEnum) {
const keys = Object.keys(schema._def.values);
return schema._def.values[keys[0]];
}
if (schema instanceof z.ZodNaN) {
return NaN;
}
if (schema instanceof z.ZodNever) {
return undefined;
}
if (schema instanceof z.ZodNull) {
return null;
}
if (schema instanceof z.ZodNullable) {
return null;
}
if (schema instanceof z.ZodNumber || schema instanceof z.ZodBigInt) {
return schema.minValue ?? 0;
}
if (schema instanceof z.ZodObject) {
return defaultInstance(schema, {});
}
if (schema instanceof z.ZodPipeline) {
if (!('out' in schema._def)) {
return undefined;
}
return getDefaultValue(schema._def.out);
}
if (schema instanceof z.ZodPromise) {
return Promise.resolve(getDefaultValue(schema.unwrap()));
}
if (schema instanceof z.ZodRecord) {
return {};
}
if (schema instanceof z.ZodSet) {
return new Set();
}
if (schema instanceof z.ZodString) {
return '';
}
if (schema instanceof z.ZodSymbol) {
return '';
}
if (schema instanceof z.ZodTuple) {
return [];
}
if (schema instanceof z.ZodUnion) {
return getDefaultValue(schema.options[0]);
}
if (schema instanceof z.ZodUnknown) {
return undefined;
}
if (schema instanceof z.ZodUndefined) {
return undefined;
}
if (schema instanceof z.ZodVoid) {
return undefined;
}
if (schema._def && schema._def.innerType) {
return getDefaultValue(schema._def.innerType);
}
return undefined;
};
const processArray = (schema: z.ZodArray<any>, source: any[]): any[] => {
if (!Array.isArray(source)) {
return [];
}
const elementSchema = schema.element;
return source.map(item => {
return processValue(elementSchema, item);
});
};
const processDiscriminatedUnion = (schema: z.ZodDiscriminatedUnion<string, z.ZodObject<any>[]>, source: any): any => {
if (typeof source !== 'object' || source === null) {
return getDefaultValue(schema);
}
const discriminator = schema.discriminator;
const discriminatorValue = source[discriminator];
const matchingSchema = schema.options.find(option => {
return option.shape[discriminator] instanceof z.ZodLiteral && option.shape[discriminator].value === discriminatorValue;
});
if (!matchingSchema) {
// If no matching schema is found, use the first option as default
return processObject(schema.options[0], source);
}
return processObject(matchingSchema, source);
};
const processValue = (schema: z.ZodTypeAny, value: any): any => {
if (schema instanceof z.ZodArray) {
return processArray(schema, value);
}
if (schema instanceof z.ZodBoolean) {
return typeof value === 'boolean' ? value : false;
}
if (schema instanceof z.ZodDiscriminatedUnion) {
return processDiscriminatedUnion(schema, value);
}
if (schema instanceof z.ZodEffects) {
return processEffect(schema, value);
}
if (schema instanceof z.ZodDefault) {
return processValue(schema._def.innerType, value);
}
if (schema instanceof z.ZodMap) {
return processMap(schema, value);
}
if (schema instanceof z.ZodNullable) {
return value === null ? null : processValue(schema.unwrap(), value);
}
if (schema instanceof z.ZodNumber || schema instanceof z.ZodBigInt) {
return typeof value === 'number' ? value : (schema.minValue ?? 0);
}
if (schema instanceof z.ZodObject) {
return processObject(schema, value);
}
if (schema instanceof z.ZodRecord) {
return processRecord(schema, value);
}
if (schema instanceof z.ZodSet) {
return processSet(schema, value);
}
if (schema instanceof z.ZodString) {
return typeof value === 'string' ? value : '';
}
if (schema instanceof z.ZodUnion) {
return processUnion(schema, value);
}
return value;
};
const processMap = (schema: z.ZodMap<any, any>, source: any): any => {
if (!(source instanceof Map)) {
return new Map();
}
const valueSchema = schema._def.valueType;
const result: any = new Map();
source.forEach((value, key) => {
result.set(key, processValue(valueSchema, value));
});
return result;
};
const processObject = (schema: z.ZodObject<any>, source: any): any => {
const result: any = {};
const shape = schema.shape;
// Process defined schema properties
for (const [key, fieldSchema] of Object.entries(shape)) {
if (key in source) {
const v = processValue(fieldSchema as z.ZodTypeAny, source[key]);
if (v !== undefined) {
result[key] = v;
}
} else {
const v = getDefaultValue(fieldSchema as z.ZodTypeAny);
if (v !== undefined) {
result[key] = v;
}
}
}
// Handle passthrough - preserve additional properties not in schema
if (schema._def.unknownKeys === 'passthrough' && isPlainObject(source)) {
for (const key in source) {
if (!(key in shape)) {
result[key] = source[key];
}
}
}
return result;
};
const processRecord = (schema: z.ZodRecord<any, any>, source: any): any => {
if (typeof source !== 'object' || source === null) {
return {};
}
const valueSchema = schema._def.valueType;
const result: any = {};
for (const key in source) {
result[key] = processValue(valueSchema, source[key]);
}
return result;
};
const processSet = (schema: z.ZodSet<any>, source: Set<any>): any => {
if (!(source instanceof Set)) {
return new Set();
}
const valueSchema = schema._def.valueType;
const result: any = new Set();
for (const value of source) {
result.add(processValue(valueSchema, value));
}
return result;
};
const processEffect = (schema: z.ZodEffects<any, any, any>, source: any): any => {
const { effect } = schema._def;
const ctx = {
addIssue: () => {},
get data() {
return source;
},
path: []
};
switch (effect.type) {
case 'preprocess':
if ('transform' in effect) {
try {
const preprocessed = effect.transform(source, ctx);
return processValue(schema.innerType(), preprocessed);
} catch {
return source;
}
}
case 'transform':
if ('transform' in effect) {
try {
return effect.transform(source, ctx);
} catch {
return source;
}
}
}
return source;
};
const processUnion = (schema: z.ZodUnion<[z.ZodTypeAny, ...z.ZodTypeAny[]]>, source: any): any => {
for (const optionSchema of schema._def.options) {
try {
const parsed = optionSchema.safeParse(source);
if (parsed.success) {
return processValue(optionSchema, source);
}
} catch {
// If parsing fails, try the next option
}
}
// If no option matches, return the default value of the first option
return getDefaultValue(schema._def.options[0]);
};
if (schema instanceof z.ZodDiscriminatedUnion) {
return processDiscriminatedUnion(schema, source);
}
if (schema instanceof z.ZodEffects) {
schema = schema.innerType();
}
return processValue(schema, source);
};
export default defaultInstance;