astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)
openapi-zod-client.vercel.app
717 stars 80 forks source link

feat: add nullish to partial objects #282

Closed dgadelha closed 4 months ago

dgadelha commented 4 months ago

This PR adds a new option, addNullishToPartial, as well as a new CLI flag, --add-nullish-to-partial, that adds .nullish() to all .partial() objects, as a workaround for the issue https://github.com/colinhacks/zod/issues/2893

This is because partial objects allow undefined values, but not null values, which is causing an unexpected error for an API I'm currently integrating.

As there isn't any other way to achieve this with the currently-implemented options, I've added this feature as an opt-in flag with no breaking changes.

vercel[bot] commented 4 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
openapi-zod-client-rim4 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 10, 2024 3:40pm
astahmer commented 4 months ago

hey, is there a way to use this callback or make another dedicated for each schema if needed ? I'd like to avoid introducing another CLI flag

dgadelha commented 4 months ago

This callback seems to be very high-level, I'm not sure if it would be possible to achieve this without a complex code, but I might be wrong.

Your idea would be to add a callback at the beginning of getZodSchema to allow modifying the schema?

astahmer commented 4 months ago

this callback should hopefully make it possible for everyone to solve their custom use-cases just like yours 🙏

yes I think adding a callback in the options (TemplateContextOptions) that you'd call somewhere near CodeMeta.assign (so that you don't have to wrap each getZodSchema) should be enough ? it's been a while since the last time I added such feature so I might be wrong

dgadelha commented 4 months ago

Alright, I'll take a look into this.