How does one indicate that a number is prohibited for use in swagger documentation?

感情迁移 提交于 2020-04-16 02:28:30

问题


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?


回答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.0.

Option 2: oneOf

In OpenAPI 3.0 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

Option 3: not

OpenAPI 3.0 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]

However, actual tooling support for not may vary.



来源:https://stackoverflow.com/questions/60657280/how-does-one-indicate-that-a-number-is-prohibited-for-use-in-swagger-documentati

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!