zod-openapi icon indicating copy to clipboard operation
zod-openapi copied to clipboard

Merging multiple calls to `openapi()`

Open mrichards42 opened this issue 1 year ago • 5 comments

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 (not z.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)

// I want effectType: "input" to sticky around
// this is just an example, in the real code I have several more openapi properties here
const funnyDate = z
  .string()
  .regex(funnyDateRegex)
  .transform(parseFunnyDate)
  .openapi({ effectType: "input" })

// when I use funnyDate, effectType gets clobbered by the second call to openapi
const SomeObject = z.object({
  date: funnyDate().openapi({ description: "hello world", example: "..." })
})

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?

mrichards42 avatar Aug 06 '24 15:08 mrichards42