forked from colinhacks/zod
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuple.test.ts
100 lines (86 loc) · 2.61 KB
/
tuple.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
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import { util } from "../helpers/util";
import * as z from "../index";
import { ZodError } from "../ZodError";
const testTuple = z.tuple([
z.string(),
z.object({ name: z.literal("Rudy") }),
z.array(z.literal("blue")),
]);
const testData = ["asdf", { name: "Rudy" }, ["blue"]];
const badData = [123, { name: "Rudy2" }, ["blue", "red"]];
test("tuple inference", () => {
const args1 = z.tuple([z.string()]);
const returns1 = z.number();
const func1 = z.function(args1, returns1);
type func1 = z.TypeOf<typeof func1>;
const t1: util.AssertEqual<func1, (k: string) => number> = true;
[t1];
});
test("successful validation", () => {
const val = testTuple.parse(testData);
expect(val).toEqual(["asdf", { name: "Rudy" }, ["blue"]]);
});
test("successful async validation", async () => {
const val = await testTuple.parseAsync(testData);
return expect(val).toEqual(testData);
});
test("failed validation", () => {
const checker = () => {
testTuple.parse([123, { name: "Rudy2" }, ["blue", "red"]] as any);
};
try {
checker();
} catch (err) {
if (err instanceof ZodError) {
expect(err.issues.length).toEqual(3);
}
}
});
test("failed async validation", async () => {
const res = await testTuple.safeParse(badData);
expect(res.success).toEqual(false);
if (!res.success) {
expect(res.error.issues.length).toEqual(3);
}
// try {
// checker();
// } catch (err) {
// if (err instanceof ZodError) {
// expect(err.issues.length).toEqual(3);
// }
// }
});
test("tuple with transformers", () => {
const stringToNumber = z.string().transform((val) => val.length);
const val = z.tuple([stringToNumber]);
type t1 = z.input<typeof val>;
const f1: util.AssertEqual<t1, [string]> = true;
type t2 = z.output<typeof val>;
const f2: util.AssertEqual<t2, [number]> = true;
expect(val.parse(["1234"])).toEqual([4]);
f1;
f2;
});
test("tuple with rest schema", () => {
const myTuple = z.tuple([z.string(), z.number()]).rest(z.boolean());
expect(myTuple.parse(["asdf", 1234, true, false, true])).toEqual([
"asdf",
1234,
true,
false,
true,
]);
expect(myTuple.parse(["asdf", 1234])).toEqual(["asdf", 1234]);
expect(() => myTuple.parse(["asdf", 1234, "asdf"])).toThrow();
type t1 = z.output<typeof myTuple>;
const f1: util.AssertEqual<t1, [string, number, ...boolean[]]> = true;
f1;
});
// test('tuple with optional elements', () => {
// const result = z
// .tuple([z.string(), z.number().optional()])
// .safeParse(['asdf']);
// expect(result).toEqual(['asdf']);
// });