首页 > 解决方案 > 单独对象中的属性之间的 JSON 模式依赖关系

问题描述

为以下文档创建架构:

{
    "name": "billy",
    "foo": {
        "this": "something"
    },
    "bar": {
        "that": "something"
    }
}

$.bar.that如果$.foo.this存在,我希望架构需要。

标签: jsonjsonschema

解决方案


dependencies仅适用于当前对象,因此必须使用if/来完成then。您需要在每个级别使用properties来设置。required

{
  "if": {
    "properties": {
      "foo": { "required": ["this"] }
    },
    "required": ["foo"]
  },
  "then": {
    "properties": {
      "bar": { "required": ["this"] }
    },
    "required": ["bar"]
  }
}

推荐阅读