-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
140 lines (137 loc) · 3.9 KB
/
index.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
import clone from "just-clone";
import type { ZodTypeAny, input, output } from "zod";
export function init<T extends ZodTypeAny>(schema: T): output<T> {
const def = schema._def;
if (schema.isNullable() && def.typeName !== "ZodDefault") {
return null;
}
switch (def.typeName) {
case "ZodObject": {
const outputObject: Record<string, unknown> = {};
for (const [key, value] of Object.entries(def.shape())) {
outputObject[key] = init(value as ZodTypeAny);
}
return outputObject;
}
case "ZodRecord":
return {};
case "ZodString": {
if (def.checks) {
for (const check of def.checks) {
if (check.kind === "uuid") {
return crypto.randomUUID();
}
}
}
return "";
}
case "ZodNumber":
for (const check of def.checks || []) {
if (["min", "max"].includes(check.kind)) {
return check.value;
}
}
return 0;
case "ZodBigInt":
return 0;
case "ZodBoolean":
return false;
case "ZodDate":
return new Date();
case "ZodLiteral":
return def.value;
case "ZodEffects":
return init(def.schema);
case "ZodArray":
return [];
case "ZodTuple":
return def.items.map((item: ZodTypeAny) => init(item));
case "ZodSet":
return new Set();
case "ZodMap":
return new Map();
case "ZodEnum":
return def.values[0];
case "ZodNativeEnum":
// ref. https://github.com/colinhacks/zod/blob/6fe152f98a434a087c0f1ecbce5c52427bd816d3/src/helpers/util.ts#L28-L43
return Object.values(def.values).filter(
(value) => typeof def.values[value as any] !== "number",
)[0];
case "ZodUnion":
return init(def.options[0]);
case "ZodDiscriminatedUnion":
return init(Array.from(def.options.values() as any[])[0]);
case "ZodIntersection":
return Object.assign(init(def.left) as any, init(def.right));
case "ZodFunction":
return (..._: any[]) => init(def.returns);
case "ZodLazy":
return init(def.getter());
case "ZodPipeline":
return init(def.in);
case "ZodDefault":
return def.innerType._def.typeName === "ZodFunction"
? def.defaultValue()
: clone(def.defaultValue());
case "ZodNaN":
return Number.NaN;
case "ZodNull":
case "ZodAny":
return null;
case "ZodOptional":
return init(def.innerType);
// case "ZodUndefined":
// case "ZodVoid":
// case "ZodUnknown":
// case "ZodNever":
default:
return undefined;
}
}
export function empty<T extends ZodTypeAny>(schema: T): input<T> {
const def = schema._def;
switch (def.typeName) {
case "ZodObject": {
const outputObject: Record<string, unknown> = {};
for (const [key, value] of Object.entries(def.shape())) {
outputObject[key] = empty(value as ZodTypeAny);
}
return outputObject;
}
case "ZodRecord":
return {};
case "ZodArray":
return [];
case "ZodTuple":
return def.items.map((item: ZodTypeAny) => empty(item));
case "ZodSet":
return new Set();
case "ZodMap":
return new Map();
case "ZodUnion":
return empty(def.options[0]);
case "ZodDiscriminatedUnion":
return empty(Array.from(def.options.values() as any[])[0]);
case "ZodIntersection":
return Object.assign(empty(def.left) as any, empty(def.right));
case "ZodLazy":
return empty(def.getter());
case "ZodPipeline":
return empty(def.in);
case "ZodNullable":
case "ZodOptional":
return empty(def.innerType);
case "ZodEffects":
return empty(def.schema);
case "ZodLiteral":
return def.value;
case "ZodNaN":
return Number.NaN;
case "ZodDefault":
return def.innerType._def.typeName === "ZodFunction"
? def.defaultValue()
: clone(def.defaultValue());
default:
return null;
}
}