forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.test.ts
213 lines (179 loc) · 4.79 KB
/
function.test.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
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import { util } from "../helpers/util";
import * as z from "../index";
const args1 = z.tuple([z.string()]);
const returns1 = z.number();
const func1 = z.function(args1, returns1);
test("function parsing", () => {
const parsed = func1.parse((arg: any) => arg.length);
parsed("asdf");
});
test("parsed function fail 1", () => {
const parsed = func1.parse((x: string) => x);
expect(() => parsed("asdf")).toThrow();
});
test("parsed function fail 2", () => {
const parsed = func1.parse((x: string) => x);
expect(() => parsed(13 as any)).toThrow();
});
test("function inference 1", () => {
type func1 = z.TypeOf<typeof func1>;
const t1: util.AssertEqual<func1, (k: string) => number> = true;
[t1];
});
test("args method", () => {
const t1 = z.function();
type t1 = z.infer<typeof t1>;
const f1: util.AssertEqual<t1, () => void> = true;
const t2 = t1.args(z.string());
type t2 = z.infer<typeof t2>;
const f2: util.AssertEqual<t2, (arg: string) => void> = true;
const t3 = t2.returns(z.boolean());
type t3 = z.infer<typeof t3>;
const f3: util.AssertEqual<t3, (arg: string) => boolean> = true;
f1;
f2;
f3;
});
const args2 = z.tuple([
z.object({
f1: z.number(),
f2: z.string().nullable(),
f3: z.array(z.boolean().optional()).optional(),
}),
]);
const returns2 = z.union([z.string(), z.number()]);
const func2 = z.function(args2, returns2);
test("function inference 2", () => {
type func2 = z.TypeOf<typeof func2>;
const t2: util.AssertEqual<
func2,
(arg: {
f1: number;
f2: string | null;
f3?: (boolean | undefined)[] | undefined;
}) => string | number
> = true;
[t2];
});
test("valid function run", () => {
const validFunc2Instance = func2.validate((_x) => {
return "adf" as any;
});
const checker = () => {
validFunc2Instance({
f1: 21,
f2: "asdf",
f3: [true, false],
});
};
checker();
});
test("input validation error", () => {
const invalidFuncInstance = func2.validate((_x) => {
return "adf" as any;
});
const checker = () => {
invalidFuncInstance("Invalid_input" as any);
};
expect(checker).toThrow();
});
test("output validation error", () => {
const invalidFuncInstance = func2.validate((_x) => {
return ["this", "is", "not", "valid", "output"] as any;
});
const checker = () => {
invalidFuncInstance({
f1: 21,
f2: "asdf",
f3: [true, false],
});
};
expect(checker).toThrow();
});
test("special function error codes", () => {
const checker = z
.function(z.tuple([z.string()]), z.boolean())
.implement((arg) => {
return arg.length as any;
});
try {
checker("12" as any);
} catch (err) {
const zerr = err as z.ZodError;
const first = zerr.issues[0];
if (first.code !== z.ZodIssueCode.invalid_return_type) throw new Error();
expect(first.returnTypeError).toBeInstanceOf(z.ZodError);
}
try {
checker(12 as any);
} catch (err) {
const zerr = err as z.ZodError;
const first = zerr.issues[0];
if (first.code !== z.ZodIssueCode.invalid_arguments) throw new Error();
expect(first.argumentsError).toBeInstanceOf(z.ZodError);
}
});
test("function with async refinements", async () => {
const func = z
.function()
.args(z.string().refine(async (val) => val.length > 10))
.returns(z.promise(z.number().refine(async (val) => val > 10)))
.implement(async (val) => {
return val.length;
});
const results = [];
try {
await func("asdfasdf");
results.push("success");
} catch (err) {
results.push("fail");
}
try {
await func("asdflkjasdflkjsf");
results.push("success");
} catch (err) {
results.push("fail");
}
expect(results).toEqual(["fail", "success"]);
});
test("non async function with async refinements should fail", async () => {
const func = z
.function()
.args(z.string().refine(async (val) => val.length > 10))
.returns(z.number().refine(async (val) => val > 10))
.implement((val) => {
return val.length;
});
const results = [];
try {
await func("asdasdfasdffasdf");
results.push("success");
} catch (err) {
results.push("fail");
}
expect(results).toEqual(["fail"]);
});
test("allow extra parameters", () => {
const maxLength5 = z
.function()
.args(z.string())
.returns(z.boolean())
.implement((str, _arg, _qewr) => {
return str.length <= 5;
});
const filteredList = [
"apple",
"orange",
"pear",
"banana",
"strawberry",
].filter(maxLength5);
expect(filteredList.length).toEqual(2);
});
test("params and returnType getters", () => {
const func = z.function().args(z.string()).returns(z.string());
func.parameters().items[0].parse("asdf");
func.returnType().parse("asdf");
});