Require an array to contain at least one element in a Swagger schema object definition

I have a schema object definition like this in mine 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 is still happy to accept POST requests using this schema object as a required body parameter that does not contain a field colors.

Can I configure Swagger so that the field is coloralways required in the schematic object Userand ideally should also contain at least one or more of the elements from the enumeration?

+4
source share
1 answer

minItems: 1. , uniqueItems .

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

Source: https://habr.com/ru/post/1659155/


All Articles