2

I am trying to specify a property in my swagger documentation called myNumber, which can be any integer in [-1, 10] except 0. This is what I have so far:

myNumber:
    type: integer
    description: You can use any number in [-1, 10] except 0.
    minimum: -1
    maximum: 10

How can I be explicit that 0 is prohibited? I haven't found any specification for this in the openAPI docs. Is this even possible?

yalpsid eman
  • 3,064
  • 6
  • 45
  • 71
  • 1
    Related: [Describe a multiple range parameter with Swagger](https://stackoverflow.com/q/58874658/113116) – Helen Mar 12 '20 at 16:01

1 Answers1

1

Option 1: enum

If the list of possible values is small, you can list them all in an enum:

myNumber:
  type: integer
  description: You can use any number in [-1, 10] except 0.
  enum: [-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

This is the easiest solution and works in both OpenAPI 2.0 and 3.x.

Option 2: oneOf

In OpenAPI 3.x you can use oneOf to define two ranges of possible values:

myNumber:
  type: integer
  description: You can use any number in [-1, 10] except 0.
  oneOf:
    - enum: [-1]  # shorthand for `minimum: -1` + `maximum: -1`
    - minimum: 1
      maximum: 10

In OpenAPI 3.1 you can replace enum: [-1] with const: -1.

Option 3: not

OpenAPI 3.x also supports not to define conditions that an instance must not meet. For example, you exclude the value 0 as follows:

myNumber:
  type: integer
  description: You can use any number in [-1, 10] except 0.
  minimum: -1
  maximum: 10
  not:
    enum: [0]

In OpenAPI 3.1 you can replace enum: [0] with const: 0.

However, actual tooling support for not may vary.

Helen
  • 87,344
  • 17
  • 243
  • 314