Search code examples
openapiswaggerhub

How to define an array with a fixed set of valid values?


I'm having trouble defining this field in OpenAPI. I've got a schema with a field that can hold an array of zero or more strings, like this { "daysOfWeek": ["Monday", "Wednesday", "Friday"] } or this { "daysOfWeek": ["Sunday", "Monday", "Tuesday", "Wednesday"] } or this { "daysOfWeek": []}.

The following schema definition yields this warning in SwaggerHub for each enum element: enum value should conform to its schema's type.

        "SampleSchema": {
            "type": "object",
            "properties": {
                "daysOfWeek": {
                    "description": "An array of zero or more days of the week",
                    "type": "array",
                    "items": {
                        "type": "string"
                    },
                    "enum": [
                        "Sunday",
                        "Monday",
                        "Tuesday",
                        "Wednesday",
                        "Thursday",
                        "Friday",
                        "Saturday"
                    ]
                }
            }
        }

Changing items.type to "array" produces the same warning.

What is the correct way to describe a field like this in OpenAPI?


Solution

  • The enum field refers to the array items, so it should be a part of the items object:

    
      "SampleSchema": {
        "type": "object",
        "properties": {
          "daysOfWeek": {
            "description": "An array of zero or more days of the week",
            "type": "array",
            "items": {
              "type": "string",
              "enum": [
                "Sunday",
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday"
              ]
            }
          }
        }
      }