首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json模式中的嵌套allOf、anyOf、oneOf

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

Stack Overflow用户
提问于 2019-10-04 19:54:58
回答 1查看 2.4K关注 0票数 1

这是我修改过的问题,我借用的是我在其中一个问题中学到的同样的知识。我在这个问题的末尾添加了我的模式& JSON。

条件:

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

所以这个条件是有效的:

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

  1. {“亚洲”}/或欧洲或澳大利亚
  2. {“某些铅笔区域”},{“亚洲”}
  3. {“某些-铅笔区域”},{某些-油彩-区域},{“亚洲”}
  4. {一些-油彩-区域},{“欧洲”}
  5. {“某些铅笔区域”},{“欧洲”}

这个条件是无效的:

  1. []
  2. {“某些铅笔区域”},{“亚洲”},{“欧洲”}{澳大利亚}//亚洲,欧洲,澳大利亚不能共同退出。
  3. {一些-油彩-区域},{“某些-铅笔-区域”},{“亚洲”},{“亚洲”},{澳大利亚}//亚洲,欧洲,澳大利亚不能共同退出。
  4. {“某些铅笔区域”}//缺失:亚洲或欧洲或澳大利亚应与其他物体一起存在。

JSON模式

代码语言:javascript
复制
{
  "$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 (这是失败的)我尝试了我所有的验证,但都失败了。

代码语言:javascript
复制
{
  "stat_data":[
    {
      "region":"some-pencil-region",
      "details":{
        "brand":"Camlin",
        "year": 2019
      }
    },
    {
      "region":"asia",
      "population":{
        "year":2018,
        "change":2
      }     
    }
  ]
}

10/06不验证强制属性LINK1

EN

回答 1

Stack Overflow用户

发布于 2019-10-05 07:49:24

我认为您需要使用一个if-然后-否则的流来实现这个目的:

代码语言:javascript
复制
{
"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"
       }
      }
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58242502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档