-
-
Notifications
You must be signed in to change notification settings - Fork 168
Support for union or discriminatedUnion? #255
Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I looked into this. Looks like a non-trivial change since To properly support OpenAPI3 style discriminators, one would have to either modify I think the latter would be better for this library, specifically. However, I still need to investigate further what a preprocessing or post processing step would look like in practice. Open to ideas! |
This would be a great feature. Unions are extremely useful for creating strict contracts and type narrowing. I definitely miss having this ability when working with openapi. |
what happened this support? how can I tackle a discriminatedUnion without using it? |
I wrote a hacky implementation to start a discussion, but realized that my needs were for nested discriminated unions, which are not supported in the OpenAPI 3 spec. I closed my PR, but someone feel free to take a look at the kernel of the PR and open a new one. |
I don't understand the challenge since |
This is also triggered by |
+1 |
I cannot use this library just because it lacks this feature. This is a necessary thing, but it seems like the library has not been functioning actively recently, could it be that it has died? |
I wrote some logic to handle Is there something that I'm misunderstanding about this requirement or is my situation a little different/simpler? I can make the PR if what I've done locally is somewhat beneficial. |
+1 |
This issue could be hacked around like this const unionSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('number'), number: z.number() }),
z.object({ type: z.literal('string'), string: z.string() }),
]);
const restSchema = z.object({
type: z.string(),
number: z.number().optional(),
string: z.string().optional(),
});
export const restRouter = router({
test: procedure
.meta({
openapi: {
method: 'GET',
path: '/test',
},
})
.input(restSchema)
.output(z.object({}))
.query(async ({ input }) => {
const validatedInput = await unionSchema.parseAsync(input);
return {};
}),
}) The code is still safe, just with a bit of duplication |
The following code produces a runtime error:
TRPCError: [query.thing] - Input parser must be a ZodObject
Is there any planned support for union or discriminatedUnion?
The text was updated successfully, but these errors were encountered: