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

bug: allOf when an element is a oneOf union generates an invalid zod schema #282

Closed
mnahkies opened this issue Dec 18, 2024 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@mnahkies
Copy link
Owner

mnahkies commented Dec 18, 2024

Given a schema like:

components:
  schemas:
    Example:
      allOf:
        - oneOf:
            - type: object
              properties:
                foo:
                  type: string
            - type: object
              properties:
                bar:
                  type: string
        - type: object
          properties:
            id:
              type: string

Which produces the type:

export type t_Example = (
  | {
      foo?: string
    }
  | {
      bar?: string
    }
) & {
  id?: string
}

We get an invalid zod schema, as .merge isn't supported by ZodUnion

@mnahkies mnahkies added the bug Something isn't working label Dec 18, 2024
@mnahkies
Copy link
Owner Author

Note: this is also an issue with the joi schema builder.

mnahkies added a commit that referenced this issue Dec 21, 2024
`merge` is only available on `ZodObject` and so we must use `intersect`
if a `ZodIntersection` or `ZodUnion` is involved.

it'd probably be simpler to just always use `intersect` but that comes
with drawbacks like being unable to `pick` or `omit` the resulting
schema.

solves #282
@mnahkies
Copy link
Owner Author

Fixed for zod by #283, still an issue for joi (hapijs/joi#3057) but going to leave this as a known issue / todo in the code for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant