Skip to content

Commit ecee1e9

Browse files
aprilrdYoung Min Kim
andauthored
fix: expose array element validation errors in zod resolver (#119)
Co-authored-by: Young Min Kim <[email protected]>
1 parent 115f041 commit ecee1e9

File tree

3 files changed

+67
-32
lines changed

3 files changed

+67
-32
lines changed

src/__snapshots__/zod.test.ts.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@ Object {
133133
"invalid_type": "Expected number, received string",
134134
},
135135
},
136+
"likedUsers": Object {
137+
"0": Object {
138+
"id": Object {
139+
"message": "Expected number, received string",
140+
"type": "invalid_type",
141+
"types": Object {
142+
"invalid_type": "Expected number, received string",
143+
},
144+
},
145+
},
146+
"message": "Invalid input",
147+
"type": "invalid_union",
148+
"types": Object {
149+
"invalid_type": "Expected undefined, received array",
150+
"invalid_union": "Invalid input",
151+
},
152+
},
136153
"password": Object {
137154
"message": "Should be at least 8 characters",
138155
"type": "too_small",

src/zod.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ const schema = z
1010
author: z.object({
1111
id: z.number(),
1212
}),
13+
likedUsers: z
14+
.array(
15+
z.object({
16+
id: z.number(),
17+
}),
18+
)
19+
.optional(),
1320
count: z.number().positive().int(),
1421
date: z.date(),
1522
url: z.string().url(),
@@ -36,6 +43,7 @@ describe('zodResolver', () => {
3643
author: {
3744
id: 1,
3845
},
46+
likedUsers: [{ id: 1 }],
3947
count: 4,
4048
date: new Date(),
4149
url: 'https://github.com/react-hook-form/resolvers',
@@ -73,6 +81,7 @@ describe('zodResolver', () => {
7381
author: {
7482
id: '1',
7583
},
84+
likedUsers: [{ id: '1' }],
7685
count: -5,
7786
date: 'date',
7887
password: 'R',

src/zod.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,48 @@ const parseErrorSchema = (
1717
return {};
1818
}
1919

20-
return zodError.errors.reduce<Record<string, any>>(
21-
(previous, { path, message, code: type }) => {
22-
const currentPath = convertArrayToPathName(path);
20+
const errors = [...zodError.errors];
21+
let previous: Record<string, any> = {};
2322

24-
return {
25-
...previous,
26-
...(path
27-
? previous[currentPath] && validateAllFieldCriteria
28-
? {
29-
[currentPath]: appendErrors(
30-
currentPath,
31-
validateAllFieldCriteria,
32-
previous,
33-
type,
34-
message,
35-
),
36-
}
37-
: {
38-
[currentPath]: previous[currentPath] || {
39-
message,
40-
type,
41-
...(validateAllFieldCriteria
42-
? {
43-
types: { [type]: message || true },
44-
}
45-
: {}),
46-
},
47-
}
48-
: {}),
49-
};
50-
},
51-
{},
52-
);
23+
for (const error of errors) {
24+
const { path, message, code: type } = error;
25+
const currentPath = convertArrayToPathName(path);
26+
27+
if ('unionErrors' in error) {
28+
for (const subErrors of error.unionErrors.map((e) => e.errors)) {
29+
errors.push(...subErrors);
30+
}
31+
}
32+
33+
previous = {
34+
...previous,
35+
...(path
36+
? previous[currentPath] && validateAllFieldCriteria
37+
? {
38+
[currentPath]: appendErrors(
39+
currentPath,
40+
validateAllFieldCriteria,
41+
previous,
42+
type,
43+
message,
44+
),
45+
}
46+
: {
47+
[currentPath]: previous[currentPath] || {
48+
message,
49+
type,
50+
...(validateAllFieldCriteria
51+
? {
52+
types: { [type]: message || true },
53+
}
54+
: {}),
55+
},
56+
}
57+
: {}),
58+
};
59+
}
60+
61+
return previous;
5362
};
5463

5564
export const zodResolver = <T extends z.ZodSchema<any, any>>(

0 commit comments

Comments
 (0)