【问题标题】:Json Schema - How to make anyOf two or more properties requiredJson Schema - 如何使任何两个或多个属性成为必需
【发布时间】:2016-12-15 19:49:59
【问题描述】:

我有这个父架构:

{
    "definitions": {
        "parcel": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                },
                "accountNumber": {
                    "type": "string"
                },
                "parcelNumber": {
                    "type": "string"
                },
                "propertyType": {
                    "type": "string"
                },
                "address": {
                    "$ref": "address.json#/definitions/address"
                },
                "coordinates": {
                    "$ref": "coordinates.json#/definitions/coordinates"
                }
            },
            "required": ["accountNumber", "parcelNumber"]
        }
    }
}

以下是引用的子模式:

{
    "definitions": {
        "address": {
            "type": "object",
            "properties": {
                "addressString": {
                    "type": "string",
                    "addressType": {
                        "enum": ["residential", "business"]
                    }
                },
                "required": ["addressString"]
            }
        }
    }
}

    {
    "definitions": {
        "coordinates": {
            "type": "object",
            "properties": {
                "latitude": {
                    "type": "number"
                },
                "longitude": {
                    "type": "number"
                },
                "projection": {
                    "type": "string"
                }
            },
            "required": ["latitude ", "longitude", " projection"]
        }
    }
}

我想将以下条件应用于父架构。

  1. 地址或坐标或两者都提供。
  2. 如果既没有提供地址也没有提供坐标,它应该无法通过验证。

【问题讨论】:

  • 小注释:您可以在架构中使用单个定义对象,包含所有三个子架构。它们不需要位于单独的对象中。
  • 另一个问题:我认为你的“id”属性属于“properties”

标签: json jsonschema


【解决方案1】:

您的anyOf 解决方案有效。您可以通过将固定的必需属性(accountNumber 和 parcelNumber)与 anyOf 属性组分开来使其更简洁:

{
  "type": "object",
  "required": [
    "accountNumber",
    "parcelNumber"
  ],
  "anyOf": [
    {"required" : ["address"]},
    {"required" : ["coordinates"]}
  ],
  "properties": {
    "id": {
      "type": "string"
    },
    "accountNumber": {
      "type": "string"
    },
    "parcelNumber": {
      "type": "string"
    },
    "propertyType": {
      "type": "string"
    },
    "address": {
      "type": "object",
      "properties": {
        "addressString": {
          "type": "string"
        },
        "addressType": {
          "enum": [
            "residential",
            "business"
          ]
        }
      },
      "required": [
        "addressString"
      ]
    },
    "coordinates": {
      "type": "object",
      "properties": {
        "latitude": {
          "type": "number"
        },
        "longitude": {
          "type": "number"
        },
        "projection": {
          "type": "string"
        }
      },
      "required": [
        "latitude",
        "longitude",
        "projection"
      ]
    }
  }
}

这里有一个要点供参考:

http://jsonschemalint.com/#/version/draft-05/markup/json?gist=f36d9a7e080c4d25dbbf09b7dd03137e

【讨论】:

    【解决方案2】:

    这就是我实施解决方案的方式。

    "anyOf": [{
        "required": ["accountNumber", "parcelNumber", "coordinates"]
    }, {
        "required": ["accountNumber", "parcelNumber", "address"]
    }]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 2015-04-01
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      相关资源
      最近更新 更多