forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord.test.ts
137 lines (115 loc) · 2.87 KB
/
record.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
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import { util } from "../helpers/util";
import * as z from "../index";
const booleanRecord = z.record(z.boolean());
type booleanRecord = z.infer<typeof booleanRecord>;
const recordWithEnumKeys = z.record(z.enum(["Tuna", "Salmon"]), z.string());
type recordWithEnumKeys = z.infer<typeof recordWithEnumKeys>;
const recordWithLiteralKeys = z.record(
z.union([z.literal("Tuna"), z.literal("Salmon")]),
z.string()
);
type recordWithLiteralKeys = z.infer<typeof recordWithLiteralKeys>;
test("type inference", () => {
const f1: util.AssertEqual<booleanRecord, Record<string, boolean>> = true;
f1;
const f2: util.AssertEqual<
recordWithEnumKeys,
Partial<Record<"Tuna" | "Salmon", string>>
> = true;
f2;
const f3: util.AssertEqual<
recordWithLiteralKeys,
Partial<Record<"Tuna" | "Salmon", string>>
> = true;
f3;
});
test("methods", () => {
booleanRecord.optional();
booleanRecord.nullable();
});
test("string record parse - pass", () => {
booleanRecord.parse({
k1: true,
k2: false,
1234: false,
});
});
test("string record parse - fail", () => {
const badCheck = () =>
booleanRecord.parse({
asdf: 1234,
} as any);
expect(badCheck).toThrow();
expect(() => booleanRecord.parse("asdf")).toThrow();
});
test("string record parse - fail", () => {
const badCheck = () =>
booleanRecord.parse({
asdf: {},
} as any);
expect(badCheck).toThrow();
});
test("string record parse - fail", () => {
const badCheck = () =>
booleanRecord.parse({
asdf: [],
} as any);
expect(badCheck).toThrow();
});
test("key schema", () => {
const result1 = recordWithEnumKeys.parse({
Tuna: "asdf",
Salmon: "asdf",
});
expect(result1).toEqual({
Tuna: "asdf",
Salmon: "asdf",
});
const result2 = recordWithLiteralKeys.parse({
Tuna: "asdf",
Salmon: "asdf",
});
expect(result2).toEqual({
Tuna: "asdf",
Salmon: "asdf",
});
// shouldn't require us to specify all props in record
const result3 = recordWithEnumKeys.parse({
Tuna: "abcd",
});
expect(result3).toEqual({
Tuna: "abcd",
});
// shouldn't require us to specify all props in record
const result4 = recordWithLiteralKeys.parse({
Salmon: "abcd",
});
expect(result4).toEqual({
Salmon: "abcd",
});
expect(() =>
recordWithEnumKeys.parse({
Tuna: "asdf",
Salmon: "asdf",
Trout: "asdf",
})
).toThrow();
expect(() =>
recordWithLiteralKeys.parse({
Tuna: "asdf",
Salmon: "asdf",
Trout: "asdf",
})
).toThrow();
});
// test("record element", () => {
// expect(booleanRecord.element).toBeInstanceOf(z.ZodBoolean);
// });
test("key and value getters", () => {
const rec = z.record(z.string(), z.number());
rec.keySchema.parse("asdf");
rec.valueSchema.parse(1234);
rec.element.parse(1234);
});