【问题标题】:JSONSchema - Required property dependent on parent propertyJSONSchema - 依赖于父属性的必需属性
【发布时间】:2019-11-30 03:47:09
【问题描述】:

我想根据根架构中是否存在属性,在数组子架构中应用额外的“必需”属性。我的架构设置如下:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required":[
       "isParentDependency",
       "subArray"
    ],
    "properties": {
        "isParentDependency": {
            "$id": "#/properties/isParentDependency",
            "type": "boolean"
        },
        "subArray": {
            "$id": "#/properties/subArray",
            "type": "array",
            "items": {
                "$id": "#/properties/subArrayItem",
                "required": ["alwaysRequiredProp"],
                "dependencies": {
                    "isParentDependency":{
                        "required":["requiredPropIfIsParentDependency"]
                    }
                },
                "properties": {
                    "alwaysRequiredProp": {
                        "$id": "#/properties/subArray/items/properties/alwaysRequiredProp",
                        "type": "boolean"
                    },
                    "requiredPropIfIsParentDependency": {
                        "$id": "#/properties/subArray/items/properties/requiredPropIfIsParentDependency",
                        "type": "boolean"
                    }
                }
            }
        }
    }
}

通过案例

{
    "isParentDependency": false,
    "subArray": [{
        "alwaysRequiredProp": true
    }]
}
    "isParentDependency": true,
    "subArray": [{
        "alwaysRequiredProp": true,
        "requiredPropIfIsParentDependency":true
    }]
}

失败案例

{
    "isParentDependency": true,
    "subArray": [{
        "alwaysRequiredProp": true
    }]
}

显然这行不通,但我一直无法弄清楚如何创建指向根架构的指针(或使用 $ref 应用 if/else 类型的解决方案)

非常感谢任何指导!

【问题讨论】:

  • 您无法“向上查看”树,因此您必须在根级别定义所需内容并使用子模式。我很乐意提供帮助,但是请您先清理一下您的示例吗?不完整的 JSON,并且不识别它正在使用哪个草稿(我将假设草稿 7)。此外,请为您的通过和失败条件提供一个 JSON 实例(这将使我能够更快地给您答案)。
  • 非常感谢@Relequestual - 我已经(希望)用你需要的东西充实了 Q
  • 超级。工作...

标签: javascript jsonschema ajv


【解决方案1】:

使用 JSON Schema,嵌套在树下的每一层 properties 都看不到树上。因此,您必须在它需要查看的最顶层定义您的条件。在这种情况下,这是根级别。

看看这个架构。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "definitions": {
    "subArray": {
      "type": "array",
      "items": {
        "required": [
          "alwaysRequiredProp"
        ],
        "properties": {
          "alwaysRequiredProp": {
            "type": "boolean"
          },
          "requiredPropIfIsParentDependency": {
            "type": "boolean"
          }
        }
      }
    }
  },
  "required": [
    "isParentDependency",
    "subArray"
  ],
  "properties": {
    "isParentDependency": {
      "type": "boolean"
    },
    "subArray": {
      "$ref": "#/definitions/subArray"
    }
  },
  "if": {
    "properties": {
      "isParentDependency": {
        "const": true
      }
    }
  },
  "then": {
    "properties": {
      "subArray": {
        "items": {
          "required": [
            "requiredPropIfIsParentDependency"
          ]
        }
      }
    }
  }
}

subArray 已被移动到定义中,然后被引用,因此可以更轻松地查看条件应用程序逻辑。

subArray 的定义只决定了items 结构的属性,如果提供的话,需要alwaysRequiredProp。它不需要requiredPropIfIsParentDependency

使用ifthen 关键字进入条件应用程序。

如果if 架构为真,则应用else 架构。

if 的值是一个模式。它适用于根级别。它要求 isParentDependencytrue (它不需要属性本身,因为这始终是必需的。在 if 语句中要求属性并不意味着始终需要该属性,除非 elsefalse,但是我们甚至没有在这里使用else)。

isParentDependencytrue 时,应用then 架构,该架构向下查找subArray 的项目,需要requiredPropIfIsParentDependency

您可以使用jsonschema.dev(预加载此架构和您的失败​​实例)在浏览器中测试此功能。

【讨论】:

  • 你吃 JSONSchema!!那是一个很好的解释-添加为定义是我未能掌握的一点。非常感谢!
  • 不客气,迈克。我是规范编写者和管理员之一,所以我非常了解它。随时通过网站讨论链接加入我们的 slack。
  • 那么感谢您的辛勤工作!我正在使用它来验证 React 中许多相互关联的组件中的状态,并且被证明是一个非常整洁的解决方案
  • 欢迎您!我们没有资金,所以如果你想支持我们,请查看我个人资料中的链接。我们也希望尽快有一个 OpenCollective。
  • 价格便宜两倍!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 2017-12-16
  • 1970-01-01
  • 2015-10-03
  • 2016-07-08
相关资源
最近更新 更多