-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv4.ts
More file actions
486 lines (378 loc) · 11.7 KB
/
Copy pathv4.ts
File metadata and controls
486 lines (378 loc) · 11.7 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import { z } from 'zod/v4';
import cloneDeep from 'lodash/cloneDeep';
import isBoolean from 'lodash/isBoolean';
import isNil from 'lodash/isNil';
import isNumber from 'lodash/isNumber';
import isPlainObject from 'lodash/isPlainObject';
import isString from 'lodash/isString';
import isUndefined from 'lodash/isUndefined';
type DeepPartial<T> = T extends (infer U)[] ? DeepPartial<U>[] : T extends object ? { [P in keyof T]?: DeepPartial<T[P]> } : T;
const cast = (schema: any): z.ZodType => {
return schema as z.ZodType;
};
const defaultInstance = <T extends z.ZodType>(schema: T, source?: DeepPartial<z.input<T>>): z.output<T> => {
const getDefaultValue = (schema: z.ZodType, defaultValue?: any): any => {
if (schema instanceof z.ZodDefault) {
const d = schema._zod.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.ZodBigInt) {
if (schema._zod.bag?.minimum && schema._zod.bag?.minimum > 0) {
return BigInt(schema._zod.bag?.minimum);
}
return BigInt(0);
}
if (schema instanceof z.ZodBoolean) {
return false;
}
if (schema instanceof z.ZodCodec) {
return defaultInstance(cast(schema._zod.def.out));
}
if (schema instanceof z.ZodDate) {
return null;
}
if (schema instanceof z.ZodDiscriminatedUnion) {
const firstOption = schema._zod.def.options[0];
return defaultInstance(cast(firstOption));
}
if (schema instanceof z.ZodEnum) {
return schema._zod.values.values().next().value;
}
if (schema instanceof z.ZodIntersection) {
return defaultInstance(cast(schema._zod.def.left), source);
}
if (schema instanceof z.ZodLiteral) {
return schema._zod.values.values().next().value;
}
if (schema instanceof z.ZodMap) {
return new Map();
}
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) {
if (schema._zod.bag?.minimum && schema._zod.bag?.minimum > 0) {
return schema._zod.bag?.minimum;
}
return 0;
}
if (schema instanceof z.ZodObject) {
return defaultInstance(schema, {});
}
if (schema instanceof z.ZodOptional) {
return undefined;
}
if (schema instanceof z.ZodPipe) {
if (schema._zod.def.out instanceof z.ZodTransform) {
const defaultInValue = getDefaultValue(cast(schema._zod.def.in));
return getDefaultValue(cast(schema._zod.def.out), defaultInValue);
}
return getDefaultValue(cast(schema._zod.def.out));
}
if (schema instanceof z.ZodPromise) {
return Promise.resolve(getDefaultValue(cast(schema._zod.def.innerType)));
}
if (schema instanceof z.ZodRecord) {
return {};
}
if (schema instanceof z.ZodSet) {
return new Set();
}
if (
schema instanceof z.ZodBase64 ||
schema instanceof z.ZodBase64URL ||
schema instanceof z.ZodCIDRv4 ||
schema instanceof z.ZodCIDRv6 ||
schema instanceof z.ZodCUID ||
schema instanceof z.ZodCUID2 ||
schema instanceof z.ZodCustomStringFormat ||
schema instanceof z.ZodE164 ||
schema instanceof z.ZodEmail ||
schema instanceof z.ZodEmoji ||
schema instanceof z.ZodGUID ||
schema instanceof z.ZodIPv4 ||
schema instanceof z.ZodIPv6 ||
schema instanceof z.ZodISODate ||
schema instanceof z.ZodISODateTime ||
schema instanceof z.ZodISODuration ||
schema instanceof z.ZodISOTime ||
schema instanceof z.ZodJWT ||
schema instanceof z.ZodKSUID ||
schema instanceof z.ZodNanoID ||
schema instanceof z.ZodString ||
schema instanceof z.ZodULID ||
schema instanceof z.ZodURL ||
schema instanceof z.ZodUUID ||
schema instanceof z.ZodXID
) {
return '';
}
if (schema instanceof z.ZodSymbol) {
return '';
}
if (schema instanceof z.ZodTransform) {
try {
return schema._zod.def.transform(defaultValue, {
value: defaultValue,
issues: []
});
} catch {
return defaultValue;
}
}
if (schema instanceof z.ZodTuple) {
return [];
}
if (schema instanceof z.ZodUnion) {
return getDefaultValue(cast(schema._zod.def.options[0]));
}
if (schema instanceof z.ZodUnknown) {
return undefined;
}
if (schema instanceof z.ZodUndefined) {
return undefined;
}
if (schema instanceof z.ZodVoid) {
return undefined;
}
if ('innerType' in schema._zod?.def && schema._zod?.def.innerType) {
return getDefaultValue(cast(schema._zod.def.innerType));
}
return undefined;
};
const processArray = (schema: z.ZodArray<any>, source: any[]): any[] => {
if (!Array.isArray(source)) {
return [];
}
const elementSchema = schema._zod.def.element;
return source.map(item => {
return processValue(elementSchema, item);
});
};
const processDiscriminatedUnion = (schema: z.ZodDiscriminatedUnion, source: any): any => {
if (!isPlainObject(source) || isNil(source)) {
return getDefaultValue(schema);
}
const discriminator = schema._zod.def.discriminator;
const discriminatorValue = source[discriminator];
// Find matching option based on discriminator value
const matchingSchema = schema._zod.def.options.find(option => {
const { propValues } = option._zod;
if (propValues && propValues[discriminator]) {
return propValues[discriminator].has(discriminatorValue);
}
return false;
});
if (!matchingSchema) {
const firstOption = schema._zod.def.options[0];
// If no matching schema is found, use the first option as default
return processObject(firstOption as z.ZodObject, source);
}
return processObject(matchingSchema as z.ZodObject, source);
};
const processValue = (schema: z.ZodType, value: any): any => {
if (schema instanceof z.ZodArray) {
return processArray(schema, value);
}
if (schema instanceof z.ZodBigInt) {
if (typeof value === 'bigint') {
return value;
}
return typeof value === 'number' ? BigInt(value) : BigInt(0);
}
if (schema instanceof z.ZodBoolean) {
return isBoolean(value) ? value : false;
}
if (schema instanceof z.ZodCodec) {
return schema.decode(value);
}
if (schema instanceof z.ZodDiscriminatedUnion) {
return processDiscriminatedUnion(schema, value);
}
if (schema instanceof z.ZodDefault) {
const inner = cast(schema._zod.def.innerType);
if (isUndefined(value)) {
return processValue(inner, getDefaultValue(schema));
}
// A present-but-invalid enum/literal value degrades to the DECLARED
// default — the schema author's chosen value — never the inner
// type's first member (which stays the fallback only when no
// .default() exists). This keeps "key absent" and "value corrupted"
// converging on the same repaired value.
if ((inner instanceof z.ZodEnum || inner instanceof z.ZodLiteral) && !inner._zod.values.has(value)) {
return getDefaultValue(schema);
}
return processValue(inner, value);
}
if (schema instanceof z.ZodEnum) {
return schema._zod.values.has(value) ? value : getDefaultValue(schema);
}
if (schema instanceof z.ZodISODate) {
return isString(value) ? value : new Date().toISOString().split('T')[0];
}
if (schema instanceof z.ZodISODateTime) {
return isString(value) ? value : new Date().toISOString();
}
if (schema instanceof z.ZodISODuration) {
return isString(value) ? value : 'P0D';
}
if (schema instanceof z.ZodISOTime) {
return isString(value) ? value : new Date().toISOString().split('T')[1];
}
if (schema instanceof z.ZodLiteral) {
return schema._zod.values.has(value) ? value : getDefaultValue(schema);
}
if (schema instanceof z.ZodMap) {
return processMap(schema, value);
}
if (schema instanceof z.ZodNullable) {
return isNil(value) ? null : processValue(schema._zod.def.innerType as any, value);
}
if (schema instanceof z.ZodNumber) {
return isNumber(value) ? value : (schema._zod.bag?.minimum ?? 0);
}
if (schema instanceof z.ZodObject) {
return processObject(schema, value);
}
if (schema instanceof z.ZodOptional) {
return isNil(value) ? undefined : processValue(schema._zod.def.innerType as any, value);
}
if (schema instanceof z.ZodPipe) {
if (schema._zod.def.out instanceof z.ZodTransform) {
return processTransform(schema._zod.def.out, value);
}
return processValue(cast(schema._zod.def.in), 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 isString(value) ? value : '';
}
if (schema instanceof z.ZodTransform) {
return processTransform(schema, 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._zod.def.valueType;
const result: any = new Map();
source.forEach((value, key) => {
result.set(key, processValue(cast(valueSchema), value));
});
return result;
};
const processObject = (schema: z.ZodObject<any>, source: any): any => {
const result: any = {};
const shape = schema._zod.def.shape;
// Process defined schema properties
for (const [key, fieldSchema] of Object.entries(shape)) {
// Create a proper ZodType from the field schema
const zodFieldSchema = cast(fieldSchema);
if (source && key in source) {
const v = processValue(zodFieldSchema, source[key]);
if (v !== undefined) {
result[key] = v;
}
} else {
const v = getDefaultValue(zodFieldSchema);
if (v !== undefined) {
result[key] = v;
}
}
}
// Handle catchall - preserve additional properties not in schema
if (schema._zod.def.catchall && 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 (!isPlainObject(source) || isNil(source)) {
return {};
}
const valueSchema = schema._zod.def.valueType;
const result: any = {};
for (const key in source) {
result[key] = processValue(cast(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._zod.def.valueType;
const result: any = new Set();
for (const value of source) {
result.add(processValue(cast(valueSchema), value));
}
return result;
};
const processTransform = (schema: z.ZodTransform<any, any>, source: any): any => {
try {
return schema._zod.def.transform(source, {
value: source,
issues: []
});
} catch {
return source;
}
};
const processUnion = (schema: z.ZodUnion<any>, source: any): any => {
for (const optionSchema of schema._zod.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._zod.def.options[0]);
};
if (schema instanceof z.ZodDiscriminatedUnion) {
return processDiscriminatedUnion(schema, source);
}
if (schema instanceof z.ZodPipe) {
if (schema._zod.def.out instanceof z.ZodTransform) {
return processTransform(schema._zod.def.out, source);
}
return processValue(cast(schema._zod.def.in), source);
}
return processValue(schema, source);
};
export default defaultInstance;