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?
Option 1: enum
If the list of possible values is small, you can list them all in an
enum
: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:In OpenAPI 3.1 you can replace
enum: [-1]
withconst: -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:In OpenAPI 3.1 you can replace
enum: [0]
withconst: 0
.However, actual tooling support for
not
may vary.