Skip to content

Commit 2e4b9d8

Browse files
authored
error handling for unparsed zod input (iway1#64)
1 parent aa71b1e commit 2e4b9d8

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

packages/dev-app/src/router.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const postsRouter = createTRPCRouter({
5858
};
5959
}),
6060
});
61+
const discriminatedFieldEnum = z.enum(["One", "Two"]);
6162

6263
export const appRouter = createTRPCRouter({
6364
postsRouter,
@@ -156,6 +157,26 @@ export const appRouter = createTRPCRouter({
156157
content,
157158
};
158159
}),
160+
discriminatedUnionInput: procedure
161+
.input(
162+
z.object({
163+
aDiscriminatedUnion: z.discriminatedUnion("discriminatedField", [
164+
z.object({
165+
discriminatedField: discriminatedFieldEnum.extract(["One"]), // <-- this doesn't work
166+
aFieldThatOnlyShowsWhenValueIsOne: z.string(),
167+
}),
168+
z.object({
169+
discriminatedField: z.literal("Two"),
170+
aFieldThatOnlyShowsWhenValueIsTwo: z.object({
171+
someTextFieldInAnObject: z.string(),
172+
}),
173+
}),
174+
]),
175+
}),
176+
)
177+
.query(({ input }) => {
178+
return input;
179+
}),
159180
procedureWithDescription: procedure
160181
.meta({
161182
description:

packages/trpc-ui/src/react-app/components/form/fields/ObjectField.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ export function ObjectField({
3434
</div>
3535
);
3636
}
37-
return (
38-
<InputGroupContainer
39-
title={label}
40-
iconElement={overrideIconElement ?? <ObjectIcon className="mr-1" />}
41-
>
42-
{Object.entries(node.children).map(([childFieldName, e]) => (
37+
const renderField = () => {
38+
try {
39+
return Object.entries(node.children).map(([childFieldName, e]) => (
4340
<Field
4441
inputNode={{
4542
...e,
@@ -48,7 +45,35 @@ export function ObjectField({
4845
control={control}
4946
key={childFieldName}
5047
/>
51-
))}
48+
));
49+
} catch (e) {
50+
return (
51+
<div>
52+
<h1 className="font-semibold text-error text-xl">
53+
Field Rendering Error
54+
</h1>
55+
Unfortunately, trpc-ui is unable to render the requested zod field.
56+
Not all zod validators are fully supported.{" "}
57+
<a
58+
href="https://github.com/aidansunbury/trpc-ui/issues/new?template=bug_report.md"
59+
target="_blank"
60+
rel="noreferrer"
61+
className="text-blue-600 underline visited:text-purple-600"
62+
>
63+
open an issue ↗
64+
</a>{" "}
65+
with the zod schema that caused this error so that it can be fixed.
66+
</div>
67+
);
68+
}
69+
};
70+
71+
return (
72+
<InputGroupContainer
73+
title={label}
74+
iconElement={overrideIconElement ?? <ObjectIcon className="mr-1" />}
75+
>
76+
{renderField()}
5277
</InputGroupContainer>
5378
);
5479
}

0 commit comments

Comments
 (0)