forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartials.test.ts
68 lines (61 loc) · 1.64 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
import * as z from '../index';
import { util } from '../helpers/util';
const nested = z.object({
name: z.string(),
age: z.number(),
outer: z.object({
inner: 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;
};
const t1: util.AssertEqual<shallow, correct> = true;
t1;
});
test('shallow partial parse', () => {
const shallow = nested.partial();
shallow.parse({});
shallow.parse({
name: 'asdf',
age: 23143,
});
});
test('deep partial inference', () => {
const deep = nested.deepPartial();
type deep = z.infer<typeof deep>;
type correct = {
name?: string | undefined;
age?: number | undefined;
outer?: { inner?: string | undefined } | undefined;
};
const t1: util.AssertEqual<deep, correct> = true;
t1;
});
test('deep partial parse', () => {
const deep = nested.deepPartial();
expect(deep.shape.name instanceof z.ZodUnion).toBe(true);
expect(deep.shape.outer instanceof z.ZodUnion).toBe(true);
expect(deep.shape.outer._def.options[0] instanceof z.ZodObject).toBe(true);
expect(deep.shape.outer._def.options[0].shape.inner instanceof z.ZodUnion).toBe(true);
expect(deep.shape.outer._def.options[0].shape.inner._def.options[0] 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',
},
});
});