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

3.0 Support: “not?” #203

Closed
drwpow opened this issue Apr 22, 2020 · 3 comments
Closed

3.0 Support: “not?” #203

drwpow opened this issue Apr 22, 2020 · 3 comments
Labels
question Further information is requested

Comments

@drwpow
Copy link
Owner

drwpow commented Apr 22, 2020

So the one 3.0 feature I’m thinking about not supporting is not. The reason is… I’m not sure how!

According to the definition:

The not keyword does not exactly combine schemas, but as all of the keywords mentioned above it helps you to modify your schemas and make them more specific.

The example given is:

    PetByType:
      type: object
      properties:
        pet_type:
          not:
            type: integer

While that can be done in JavaScript at runtime (typeof PetByType !== 'number'), I don’t think it’s possible in TypeScript to create a type that’s equal to anything but the given type.

Am I wrong? Is this possible?

@drwpow drwpow added the question Further information is requested label Apr 22, 2020
@crickford
Copy link

It seems like this could be accomplished with a general union type which expresses all the basic types which openAPI supports, and the Exclude utility type:

type GenericTypes =
  | { [key: string]: unknown }
  | unknown[]
  | string
  | number
  | boolean;

type nonNumber = Exclude<GenericTypes, number>;

Of course the example in the docs cannot actually be statically narrowed at all, due to Typescript having no integer type 🙂

@drwpow
Copy link
Owner Author

drwpow commented May 5, 2020

I actually haven’t used Exclude<> much! But it looks promising.

🤔 I think your solution is absolutely perfect for primitives. But my hesitation would be when it comes to advanced types. Like, what would not: { $ref: '…' } look like?

I haven’t run across not used in a real-world schema, and I couldn‘t get a good picture from the spec how this is enforced. Should we just put a pin in this until someone has a real-world example how they use it?

@crickford
Copy link

Good point(s). Waiting for a sample usecase sounds like a good plan to me 👍

@drwpow drwpow closed this as completed Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants