forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.test.ts
138 lines (122 loc) · 3.2 KB
/
object.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
import * as z from '../index';
import { util } from '../helpers/util';
const Test = z.object({
f1: z.number(),
f2: z.string().optional(),
f3: z.string().nullable(),
f4: z.array(z.object({ t: z.union([z.string(), z.boolean()]) })),
});
type Test = z.infer<typeof Test>;
test('object type inference', () => {
type TestType = {
f1: number;
f2?: string | undefined;
f3: string | null;
f4: { t: string | boolean }[];
};
const t1: util.AssertEqual<z.TypeOf<typeof Test>, TestType> = true;
[t1];
});
test('unknown throw', () => {
const asdf: unknown = 35;
expect(() => Test.parse(asdf)).toThrow();
});
test('correct parsing', () => {
Test.parse({
f1: 12,
f2: 'string',
f3: 'string',
f4: [
{
t: 'string',
},
],
});
Test.parse({
f1: 12,
f3: null,
f4: [
{
t: false,
},
],
});
});
test('incorrect #1', () => {
expect(() => Test.parse({} as any)).toThrow();
});
test('inference', () => {
const t1 = z.object({
name: z.string(),
obj: z.object({}),
arrayarray: z.array(z.array(z.string())),
});
const i1 = t1.primitives();
type i1 = z.infer<typeof i1>;
const f1: util.AssertEqual<i1, { name: string }> = true;
const i2 = t1.nonprimitives();
type i2 = z.infer<typeof i2>;
const f2: util.AssertEqual<i2, { obj: {}; arrayarray: string[][] }> = true;
f1;
f2;
i1.parse({ name: 'name' });
i2.parse({ obj: {}, arrayarray: [['asdf']] });
expect(() => i1.parse({} as any)).toThrow();
});
test('nonstrict', () => {
z.object({ points: z.number() })
.nonstrict()
.parse({
points: 2314,
unknown: 'asdf',
});
});
test('primitives', () => {
const baseObj = z.object({
stringPrimitive: z.string(),
stringArrayPrimitive: z.array(z.string()),
numberPrimitive: z.number(),
numberArrayPrimitive: z.array(z.number()),
booleanPrimitive: z.boolean(),
booleanArrayPrimitive: z.array(z.boolean()),
bigintPrimitive: z.bigint(),
bigintArrayPrimitive: z.array(z.bigint()),
undefinedPrimitive: z.undefined(),
nullPrimitive: z.null(),
primitiveUnion: z.union([z.string(), z.number()]),
primitiveIntersection: z.intersection(z.string(), z.string()),
lazyPrimitive: z.lazy(() => z.string()),
literalPrimitive: z.literal('sup'),
enumPrimitive: z.enum(['asdf', 'qwer']),
datePrimitive: z.date(),
nonprimitiveUnion: z.union([z.string(), z.tuple([])]),
object: z.object({}),
objectArray: z.object({}).array(),
arrayarray: z.array(z.array(z.string())),
});
console.log(JSON.stringify(Object.keys(baseObj.nonprimitives()), null, 2));
expect(Object.keys(baseObj.primitives().shape)).toEqual([
'stringPrimitive',
'stringArrayPrimitive',
'numberPrimitive',
'numberArrayPrimitive',
'booleanPrimitive',
'booleanArrayPrimitive',
'bigintPrimitive',
'bigintArrayPrimitive',
'undefinedPrimitive',
'nullPrimitive',
'primitiveUnion',
'primitiveIntersection',
'lazyPrimitive',
'literalPrimitive',
'enumPrimitive',
'datePrimitive',
]);
expect(Object.keys(baseObj.nonprimitives().shape)).toEqual([
'nonprimitiveUnion',
'object',
'objectArray',
'arrayarray',
]);
});