Require array to contain at least one element in Swagger Schema Object definition

泪湿孤枕 提交于 2019-12-06 19:33:01

问题


I'm having a Schema Object definition like this in my 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

However, the generated server still happily accepts POST requests using this schema object as required body parameter that do not contain any colors field.

Can I configure Swagger in a way that the color field is always required in a User schema object and ideally also must contain at least one or more items from the enum?


回答1:


Use minItems: 1. Additionally you can enforce uniqueItems within the array.

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


来源:https://stackoverflow.com/questions/40300780/require-array-to-contain-at-least-one-element-in-swagger-schema-object-definitio

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