【问题标题】:Correct way to define array of enums in JSON schema在 JSON 模式中定义枚举数组的正确方法
【发布时间】:2015-09-04 14:49:50
【问题描述】:

我想用 JSON 模式数组来描述,它应该由零个或多个预定义值组成。为了简单起见,让我们有这些可能的值:onetwothree

正确的数组(应该通过验证):

[]
["one", "one"]
["one", "three"]

不正确:

["four"]

现在,我知道应该使用"enum" 属性,但我找不到相关信息将它放在哪里。

选项 A("items" 下):

{
    "type": "array",
    "items": {
        "type": "string",
        "enum": ["one", "two", "three"]
    }
}

选项 B:

{
    "type": "array",
    "items": {
        "type": "string"
    },
    "enum": ["one", "two", "three"]
}

【问题讨论】:

标签: arrays json enums jsonschema


【解决方案1】:

根据json-schemadocumentationarray的枚举值必须包含在"items"字段中:

{
    "type": "array",
    "items": {
        "type": "string",
        "enum": ["one", "two", "three"]
    }
}

如果您有一个 array 可以容纳例如不同类型的项目,那么您的架构应该如下所示:

{
  "type": "array",
  "items": [
    {
      "type": "string",
      "enum": ["one", "two", "three"]
    },
    {
      "type": "integer",
      "enum": [1, 2, 3]
    }
  ]
}

【讨论】:

【解决方案2】:

选项 A 正确,满足您的要求。

{
    "type": "array",
    "items": {
        "type": "string",
        "enum": ["one", "two", "three"]
    }
}

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多