【问题标题】:json schema - field is required based on another field valuejson 模式 - 基于另一个字段值的字段是必需的
【发布时间】:2017-06-19 11:52:55
【问题描述】:

我正在创建一个 JsonSchema(v4)。 我正在尝试根据其父级的另一个属性的值来使一个属性成为必需。

父母

  • 用户
    • 亚型
    • 地址

孩子

  • 地址
    • 第一行
    • 第2行
    • companyName(如果用户子类型是公司,则为必填)

如何做到这一点? 我现在有这样的东西......

{
 "User": {
  "title": "User",
  "type": "object",
  "id": "#User",
  "properties": {
     "subtype": {
      "type": "string"
     },
     "address": {
      "$ref": "Address"
     }
  }
 }


 "Address": {
  "title": "Address",
  "type": "object",
  "id": "#Address",
  "properties": {
     "line1": {
      "type": "string"
     },
     "line2": {
      "type": "string"
     },
     "companyName": {
      "type": "string"
     }
   },
   "required": ["line1", "line2"]
 }
}

子类型是任意字符串,因此不可能列出不同子类型的完整列表。

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    将此添加到您的用户架构中。本质上它读作:“子类型”不是“公司”或“地址”需要“公司名称”。

    "anyOf": [
      {
        "not": {
          "properties": {
            "subtype": { "enum": ["company"] }
          }
        }
      },
      {
        "properties": {
          "address": {
            "required": ["companyName"]
          }
        }
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 2015-01-21
      • 2019-04-14
      • 2017-05-08
      • 2015-04-09
      • 1970-01-01
      • 2016-10-17
      • 2020-09-30
      • 1970-01-01
      • 2010-11-13
      相关资源
      最近更新 更多