deno.land / x / zod@v3.19.1 / __tests__ / intersection.test.ts

intersection.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
// @ts-ignore TS6133import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts";const test = Deno.test;
import * as z from "../index.ts";
test("object intersection", () => { const BaseTeacher = z.object({ subjects: z.array(z.string()), }); const HasID = z.object({ id: z.string() });
const Teacher = z.intersection(BaseTeacher.passthrough(), HasID); // BaseTeacher.merge(HasID); const data = { subjects: ["math"], id: "asdfasdf", }; expect(Teacher.parse(data)).toEqual(data); expect(() => Teacher.parse({ subject: data.subjects })).toThrow(); expect(Teacher.parse({ ...data, extra: 12 })).toEqual({ ...data, extra: 12 });
expect(() => z.intersection(BaseTeacher.strict(), HasID).parse({ ...data, extra: 12 }) ).toThrow();});
test("deep intersection", () => { const Animal = z.object({ properties: z.object({ is_animal: z.boolean(), }), }); const Cat = z .object({ properties: z.object({ jumped: z.boolean(), }), }) .and(Animal);
type Cat = z.infer<typeof Cat>; // const cat:Cat = 'asdf' as any; const cat = Cat.parse({ properties: { is_animal: true, jumped: true } }); expect(cat.properties).toEqual({ is_animal: true, jumped: true });});
test("deep intersection of arrays", async () => { const Author = z.object({ posts: z.array( z.object({ post_id: z.number(), }) ), }); const Registry = z .object({ posts: z.array( z.object({ title: z.string(), }) ), }) .and(Author);
const posts = [ { post_id: 1, title: "Novels" }, { post_id: 2, title: "Fairy tales" }, ]; const cat = Registry.parse({ posts }); expect(cat.posts).toEqual(posts); const asyncCat = await Registry.parseAsync({ posts }); expect(asyncCat.posts).toEqual(posts);});
test("invalid intersection types", async () => { const numberIntersection = z.intersection( z.number(), z.number().transform((x) => x + 1) );
const syncResult = numberIntersection.safeParse(1234); expect(syncResult.success).toEqual(false); if (!syncResult.success) { expect(syncResult.error.issues[0].code).toEqual( z.ZodIssueCode.invalid_intersection_types ); }
const asyncResult = await numberIntersection.spa(1234); expect(asyncResult.success).toEqual(false); if (!asyncResult.success) { expect(asyncResult.error.issues[0].code).toEqual( z.ZodIssueCode.invalid_intersection_types ); }});
test("invalid array merge", async () => { const stringArrInt = z.intersection( z.string().array(), z .string() .array() .transform((val) => [...val, "asdf"]) ); const syncResult = stringArrInt.safeParse(["asdf", "qwer"]); expect(syncResult.success).toEqual(false); if (!syncResult.success) { expect(syncResult.error.issues[0].code).toEqual( z.ZodIssueCode.invalid_intersection_types ); }
const asyncResult = await stringArrInt.spa(["asdf", "qwer"]); expect(asyncResult.success).toEqual(false); if (!asyncResult.success) { expect(asyncResult.error.issues[0].code).toEqual( z.ZodIssueCode.invalid_intersection_types ); }});
zod

Version Info

Tagged at
a year ago