-
-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merging multiple calls to openapi()
#303
Comments
Hey @mrichards42! Thanks for raising the issue. This one is admittedly tricky, as I think this is modeled similar to I do see your conundrum, but I'm not certain if I want to make a change to make them merge. I'd have to think about the implications |
Agreed, I don't think this is straightforward, just wanted to raise the issue for discussion, and especially see if you had any thoughts that wouldn't require an invasive change. I'm not sure there's really a good example of this pattern in zod itself, since |
I stumbled upon the same thing. A lot of the times, we want to add special examples or description to existing schemas used in different contexts, but I was bummed to see that multiple Example: const userId = z
.string()
.format('x-user-id') // we use custom formats to generate type-safe client SDK
.openapi({ example: "usr-123-456" })
// elsewhere
const message = z.object({
senderId: userId.openapi({ description: "id of the sender" }) // this overwrites the example
}) If you don't want to break existing code, then the two alternatives are:
However, my first intuition was that multiple |
Hmmm I'm considering maybe adding an opts object here: https://github.com/samchungy/zod-openapi/blob/master/src/extendZod.ts#L4 to be able to configure it. Another issue I foresee with introducing this would be the eg. const foo = z.string().openapi({ ref: 'foo' });
const foo2 = foo.openapi( { description: "foo" });
// Reference foo is already registered |
That makes sense to me, something like extendZodWithOpenApi(z, {
chainingBehavior: 'merge' // 'override'
// or perhaps
mergeChained: true // default false
}) You'd need to define a new function that would perform the merging and a set of properties that are never merged. I don't know if there are any edge with nested objects or arrays, but I would personally not bother with that and just overwrite all repeated top-level properties. |
Hey, just keeping you updated but I'll have a form of this in the library over the next couple weeks. |
Hi, thanks for a great library!
I was surprised to see that if I make multiple calls to
openapi()
that the_def.openapi
object is overwritten instead of being extended. My use case is that I have an existing module with some custom zod schemas (notz.custom()
, I just mean functions returning schemas) that have partially filled out openapi info, where I then want to finish with a description and example at the point they are used.That's a mouthful, so an example (this isn't my exact code, but close enough)
IMO augmenting is a little closer to how the rest of zod works (e.g. if i make a call to z.number().positive() and then add .description() I don't lose .positive()), but I get that changing this now could break existing code. Any thoughts?
The text was updated successfully, but these errors were encountered: