要求数组在Swagger架构对象定义中包含至少一个元素

Byt*_*der 11 rest swagger swagger-2.0

我在我的模式中有这样的模式对象定义swagger.yaml:

User:
  type: object
  properties:
    username:
      type: string
      description: the user name
    colors:
      type: array
      items: {
        type: string,
        enum: [ "red", "blue", "green" ]
      }
      description: user must have one or more colors associated
  required:
    - username
    - colors
Run Code Online (Sandbox Code Playgroud)

但是,生成的服务器仍然很乐意接受使用此架构对象的POST请求作为不包含任何colors字段的必需body参数.

我是否可以在模式对象中color始终需要字段的方式配置Swagger,User理想情况下还必须包含枚举中的至少一个或多个项目?

Hel*_*len 16

使用minItems: 1.此外,您可以uniqueItems在阵列中强制执行.

    colors:
      type: array
      minItems: 1
      uniqueItems: true
      items:
        type: string
        enum: [ "red", "blue", "green" ]
Run Code Online (Sandbox Code Playgroud)