首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON模式--如何使依赖于枚举的字段成为必需的?

JSON模式--如何使依赖于枚举的字段成为必需的?
EN

Stack Overflow用户
提问于 2017-03-29 05:13:55
回答 1查看 1.3K关注 0票数 2

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

在大多数情况下,这可以使用"additionalProperties": false"minProperties": X来解决,其中X表示对象中的#属性(这个解决方案的礼貌)

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

下面的例子就是这种情况,其中选择“同步”中的“手动更新”(enum 2)可以导致添加"manual_date“字段。当“同步”设置为“手动更新”时,需要使用"manual_date“属性,但否则不应该要求它。

我尝试通过依赖项下的"required“语句实现这一点,但我认为这是错误的,因为我在通过验证器测试它时得到了一些非常广泛的错误消息。

因此,简而言之:我正在寻找在我的JSON模式中创建与枚举相关的字段的正确方法。

我的模式:

代码语言:javascript
复制
{
  "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
    }
  }
}
EN

回答 1

Stack Overflow用户

发布于 2019-03-15 11:53:24

更新:我没有找到一个直接的解决办法。但是,可以结合使用additionalProperties: falseminProperties: X来实现部分解决方案。这有助于确保正确的字段数量--并且只包含正确的字段。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43084878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档