Skip to content
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

Conditional use of "oneOf" instead of "anyOf" for particular z.union #236

Open
M1r1k opened this issue May 29, 2024 · 1 comment
Open

Conditional use of "oneOf" instead of "anyOf" for particular z.union #236

M1r1k opened this issue May 29, 2024 · 1 comment

Comments

@M1r1k
Copy link

M1r1k commented May 29, 2024

First, thank you for the great library!

I went through all related issues and PRs about but did not find anything :)

I have a case where z.union() is used not for different object structures but for object vs string:

const t = z.union([
 z.string(),
 z.object({ prop1: z.string() }),
 ]);

That generates OpenAPI like this:

anyOf:
  - type: string
  - type: object
    properties:
      prop1:
        type: string
    required:
      - prop1

Which ends up as

export type T = Partial<string> & Partial<{ prop1: string }>;

And I understand that in some cases it makes sense, but when different data formats are compared I need oneOf instead.
So is there any "smart" way to alter OpenAPI generation? So far I see only post-processing OpenAPIObject as a way to conditionally alter this behaviour

Thanks in advance!

@arjunyel
Copy link

@M1r1k also hitting this, what did you mean by post-processing the OpenAPIObject?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants