如何定义具有一组固定有效值的数组?

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

我在 OpenAPI 中定义此字段时遇到问题。我有一个模式,其中有一个字段可以容纳零个或多个字符串的数组, 像这样 { "daysOfWeek": ["Monday", "Wednesday", "Friday"] } 或者这个 { "daysOfWeek": ["Sunday", "Monday", "Tuesday", "Wednesday"] } 或者这个 { "daysOfWeek": []}.

以下架构定义在 SwaggerHub 中针对每个枚举元素产生此警告: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"
                    ]
                }
            }
        }

items.type 更改为“array”会产生相同的警告。

在 OpenAPI 中描述这样的字段的正确方法是什么?

enum 字段引用数组项,因此它应该是 items 对象的一部分:


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