【问题标题】:Conditionally apply sub-schema, based on another file's property基于另一个文件的属性有条件地应用子模式
【发布时间】:2020-05-01 04:57:07
【问题描述】:

json-schema 是否允许基于另一个文件的属性值有条件子模式?

例子:

Jsons:

另一个.json

{
  "honey": "honey"
}

ma​​in.json

{
  "hello": "abc"
}

架构

{
  "type": "object",
  "properties": {
    "hello": {
      "type": "string"
    }
  },
  "required": ["hello"],
  "if": {
    "properties": {
      "another.json#/honey": {       -->> is this possible
        "const": "honey"
      }
    }
  },
  "then": {
    "properties": {
      "hi": {
        "type": "string"
      }
    },
    "required": ["hi"]
  }
}

main.json 应该通过尖叫 “hi”是必需的验证失败,因为 another.json 具有“honey”作为值。

现在,将 another.json 更改为

{
  "honey": "not_honey"
}

应该通过 main.json 的验证

问题是

【问题讨论】:

    标签: json validation conditional-statements schema jsonschema


    【解决方案1】:

    没有。 JSON Schema 没有文件的概念,只有“一个 JSON 实例”,一次只能验证一个。您需要自己以某种方式将它们组合起来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      相关资源
      最近更新 更多