-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconvert.test-d.ts
148 lines (127 loc) · 4.63 KB
/
convert.test-d.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
import { assertType, describe, expectTypeOf, test } from "vitest";
import { SafeParseReturnType, z } from "../index";
const stringToNumber = z.string().transform((arg) => parseFloat(arg));
const numberToString = z.number().transform((n) => String(n));
const asyncNumberToString = z.number().transform(async (n) => String(n));
const asyncStringToNumber = z.string().transform(async (n) => parseFloat(n));
describe("convert", () => {
test("input and output types to be enforced in", () => {
expectTypeOf(stringToNumber.convert).toBeFunction();
expectTypeOf(stringToNumber.convert).parameter(0).toMatchTypeOf<string>();
expectTypeOf(stringToNumber.convert).returns.toMatchTypeOf<number>();
expectTypeOf(numberToString.convert).toBeFunction();
expectTypeOf(numberToString.convert).parameter(0).toMatchTypeOf<number>();
expectTypeOf(numberToString.convert).returns.toMatchTypeOf<string>();
});
test("valid schema conversion", () => {
const userSchema = z.object({
id: z.string(),
name: z.string(),
age: z.string().transform((age) => parseInt(age, 10)),
});
const validInput: z.input<typeof userSchema> = {
id: "123",
name: "Alice",
age: "25",
};
const user = userSchema.convert(validInput);
expectTypeOf(user).toMatchTypeOf<z.infer<typeof userSchema>>();
});
test("invalid schema conversion", () => {
const userSchema = z.object({
id: z.string(),
name: z.string(),
age: z.string().transform((age) => parseInt(age, 10)),
});
// Input not matching the schema's input type
const invalidInput = {
name: "Alice",
age: "25",
};
expectTypeOf(numberToString.convert)
.parameter(0)
.not.toMatchTypeOf<typeof invalidInput>();
try {
// @ts-expect-error - compile error
userSchema.convert(invalidInput);
} catch {}
});
});
describe("safeConvert", () => {
test("input and output types to be enforced in", () => {
expectTypeOf(stringToNumber.safeConvert).toBeFunction();
expectTypeOf(stringToNumber.safeConvert)
.parameter(0)
.toMatchTypeOf<string>();
expectTypeOf(stringToNumber.safeConvert).returns.toMatchTypeOf<
SafeParseReturnType<string, number>
>();
const resA = stringToNumber.safeConvert(""); // valid input but invlaid output
if (resA.success) {
assertType<number>(resA.data);
} else {
assertType<z.ZodError>(resA.error);
}
expectTypeOf(numberToString.safeConvert).toBeFunction();
expectTypeOf(numberToString.safeConvert)
.parameter(0)
.toMatchTypeOf<number>();
expectTypeOf(numberToString.safeConvert).returns.toMatchTypeOf<
SafeParseReturnType<number, string>
>();
const resB = numberToString.safeConvert(321);
if (resB.success) {
assertType<string>(resB.data);
} else {
assertType<z.ZodError>(resB.error);
}
});
});
describe("convertAsync", () => {
test("input and output types to be enforced in", () => {
expectTypeOf(asyncStringToNumber.convertAsync).toBeFunction();
expectTypeOf(asyncStringToNumber.convertAsync)
.parameter(0)
.toMatchTypeOf<string>();
expectTypeOf(asyncStringToNumber.convertAsync).returns.toMatchTypeOf<
Promise<number>
>();
expectTypeOf(asyncNumberToString.convertAsync).toBeFunction();
expectTypeOf(asyncNumberToString.convertAsync)
.parameter(0)
.toMatchTypeOf<number>();
expectTypeOf(asyncNumberToString.convertAsync).returns.toMatchTypeOf<
Promise<string>
>();
});
});
describe("safeConvertAsync", () => {
test("input and output types to be enforced in", async () => {
expectTypeOf(asyncStringToNumber.safeConvertAsync).toBeFunction();
expectTypeOf(asyncStringToNumber.safeConvertAsync)
.parameter(0)
.toMatchTypeOf<string>();
expectTypeOf(asyncStringToNumber.safeConvertAsync).returns.toMatchTypeOf<
Promise<SafeParseReturnType<string, number>>
>();
const resA = await asyncStringToNumber.safeConvertAsync(""); // valid input but invlaid output
if (resA.success) {
assertType<number>(resA.data);
} else {
assertType<z.ZodError>(resA.error);
}
expectTypeOf(asyncNumberToString.safeConvertAsync).toBeFunction();
expectTypeOf(asyncNumberToString.safeConvertAsync)
.parameter(0)
.toMatchTypeOf<number>();
expectTypeOf(asyncNumberToString.safeConvertAsync).returns.toMatchTypeOf<
Promise<SafeParseReturnType<number, string>>
>();
const resB = await asyncNumberToString.safeConvertAsync(321);
if (resB.success) {
assertType<string>(resB.data);
} else {
assertType<z.ZodError>(resB.error);
}
});
});