forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 1
/
partials.test.ts
267 lines (230 loc) · 7.17 KB
/
partials.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import { util } from "../helpers/util";
import * as z from "../index";
import { ZodNullable, ZodOptional } from "../index";
const nested = z.object({
name: z.string(),
age: z.number(),
outer: z.object({
inner: z.string(),
}),
array: z.array(z.object({ asdf: z.string() })),
});
test("shallow inference", () => {
const shallow = nested.partial();
type shallow = z.infer<typeof shallow>;
type correct = {
name?: string | undefined;
age?: number | undefined;
outer?: { inner: string } | undefined;
array?: { asdf: string }[];
};
util.assertEqual<shallow, correct>(true);
});
test("shallow partial parse", () => {
const shallow = nested.partial();
shallow.parse({});
shallow.parse({
name: "asdf",
age: 23143,
});
});
test("deep partial inference", () => {
const deep = nested.deepPartial();
const asdf = deep.shape.array.unwrap().element.shape.asdf.unwrap();
asdf.parse("asdf");
type deep = z.infer<typeof deep>;
type correct = {
array?: { asdf?: string }[];
name?: string | undefined;
age?: number | undefined;
outer?: { inner?: string | undefined } | undefined;
};
util.assertEqual<deep, correct>(true);
});
test("deep partial parse", () => {
const deep = nested.deepPartial();
expect(deep.shape.name instanceof z.ZodOptional).toBe(true);
expect(deep.shape.outer instanceof z.ZodOptional).toBe(true);
expect(deep.shape.outer._def.innerType instanceof z.ZodObject).toBe(true);
expect(
deep.shape.outer._def.innerType.shape.inner instanceof z.ZodOptional
).toBe(true);
expect(
deep.shape.outer._def.innerType.shape.inner._def.innerType instanceof
z.ZodString
).toBe(true);
});
test("deep partial runtime tests", () => {
const deep = nested.deepPartial();
deep.parse({});
deep.parse({
outer: {},
});
deep.parse({
name: "asdf",
age: 23143,
outer: {
inner: "adsf",
},
});
});
test("deep partial optional/nullable", () => {
const schema = z
.object({
name: z.string().optional(),
age: z.number().nullable(),
})
.deepPartial();
expect(schema.shape.name.unwrap()).toBeInstanceOf(ZodOptional);
expect(schema.shape.age.unwrap()).toBeInstanceOf(ZodNullable);
});
test("deep partial tuple", () => {
const schema = z
.object({
tuple: z.tuple([
z.object({
name: z.string().optional(),
age: z.number().nullable(),
}),
]),
})
.deepPartial();
expect(schema.shape.tuple.unwrap().items[0].shape.name).toBeInstanceOf(
ZodOptional
);
});
test("deep partial inference", () => {
const mySchema = z.object({
name: z.string(),
array: z.array(z.object({ asdf: z.string() })),
tuple: z.tuple([z.object({ value: z.string() })]),
});
const partialed = mySchema.deepPartial();
type partialed = z.infer<typeof partialed>;
type expected = {
name?: string | undefined;
array?:
| {
asdf?: string | undefined;
}[]
| undefined;
tuple?: [{ value?: string }] | undefined;
};
util.assertEqual<expected, partialed>(true);
});
test("required", () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
nullableField: z.number().nullable(),
nullishField: z.string().nullish(),
});
const requiredObject = object.required();
expect(requiredObject.shape.name).toBeInstanceOf(z.ZodString);
expect(requiredObject.shape.age).toBeInstanceOf(z.ZodNumber);
expect(requiredObject.shape.field).toBeInstanceOf(z.ZodDefault);
expect(requiredObject.shape.nullableField).toBeInstanceOf(z.ZodNullable);
expect(requiredObject.shape.nullishField).toBeInstanceOf(z.ZodNullable);
});
test("required inference", () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
nullableField: z.number().nullable(),
nullishField: z.string().nullish(),
});
const requiredObject = object.required();
type required = z.infer<typeof requiredObject>;
type expected = {
name: string;
age: number;
field: string;
nullableField: number | null;
nullishField: string | null;
};
util.assertEqual<expected, required>(true);
});
test("required with mask", () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
country: z.string().optional(),
});
const requiredObject = object.required({ age: true });
expect(requiredObject.shape.name).toBeInstanceOf(z.ZodString);
expect(requiredObject.shape.age).toBeInstanceOf(z.ZodNumber);
expect(requiredObject.shape.field).toBeInstanceOf(z.ZodDefault);
expect(requiredObject.shape.country).toBeInstanceOf(z.ZodOptional);
});
test("required with mask containing a nonexistent key", () => {
object.required({
age: true,
// @ts-expect-error should not accept unexpected keys.
doesntExist: true,
});
});
test("required with mask -- ignore falsy values", () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
country: z.string().optional(),
});
// @ts-expect-error
const requiredObject = object.required({ age: true, country: false });
expect(requiredObject.shape.name).toBeInstanceOf(z.ZodString);
expect(requiredObject.shape.age).toBeInstanceOf(z.ZodNumber);
expect(requiredObject.shape.field).toBeInstanceOf(z.ZodDefault);
expect(requiredObject.shape.country).toBeInstanceOf(z.ZodOptional);
});
test("partial with mask", async () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
country: z.string(),
});
const masked = object
.partial({ age: true, field: true, name: true })
.strict();
expect(masked.shape.name).toBeInstanceOf(z.ZodOptional);
expect(masked.shape.age).toBeInstanceOf(z.ZodOptional);
expect(masked.shape.field).toBeInstanceOf(z.ZodOptional);
expect(masked.shape.country).toBeInstanceOf(z.ZodString);
masked.parse({ country: "US" });
await masked.parseAsync({ country: "US" });
});
test("partial with mask -- ignore falsy values", async () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
country: z.string(),
});
// @ts-expect-error
const masked = object.partial({ name: true, country: false }).strict();
expect(masked.shape.name).toBeInstanceOf(z.ZodOptional);
expect(masked.shape.age).toBeInstanceOf(z.ZodOptional);
expect(masked.shape.field).toBeInstanceOf(z.ZodDefault);
expect(masked.shape.country).toBeInstanceOf(z.ZodString);
masked.parse({ country: "US" });
await masked.parseAsync({ country: "US" });
});
test("partial with mask containing a nonexistent key", () => {
const object = z.object({
name: z.string(),
age: z.number().optional(),
field: z.string().optional().default("asdf"),
country: z.string().optional(),
});
object.partial({
age: true,
// @ts-expect-error should not accept unexpected keys.
doesntExist: true,
});
});