Cannot create multiple errors response when generate Document #1332
-
So the thing it in the doc there are mention to create custom response here https://github.com/RobinTail/express-zod-api#response-customization. But you can only create 1 schema for 1 negative response, but if I want to create multiple errors like 400 for validate and 401 for authorization, there is no way to define that. How can I utilize the custom response to have multiple entries of error in the same result handler? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
Hello @huyhoang160593 Consider discriminated union schema for that purpose: const negativeResponseSchema = z.discriminatedUnion("cause", [
z.object({ cause: z.literal("validation"), message: z.string() }),
z.object({ cause: z.literal("authorization"), details: z.object({ ...}) }),
]); However, the generated documentation currently is designed for a single negative schema that does not depend on particular faulty status code. It's purpose was to illustrate what the negative payload could be, but not to differentiate between This could probably be improved later, but so far a nice way is not yet invented. |
Beta Was this translation helpful? Give feedback.
-
@huyhoang160593 , would you be interested in helping me to test the feature #1431 ? |
Beta Was this translation helpful? Give feedback.
-
For those looking for a solution to the same problem, |
Beta Was this translation helpful? Give feedback.
For those looking for a solution to the same problem,
the feature is available in version 16.2.0 🚀 — see Changelog for details.