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

时间:2016-10-28 08:12:36

标签: 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

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

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

1 个答案:

答案 0 :(得分:13)

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

    colors:
      type: array
      minItems: 1
      uniqueItems: true
      items:
        type: string
        enum: [ "red", "blue", "green" ]