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

Support intersecting union types (concat an object with alternatives) #3057

Open
mnahkies opened this issue Dec 21, 2024 · 0 comments
Open
Labels
feature New functionality or improvement

Comments

@mnahkies
Copy link

Runtime

node.js

Runtime version

22

Module version

17.13.3

Used with

@nahkies/openapi-code-generator

Any other relevant information

No response

What problem are you trying to solve?

Related: mnahkies/openapi-code-generator#282

In openapi 3 definitions you can intersect schemas with unioned schemas, essentially forming something like this in typescript terms:

(A | B) & C

At the moment the generator attempts to define this as something like:

const x = joi
  .alternatives()
  .try(
    joi
      .object()
      .keys({foo: joi.string().required()})
      .options({stripUnknown: true})
      .required(),
    joi
      .object()
      .keys({bar: joi.string().required()})
      .options({stripUnknown: true})
      .required(),
  )
  .required()
  .concat(
    joi
      .object()
      .keys({id: joi.string().required()})
      .options({stripUnknown: true})
      .required(),
  )
  .required()

However this results in a runtime error like:

[Error: Cannot merge type alternatives with another type: object]

(unit test here)

Do you have a new or modified API suggestion to solve the problem?

I'll skip that test for now, and leave it as a known issue, but it would be great if it would be possible to concat into alternatives.

Alternatives
I suspect I could probably come up with a way to detect definitions of this nature, and basically re-write / invert them such that it was a union of intersections, instead of an intersected union. Eg:

(A & C) | (A & C)

But I think this would probably be quite complicated to do correctly supporting arbitrary input.

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

No branches or pull requests

1 participant