deno.land / x / zod@v3.19.1 / __tests__ / all-errors.test.ts

all-errors.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
// @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";
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 TestFlattenedErrors = z.inferFlattenedErrors< typeof Test, { message: string; code: number }>;type TestFormErrors = z.inferFlattenedErrors<typeof Test>;
test("default flattened errors type inference", () => { type TestTypeErrors = { formErrors: string[]; fieldErrors: { [P in keyof z.TypeOf<typeof Test>]?: string[] | undefined }; };
util.assertEqual<z.inferFlattenedErrors<typeof Test>, TestTypeErrors>(true); util.assertEqual< z.inferFlattenedErrors<typeof Test, { message: string }>, TestTypeErrors >(false);});
test("custom flattened errors type inference", () => { type ErrorType = { message: string; code: number }; type TestTypeErrors = { formErrors: ErrorType[]; fieldErrors: { [P in keyof z.TypeOf<typeof Test>]?: ErrorType[] | undefined; }; };
util.assertEqual<z.inferFlattenedErrors<typeof Test>, TestTypeErrors>(false); util.assertEqual< z.inferFlattenedErrors<typeof Test, { message: string; code: number }>, TestTypeErrors >(true); util.assertEqual< z.inferFlattenedErrors<typeof Test, { message: string }>, TestTypeErrors >(false);});
test("form errors type inference", () => { type TestTypeErrors = { formErrors: string[]; fieldErrors: { [P in keyof z.TypeOf<typeof Test>]?: string[] | undefined }; };
util.assertEqual<z.inferFlattenedErrors<typeof Test>, TestTypeErrors>(true);});
test(".flatten() type assertion", () => { const parsed = Test.safeParse({}) as z.SafeParseError<void>; const validFlattenedErrors: TestFlattenedErrors = parsed.error.flatten( () => ({ message: "", code: 0 }) ); // @ts-expect-error should fail assertion between `TestFlattenedErrors` and unmapped `flatten()`. const invalidFlattenedErrors: TestFlattenedErrors = parsed.error.flatten(); const validFormErrors: TestFormErrors = parsed.error.flatten(); // @ts-expect-error should fail assertion between `TestFormErrors` and mapped `flatten()`. const invalidFormErrors: TestFormErrors = parsed.error.flatten(() => ({ message: "string", code: 0, }));
[ validFlattenedErrors, invalidFlattenedErrors, validFormErrors, invalidFormErrors, ];});
test(".formErrors type assertion", () => { const parsed = Test.safeParse({}) as z.SafeParseError<void>; const validFormErrors: TestFormErrors = parsed.error.formErrors; // @ts-expect-error should fail assertion between `TestFlattenedErrors` and `.formErrors`. const invalidFlattenedErrors: TestFlattenedErrors = parsed.error.formErrors;
[validFormErrors, invalidFlattenedErrors];});
test("all errors", () => { const propertySchema = z.string(); const schema = z .object({ a: propertySchema, b: propertySchema, }) .refine( (val) => { return val.a === val.b; }, { message: "Must be equal" } );
try { schema.parse({ a: "asdf", b: "qwer", }); } catch (error) { if (error instanceof z.ZodError) { expect(error.flatten()).toEqual({ formErrors: ["Must be equal"], fieldErrors: {}, }); } }
try { schema.parse({ a: null, b: null, }); } catch (_error) { const error = _error as z.ZodError; expect(error.flatten()).toEqual({ formErrors: [], fieldErrors: { a: ["Expected string, received null"], b: ["Expected string, received null"], }, });
expect(error.flatten((iss) => iss.message.toUpperCase())).toEqual({ formErrors: [], fieldErrors: { a: ["EXPECTED STRING, RECEIVED NULL"], b: ["EXPECTED STRING, RECEIVED NULL"], }, }); // Test identity
expect(error.flatten((i: z.ZodIssue) => i)).toEqual({ formErrors: [], fieldErrors: { a: [ { code: "invalid_type", expected: "string", message: "Expected string, received null", path: ["a"], received: "null", }, ], b: [ { code: "invalid_type", expected: "string", message: "Expected string, received null", path: ["b"], received: "null", }, ], }, }); // Test mapping expect(error.flatten((i: z.ZodIssue) => i.message.length)).toEqual({ formErrors: [], fieldErrors: { a: ["Expected string, received null".length], b: ["Expected string, received null".length], }, }); }});
zod

Version Info

Tagged at
a year ago