繁体   English   中英

json 模式中的嵌套 allOf、anyOf、oneOf

[英]Nested allOf, anyOf, oneOf in json schema

这是我修改后的问题,我借用了我在其中一个问题中学到的相同知识。 我在这个问题的底部添加了我的架构和 JSON。

标准:

  1. 每个区域 object 将始终具有一个属性为“区域”,并且可能具有其他不同的属性和嵌套的 json object。 (请注意 object 没有类似的属性,所以我使用定义)
  2. 该阵列必须包含至少一个区域 object,但只能是以下类型:澳大利亚或亚洲或欧洲以及其他区域类型 object。 【澳亚欧不能共存】
  3. JSON Schema 应该抱怨缺少必需的属性。

所以这个条件是有效的:

这里的数组是 ["stat_data":{},{},{}]

  1. [{"asia"}] //或欧洲或澳大利亚
  2. [{"some-pencil-region"},{"asia"}]
  3. [{"some-pencil-region"},{some-oil-pastels-region}, {"asia"}]
  4. [{some-oil-pastels-region}, {“欧洲”}]
  5. [{"some-pencil-region"}, {"europe"}]

这个条件是无效的:

  1. []
  2. [{"some-pencil-region"},{"asia"},{"europe"} {australia}] //亚洲、欧洲、澳大利亚不能并存
  3. [{some-oil-pastels-region},{"some-pencil-region"},{"asia"},{"asia"}, {australia}] //亚洲、欧洲、澳大利亚不能并存
  4. [{"some-pencil-region"}] //缺少:亚洲或欧洲或澳大利亚应与其他 object 一起出现

JSON 架构

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Pencils": {
      "contains": {
        "properties": {
          "region": {
            "const": "some-pencil-region"
          },
          "details": {
            "type": "object",
            "required": [
              "brand",
              "year"
            ],
            "properties": {
              "brand": {
                "type": "string"
              },
              "year": {
                "type": "number"
              }
            }
          }
        }
      }
    },
    "OilPastels": {
      "contains": {
        "required": [
          "population"
        ],
        "properties": {
          "region": {
            "const": "some-oil-pastels-region"
          },
          "details": {
            "type": "object",
            "properties": {
              "size": {
                "type": "number"
              }
            }
          }
        }
      }
    },
    "containsAsia": {
      "contains": {
        "required": [
          "population"
        ],
        "properties": {
          "region": {
            "const": "asia"
          },
          "population": {
            "type": "object",
            "required": [
              "year"
            ],
            "properties": {
              "year": {
                "type": "number"
              },
              "change": {
                "type": "number"
              }
            }
          }
        }
      }
    },
    "containsEurope": {
      "contains": {
        "properties": {
          "region": {
            "const": "europe"
          },
          "tourist": {
            "type": "number"
          }
        }
      }
    },
    "containsAustralia": {
      "contains": {
        "properties": {
          "region": {
            "const": "australia"
          },
          "stadium": {
            "type": "object",
            "required": [
              "year"
            ],
            "properties": {
              "year": {
                "type": "number"
              },
              "area": {
                "type": "number"
              }
            }
          }
        }
      }
    }
  },
  "type": "object",
  "properties": {
    "stat_data": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object"
      },
      "oneOf": [
        {
          "$ref": "#/definitions/Pencils"
        },
        {
          "$ref": "#/definitions/OilPastels"
        },
        {
          "allOf": [
            {
              "$ref": "#/definitions/containsAsia"
            },
            {
              "not": {
                "$ref": "#/definitions/containsEurope"
              }
            },
            {
              "not": {
                "$ref": "#/definitions/containsAustralia"
              }
            }
          ]
        },
        {
          "allOf": [
            {
              "$ref": "#/definitions/containsEurope"
            },
            {
              "not": {
                "$ref": "#/definitions/containsAsia"
              }
            },
            {
              "not": {
                "$ref": "#/definitions/containsAustralia"
              }
            }
          ]
        },
        {
          "allOf": [
            {
              "$ref": "#/definitions/containsAustralia"
            },
            {
              "not": {
                "$ref": "#/definitions/containsAsia"
              }
            },
            {
              "not": {
                "$ref": "#/definitions/containsEurope"
              }
            }
          ]
        }
      ]
    }
  }
}

JSON(这是失败的)[我尝试了所有的验证,但都是徒劳的]

{
  "stat_data":[
    {
      "region":"some-pencil-region",
      "details":{
        "brand":"Camlin",
        "year": 2019
      }
    },
    {
      "region":"asia",
      "population":{
        "year":2018,
        "change":2
      }     
    }
  ]
}

10/06 未验证强制属性LINK1

我相信您需要为此使用 if-then-else 流程:

{
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
"properties": {
"stat_data": {
  "type": "array",
  "if": {
    "contains": {
      "type": "object",
      "properties": {
        "region": {
          "type": "string",
          "enum": [ "europe" ]
        }
      }
    }
  },
  "then": {
    "not": {
      "contains": {
        "type": "object",
        "properties": {
          "region": {
            "type": "string",
            "enum": [ "asia", "australia" ]
          }
        }
      }
    }
  },
  "else": {
    "if": {
      "contains": {
        "type": "object",
        "properties": {
          "region": {
            "type": "string",
            "enum": [ "asia" ]
          }
        }
      }
    },
    "then": {
      "not": {
        "contains": {
          "type": "object",
          "properties": {
            "region": {
              "type": "string",
              "enum": [ "europe", "australia" ]
            }
          }
        }
      }
    },
    "else": {
      "if": {
        "contains": {
          "type": "object",
          "properties": {
            "region": {
              "type": "string",
              "enum": [ "australia" ]
            }
          }
        }
      },
      "then": {
        "not": {
          "contains": {
            "type": "object",
            "properties": {
              "region": {
                "type": "string",
                "enum": [ "europe", "asia" ]
              }
            }
          }
        }
      },
      "else": {

      }
    }
  },
  "items": {
    "type": "object",
    "properties": {
      "details": {
        "$ref": "#/definitions/details"
      },
      "population": {
        "$ref": "#/definitions/population"
      },
      "region": {
        "enum": [ "asia", "europe", "australia", "some-pencil-region", "some-oil-pastels-region" ]
      }
    }
  }
 }
},
"definitions": {
  "details": {
  "type": "object",
  "properties": {
    "brand": {
      "type": "string"
    },
    "year": {
      "type": "integer"
    }
  }
},
"population": {
  "type": "object",
    "properties": {
      "change": {
        "type": "integer"
       },
       "year": {
        "type": "integer"
       }
      }
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM