16

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
  • The problem has actually been the custom validation code which someone who used to work on that project wrote that did not correctly process the annotations generated by Swagger and therefore it was not working. Voting to close my question because it went away for reasons outside of this question's scope. Commented Nov 15, 2016 at 14:40

1 Answer 1

24

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" ]

Not the answer you're looking for? Browse other questions tagged or ask your own question.