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

refine.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
// @ts-ignore TS6133import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts";const test = Deno.test;
import { util } from "../helpers/util.ts";import * as z from "../index.ts";import { ZodIssueCode } from "../ZodError.ts";
test("refinement", () => { const obj1 = z.object({ first: z.string(), second: z.string(), }); const obj2 = obj1.partial().strict();
const obj3 = obj2.refine( (data) => data.first || data.second, "Either first or second should be filled in." );
expect(obj1 === (obj2 as any)).toEqual(false); expect(obj2 === (obj3 as any)).toEqual(false);
expect(() => obj1.parse({})).toThrow(); expect(() => obj2.parse({ third: "adsf" })).toThrow(); expect(() => obj3.parse({})).toThrow(); obj3.parse({ first: "a" }); obj3.parse({ second: "a" }); obj3.parse({ first: "a", second: "a" });});
test("refinement 2", () => { const validationSchema = z .object({ email: z.string().email(), password: z.string(), confirmPassword: z.string(), }) .refine( (data) => data.password === data.confirmPassword, "Both password and confirmation must match" );
expect(() => validationSchema.parse({ email: "aaaa@gmail.com", password: "aaaaaaaa", confirmPassword: "bbbbbbbb", }) ).toThrow();});
test("refinement type guard", () => { const validationSchema = z.object({ a: z.string().refine((s): s is "a" => s === "a"), }); type Input = z.input<typeof validationSchema>; type Schema = z.infer<typeof validationSchema>;
util.assertEqual<"a", Input["a"]>(false); util.assertEqual<string, Input["a"]>(true);
util.assertEqual<"a", Schema["a"]>(true); util.assertEqual<string, Schema["a"]>(false);});
test("refinement Promise", async () => { const validationSchema = z .object({ email: z.string().email(), password: z.string(), confirmPassword: z.string(), }) .refine( (data) => Promise.resolve().then(() => data.password === data.confirmPassword), "Both password and confirmation must match" );
await validationSchema.parseAsync({ email: "aaaa@gmail.com", password: "password", confirmPassword: "password", });});
test("custom path", async () => { const result = await z .object({ password: z.string(), confirm: z.string(), }) .refine((data) => data.confirm === data.password, { path: ["confirm"] }) .spa({ password: "asdf", confirm: "qewr" }); expect(result.success).toEqual(false); if (!result.success) { expect(result.error.issues[0].path).toEqual(["confirm"]); }});
test("use path in refinement context", async () => { const noNested = z.string()._refinement((_val, ctx) => { if (ctx.path.length > 0) { ctx.addIssue({ code: ZodIssueCode.custom, message: `schema cannot be nested. path: ${ctx.path.join(".")}`, }); return false; } else { return true; } });
const data = z.object({ foo: noNested, });
const t1 = await noNested.spa("asdf"); const t2 = await data.spa({ foo: "asdf" });
expect(t1.success).toBe(true); expect(t2.success).toBe(false); if (t2.success === false) { expect(t2.error.issues[0].message).toEqual( "schema cannot be nested. path: foo" ); }});
test("superRefine", () => { const Strings = z.array(z.string()).superRefine((val, ctx) => { if (val.length > 3) { ctx.addIssue({ code: z.ZodIssueCode.too_big, maximum: 3, type: "array", inclusive: true, message: "Too many items 😡", }); }
if (val.length !== new Set(val).size) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `No duplicates allowed.`, }); } });
const result = Strings.safeParse(["asfd", "asfd", "asfd", "asfd"]);
expect(result.success).toEqual(false); if (!result.success) expect(result.error.issues.length).toEqual(2);
Strings.parse(["asfd", "qwer"]);});
test("get inner type", () => { z.string() .refine(() => true) .innerType() .parse("asdf");});
test("chained refinements", () => { const objectSchema = z .object({ length: z.number(), size: z.number(), }) .refine(({ length }) => length > 5, { path: ["length"], message: "length greater than 5", }) .refine(({ size }) => size > 7, { path: ["size"], message: "size greater than 7", }); const r1 = objectSchema.safeParse({ length: 4, size: 9, }); expect(r1.success).toEqual(false); if (!r1.success) expect(r1.error.issues.length).toEqual(1);
const r2 = objectSchema.safeParse({ length: 4, size: 3, }); expect(r2.success).toEqual(false); if (!r2.success) expect(r2.error.issues.length).toEqual(2);});
test("fatal superRefine", () => { const Strings = z .string() .superRefine((val, ctx) => { if (val === "") { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "foo", fatal: true, }); } }) .superRefine((val, ctx) => { if (val !== " ") { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "bar", }); } });
const result = Strings.safeParse("");
expect(result.success).toEqual(false); if (!result.success) expect(result.error.issues.length).toEqual(1);});
zod

Version Info

Tagged at
a year ago