【问题标题】:JSON Schema: Enum not working in an array of objectsJSON Schema:枚举在对象数组中不起作用
【发布时间】:2021-08-31 12:50:35
【问题描述】:

枚举似乎不适用于对象数组。我正在使用 2020-12 草案,这是我的 JSON 的结构如下:

{
  "main": {
    "items": [
      {
        "attribute1": "Option 1"
      },
      {
        "attribute1": "Option 2",
        "attribute2": "some random string"
      }
    ]
  }
}

属性 1 是强制性的,它必须是三个值之一 - “选项 1”、“选项 2”或“选项 3”。

我尝试了几种 JSON 模式的变体,但似乎都无法满足要求。

变体 1:作为 $ref

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schema.json",
  "$defs": {
    "my_shiny_object": {
      "type": "object",
      "required": [
        "attribute1"
      ],
      "properties": {
        "attribute1": {
          "description": "This is mandatory attribute 1 with an enum",
          "type": "string",
          "enum": [
            "Option 1",
            "Option 2",
            "Option 3"
            ]
        },
        "attribute2": {
          "type": "string",
          "description": "This is an optional attribute 2"
        }
      }
    }
  },
  "title": "root",
  "description": "Root Element.",
  "type": "object",
  "required": [
    "main"
  ],
  "properties": {
    "main": {
      "type": "object",
      "title": "main",
      "description": "Main is the top level field.",
      "required": [
        "items"
      ],
      "properties": {
        "date_time": {
          "type": "string",
          "format": "date-time",
          "title": "Date/time optional",
          "description": "Date time"
        }
      },
      "items": {
        "type": "array",
        "description": "Items included in main",
        "items": {
          "$ref": "#/$defs/my_shiny_object"
        }
      }
    }
  }
}

变体 2:内联定义

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/schema.json",
  "title": "root",
  "description": "Root Element.",
  "type": "object",
  "required": [
    "main"
  ],
  "properties": {
    "main": {
      "type": "object",
      "title": "main",
      "description": "Main is the top level field.",
      "required": [
        "items"
      ],
      "properties": {
        "date_time": {
          "type": "string",
          "format": "date-time",
          "title": "Date/time optional",
          "description": "Date time"
        }
      },
      "items": {
        "type": "array",
        "description": "Items included in main",
        "items": {
          "type": "object",
          "required": [
            "attribute1"
          ],
          "properties": {
            "attribute1": {
              "description": "This is mandatory attribute 1 with an enum",
              "type": "string",
              "enum": [
                "Option 1",
                "Option 2",
                "Option 3"
              ]
            },
            "attribute2": {
              "type": "string",
              "description": "This is an optional attribute 2"
            }
          }
        }
      }
    }
  }
}

为属性 1 字段提供不正确的值,甚至排除它不会触发任何验证错误。我尝试使用Hyperjump JSVjschon.dev

【问题讨论】:

  • 请提供您的完整架构和一些您希望通过和失败的数据。请参阅stackoverflow.com/help/minimal-reproducible-example - 如果没有完整的架构和示例数据,就不可能知道“似乎效果不佳”是什么意思。另外,您能解释一下“使用对象数组”是什么意思吗?您的示例显示了一个字符串数组。
  • 示例是一个包含字符串枚举值的对象数组
  • 抱歉。工作了一整天后问这个问题。我已添加您要求的详细信息。请指教。

标签: jsonschema json-schema-validator


【解决方案1】:

您没有在正确的位置定义您的“项目”属性。 /properties/main/items 的架构需要移动到 /properties/main/properties/items。然后它将按照您的预期进行评估。

【讨论】:

  • 谢谢杰森。如此简单的错字,在过去的几天里一直让我发疯。干杯。
猜你喜欢
  • 2022-12-17
  • 2022-01-23
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 2023-02-06
  • 1970-01-01
  • 2021-11-29
  • 2018-07-09
相关资源
最近更新 更多