验证JSON模式中的子级

时间:2019-03-17 23:38:48

标签: json validation schema json-schema-validator

我有以下json:

{
  "firstName": "Mac",
  "countryOfBirth": "England",
  "cityOfBirth": "England",
  "nested": {
    "testValue": "someValue"
  }
}

我想暗示架构中的以下条件:

if countryOfBirth = "England" then nested.testValue should be present

我尝试了以下模式定义,但它根本无法正常工作。所需条件尝试在countryBirth的同一级别而不是嵌套级别中查找该元素。知道如何实现吗?

{
  "$schema": "http://json-schema.org/draft-07/schema#",

  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "countryOfBirth": {
      "type": "string"
    },
    "cityOfBirth": {
      "type": "string"
    },
    "nested": {
      "type": "object",
      "properties": {
        "testValue": {
          "$id": "properties/nested/properties/testValue",
          "type": "string"
        }
      }
    }
  },
  "required": ["countryOfBirth"],
  "if": {"properties": {"countryOfBirth": {"pattern": "^England$"}}},
  "then": {"required": ["cityOfBirth","#/properties/nested/properties/testValue"]
  }
} 

0 个答案:

没有答案