【问题标题】:JSON schema - how to make an enum-dependent field required?JSON 模式 - 如何使依赖于枚举的字段成为必需的?
【发布时间】:2017-08-22 10:21:26
【问题描述】:

我有一个 JSON 模式 (v7.0),我需要在其中确保完全列出的属性存在于最终的 formData 中。

在大多数情况下,这可以使用"additionalProperties": false"minProperties": X 解决,其中X 表示对象中的#properties(由this solution 提供)

但是,在某些情况下,属性的数量是可变的。

在下面的示例中就是这种情况,在“同步”中选择“手动更新”(枚举 2)可能会导致添加“手动日期”字段。当“sync”设置为“Manual update”时,需要“manual_date”属性——否则不应该是必需的。

我尝试通过依赖项下的“必需”语句来实现这一点,但我认为这是错误的,因为我在通过 validator 测试它时收到了一些非常广泛的错误消息。

简而言之:我正在寻找正确的方法来在我的 JSON 架构中创建依赖于枚举的字段。


我的架构:

{
  "type": "object",
  "properties": {
    "rtc": {
      "title": "REAL-TIME-CLOCK (RTC)",
      "type": "object",
      "properties": {
        "sync": {
          "title": "Time synchronization method",
          "type": "integer",
          "default": 1,
          "anyOf": [
            {
              "type": "integer",
              "title": "Retain current time",
              "enum": [
                1
              ]
            },
            {
              "type": "integer",
              "title": "Manual update",
              "enum": [
                2
              ]
            },
            {
              "type": "integer",
              "title": "Automatic update (requires WiFi)",
              "enum": [
                3
              ]
            }
          ]
        },
        "timezone": {
          "title": "Time zone (UTC−12 to UTC+14)",
          "type": "number",
          "default": 0,
          "minimum": -12,
          "maximum": 14
        },
        "adjustment": {
          "title": "Adjustment (-129600 to 129600 seconds)",
          "type": "number",
          "default": 0,
          "minimum": -129600,
          "maximum": 129600
        }
      },
      "dependencies": {
        "sync": {
          "oneOf": [
            {
              "properties": {
                "sync": {
                  "enum": [
                    1
                  ]
                }
              }
            },
            {
              "properties": {
                "sync": {
                  "enum": [
                    2
                  ]
                },
                "manual_date": {
                  "title": "Time (UTC) ",
                  "type": "string",
                  "format": "date-time"
                }
              },
              "required": [
                "manual_date"
              ]
            },
            {
              "properties": {
                "sync": {
                  "enum": [
                    3
                  ]
                }
              }
            }
          ]
        }
      },
      "additionalProperties": false,
      "patternProperties": {
        "manual_date": {}
      },
      "minProperties": 3
    }
  }
}

【问题讨论】:

    标签: jsonschema json-schema-validator react-jsonschema-forms


    【解决方案1】:

    更新:我没有找到直接的解决方案。但是,可以通过组合使用additionalProperties: falseminProperties: X 来完成部分解决方案。这有助于确保正确数量的字段 - 并且仅包含正确的字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-09
      • 2016-10-17
      • 1970-01-01
      • 2020-12-20
      • 2020-01-19
      • 2015-04-01
      • 1970-01-01
      相关资源
      最近更新 更多