首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为JSON在anyOf中嵌套anyOf

为JSON在anyOf中嵌套anyOf
EN

Stack Overflow用户
提问于 2019-09-30 17:56:02
回答 1查看 3.1K关注 0票数 4

下面是JSON和JSON,如下所示,链接中提供了用于插画的内容。

JSON模式和JSON

格式:数组中的单个JSON对象(以及它们的附加属性和可能随数组中的其他对象而异)可以是任意3个区域:“america”、“asia”和“europe”,至少在区域对象的类型上应该存在。这可以通过数组minItems属性来实现)

问题陈述:

  1. 数组中的单个JSON对象可以是任意3个区域:“america”、“asia”和“europe”,至少在区域对象的类型上应该存在。 ==>我可以通过将所有的区域对象放到anyOf数组中来解决这个问题,因为我希望匹配至少一个有效的区域对象。
  2. JSON对象'asia‘或'europe’可以与其他区域类型一起存在。两者不能共存。 ==>我尝试使用'oneOf‘,但它通过了ajv验证。实际上它应该失败。有人能帮上忙吗。谢谢

JSON模式

代码语言:javascript
复制
{
    "type": "object",
    "properties": {
        "stat_data": {
            "type": "array",
            "minItems": 1,
            "items": {
                "type": "object",
                "properties": {},
                "anyOf": [{
                        "required": ["region"],
                        "properties": {
                            "region": {
                                "enum": ["america"]
                            },
                            "country": {
                                "type": "string"
                            },
                            "population": {
                                "type": "string"
                            }
                        }
                    },
                    {
                        "oneOf": [
                            {
                                "required": ["region"],
                                "properties": {
                                    "region": {
                                        "enum": ["asia"]
                                    },
                                    "country": {
                                        "type": "string"
                                    },
                                    "details": {
                                        "type": "object",
                                        "properties": {
                                            "language": {
                                                "type": "string"
                                            },
                                            "tz": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            }, {
                                "required": ["region"],
                                "properties": {
                                    "region": {
                                        "enum": ["europe"]
                                    },
                                    "country": {
                                        "type": "string"
                                    },
                                    "language": {
                                        "type": "string"
                                    }
                                }
                            }
                        ]
                    }
                ]
            }
        }
    }
}

JSON反对失败,因为“亚洲”和“欧洲”类型的对象不能共存。

代码语言:javascript
复制
{
    "stat_data": [{
            "region": "america",
            "country": "USA",
            "states": "50"
        }, {
            "region": "asia",
            "country": "Japan",
            "details": {
                "language": "Japanese",
                "tz": "utc+9.00"
            }
        }, {
            "region": "europe",
            "country": "finland",
            "language": "Finnish"
        }
    ]
}

JSON对象作为只有“亚洲”类型的对象存在。

代码语言:javascript
复制
{
    "stat_data": [{
            "region": "america",
            "country": "USA",
            "states": "50"
        }, {
            "region": "asia",
            "country": "Japan",
            "details": {
                "language": "Japanese",
                "tz": "utc+9.00"
            }
        }
    ]
}

JSON对象作为只传递“欧洲”类型的对象存在。

代码语言:javascript
复制
{
    "stat_data": [{
            "region": "america",
            "country": "USA",
            "states": "50"
        }, {
            "region": "europe",
            "country": "finland",
            "language": "Finnish"
        }
    ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-01 11:51:34

我可以理解为什么您尝试了这种方法,但是它并不像预期的那样工作,因为您已经定义了数组中的每个项可能是america或(europeasia),这不是您想要的。

记住,items将值模式应用于数组中的每个元素。它对整个数组本身没有任何限制。contains检查数组中至少有一个项对其值架构进行验证。

您想要说的是,数组中的每一项都可能有americaeuropeasia,但是如果数组中包含asia,则数组可能不包含europe,反之则不包含europe

我重构了模式并做了一些更改。

希望您也能看到使用oneOf >> (containsnot > contains)的意图是明确的。

JSON模式通过添加约束来工作。通常不能通过省略来定义约束。

JSON模式和数据验证演示

代码语言:javascript
复制
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "containtsAsia": {
      "contains": {
        "properties": {
          "region": {
            "const": "asia"
          }
        }
      }
    },
    "containsEurope": {
      "contains": {
        "properties": {
          "region": {
            "const": "europe"
          }
        }
      }
    }
  },
  "type": "object",
  "properties": {
    "stat_data": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "properties": {
          "region": {
            "enum": [
              "america",
              "asia",
              "europe"
            ]
          },
          "country": {
            "type": "string"
          },
          "population": {
            "type": "string"
          }
        }
      },
      "oneOf": [
        {
          "allOf": [
            {
              "$ref": "#/definitions/containtsAsia"
            },
            {
              "not": {
                "$ref": "#/definitions/containsEurope"
              }
            }
          ]
        },
        {
          "allOf": [
            {
              "$ref": "#/definitions/containsEurope"
            },
            {
              "not": {
                "$ref": "#/definitions/containtsAsia"
              }
            }
          ]
        }
      ]
    }
  }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58172931

复制
相关文章

相似问题

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