JSON Schema:使用另一个对象中定义的属性值

2023-12-27

我在 JSON 模式中有两个属性:
"A": {"type": "object", "properties":{ "X":{"type":"boolean"}}}
"B": {"type": "object", "properties":{...}}

在逻辑上B我需要使用的值X to set B财产价值和"required":[...].
我如何使用价值XB ?


您需要在架构级别声明一个规则来定义包含这两个属性的对象。然后您可以使用dependentSchemas, dependentRequired, or if/then/else关键字来定义规则,例如“if in property A, then in property B”。

https://json-schema.org/understanding-json-schema/reference/conditionals.html https://json-schema.org/understanding-json-schema/reference/conditionals.html

例如:

{
  "type": "object",
  "properties": {
    "A": {
      "type": "object",
      "properties":{ "X":{"type":"boolean"}}
    },
    "B": {
      "type": "object",
      "properties":{...}
    }
  },
  "if": {
    "required": ["A"],
    "properties": {
      "A": {
        "required": ["X"],
        "properties": { "X": { "const": true } }
      }
    }
  },
  "then": {
    "required": ["B"],
    "properties": {
      "B": {
        "required": ["Y"],
        "properties": { "Y": { "const": "/A/X is true" } }
      }
    }
  }
}

这就是说“如果属性 A 存在,并且有一个为 true 的子属性 X,那么 B 下一定有一个属性 Y,它是字符串“/A/X is true””。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JSON Schema:使用另一个对象中定义的属性值 的相关文章

随机推荐