【问题标题】:Nested allOf, anyOf, oneOf in json schema在 json 模式中嵌套 allOf、anyOf、oneOf
【发布时间】:2020-02-03 03:39:28
【问题描述】:

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

标准:

  1. 每个区域对象总是有一个属性为“区域”,并且可能有其他不同的属性和一个嵌套的 json 对象。 (请注意该对象没有相似的属性,所以我使用的是定义)
  2. 该数组必须包含至少一个区域对象,但只能是以下类型:澳大利亚或亚洲或欧洲以及其他区域类型的对象。 [澳大利亚或亚洲或欧洲不能共存]
  3. JSON 架构应该抱怨缺少必需的属性。

所以这个条件是有效的:

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

  1. [{"asia"}] //或欧洲或澳大利亚
  2. [{"some-pencil-region"},{"asia"}]
  3. [{"some-pencil-region"},{some-oil-pastels-region}, {"asia"}]
  4. [{some-oil-pastels-region}, {"europe"}]
  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"}] //缺少:亚洲、欧洲或澳大利亚应该与其他对象一起存在

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

【问题讨论】:

    标签: json jsonschema ajv


    【解决方案1】:

    我认为您需要为此使用 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"
           }
          }
        }
      }
    }
    

    【讨论】:

    • 这行得通。但是,如果我添加另一个定义,例如具有 2 个强制属性高度和宽度的“维度”,然后如果我将对象添加到数组中,架构验证不会抱怨缺少宽度属性。
    • 换句话说,如果我添加一个具有多个属性的新定义,包括任何有效区域的嵌套一加几个必需属性,架构应该验证 json。在架构中 {"dimension": {"$ref": "#/definitions/dimension"}} {"dimension":{"type":"object","re​​quired":["width"],"properties": {"height": {"type":"integer"},"width":{"type":"integer"}}}} 在 JSON {"type":"some-oil-pastels-region","height ":30}
    • 也许我不明白你试图做什么,但如果我从你的评论中添加维度定义,JSONBuddy 中的验证器确实会抱怨任何缺少的属性/属性。
    • 感谢您的回复。如果您看到链接 (LINK1),它会给出错误“不应有效”。我已经在问题中添加了链接,因为链接很大,可以容纳在评论部分。
    • 我想做的是JSON数组中的“亚洲、欧洲、澳大利亚相关对象不能与其他对象共存”。单个对象必须验证它们所需的属性。你的回答真的很接近我的问题。
    猜你喜欢
    • 2022-01-14
    • 1970-01-01
    • 2017-06-02
    • 2020-01-30
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多