为不同类型的项数组更正JSON架构

为不同类型的项数组更正JSON架构,json,validation,jsonschema,Json,Validation,Jsonschema,我有一个无序的JSON项目数组。根据规范,只有当数组中的对象按该顺序出现时,下面的json模式才会验证。我不想指定顺序,只需验证数组中的对象,而不管对象的顺序或数量。从规范来看,我似乎无法理解这是如何做到的 "transactions" : { "type" : "array", "items" : [ { "type" : "object", "properties" : { "ty

我有一个无序的JSON项目数组。根据规范,只有当数组中的对象按该顺序出现时,下面的json模式才会验证。我不想指定顺序,只需验证数组中的对象,而不管对象的顺序或数量。从规范来看,我似乎无法理解这是如何做到的

"transactions" : {
    "type" : "array",
    "items" : [
        {
            "type" : "object",
            "properties" : {
                "type" : {
                    "type" : "string",
                    "enum" : ["BUILD", "REASSIGN"]
                }
            }
        },
        {
            "type" : "object",
            "properties" : {
                "type" : {
                    "type" : "string",
                    "enum" : ["BREAK"]
                }
            }
        }
    ]
}

我也已经调查这件事很久了。但是还没有找到有效的解决方案。如果只有一个模式,它就可以正常工作

"transactions" : {
          "type" : "array",
          "items" : 
          {
            "type" : "object",
            "properties" : {
              "type" : {
                "type" : "string",
                "enum" : ["BREAK"]
              },
          }
}

然后跳过数组括号,使用一个对象。然而,如果你想做你正在做的事情,似乎没有可靠的答案。这是迄今为止我发现的唯一一件事:

我在JSON模式google组上问了同样的问题,很快得到了回答。用户fge要求我在此处发布他的回复:

你好,

当前规范是草案v4,而不是草案v3。更多 具体而言,验证规范如下:

网站不是最新的,我不知道为什么。。。我将提交一份提拉 请求

对于草稿v4,您可以使用以下内容:

例如,这是一个数组的模式,其中的项可以是 字符串或整数(但可以用更简单的方式编写):

这是正确的答案。我更正的模式现在包括:

"transactions" : {
    "type" : "array",
    "items" : {
        "oneOf" : [
            {
                "type" : "object",
                "properties" : {
                    "type" : {
                        "type" : "string",
                        "enum" : ["BUILD", "REASSIGN"]
                    }
                }
            },
            {
               "type" : "object",
               "properties" : {
                 "type" : {
                   "type" : "string",
                   "enum" : ["BREAK"]
                  }
               }
            }
        ]
    }
}

对于任何坚持草案3模式的人。有一个“Type”关键字与草案4中的“anyOf”相同:

所以你可以用

{
    "fooBar" : {
        "type" : "array",
        "items" : {
            "type" : [{
                    "type" : "object",
                    "properties" : {
                        "foo" : {                           
                            "type" : "string"
                        }
                    }
                }, {
                    "type" : "object",
                    "properties" : {
                        "bar" : {
                            "type" : "string"
                        }
                    }
                }
            ]
        }
    }
}
作为对用户Vdex的响应:这是不等价的,您所写的内容意味着数组元素在数组中以这个特定顺序出现

根据正确的实施,如果您使用

使用此模式:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": [
    {
      "type": "boolean"
    },
    {
      "type": "number"
    },
    {
      "type": "string"
    }
  ]
}
将通过以下方式验证此JSON:

[
  true,
  5,
  "a",
  "6",
  "a",
  5.2
]
但不是这个:

[
  5,
  true,
  "a",
  "6",
  "a",
  5.2
]

因此,目标与“其中之一”等关键词完全不同

在我的例子中,我希望数组中的第一个元素具有特定格式,其余元素具有另一种格式。这是我的解决方案:

my_schema = {
    "type": "object",
    "properties": {
        "token": {"type": "string"},
        "service_id": {"type": "string"},
        "promo_code": {"type": "string"},
        "path": {
            "type": "array",
            "items": [
                {
                    "type": "object",
                    "properties": {
                        "address": {"type": "string"},
                        "lat": {"type": "number"},
                        "lng": {"type": "number"}
                    },
                    "required": ["address", "lat", "lng"]
                },
                {
                    "type": "object",
                    "properties": {
                        "address": {"type": "string"},
                        "lat": {"type": "number"},
                        "lng": {"type": "number"},
                        "district_id": {"type": "number"},
                        "ward_code": {"type": "number"},
                        "weight": {"type": "number"}
                    },
                    "required": ["address","lat", "lng","ward_code", 
                                 "district_id", "weight"]
                }
            ]
        }
    },
    "required": ["token", "service_id", "path"]
}

上面的模式意味着从路径的第二个元素开始,我需要区号、区号、权重

这个JSON从一开始就不是有效的。你能告诉我具体的无效部分吗?这是一个更大的JSON模式文件的摘录,该文件本身可以很好地传递JSON lint。也许有一个我看不到的打字错误?我认为这不值得投反对票-你可以建议编辑。发现无效性-当我从更大的文件中摘录JSON时的副作用。最好你发布固定版本作为你的答案(如果这解决了你的问题),如果将来有人问这个问题,你也可以使用这个网站来检查你的JSON是否存在问题:对我来说,这并没有对路径数组中第二个对象之后的对象应用模式。
[
  5,
  true,
  "a",
  "6",
  "a",
  5.2
]
my_schema = {
    "type": "object",
    "properties": {
        "token": {"type": "string"},
        "service_id": {"type": "string"},
        "promo_code": {"type": "string"},
        "path": {
            "type": "array",
            "items": [
                {
                    "type": "object",
                    "properties": {
                        "address": {"type": "string"},
                        "lat": {"type": "number"},
                        "lng": {"type": "number"}
                    },
                    "required": ["address", "lat", "lng"]
                },
                {
                    "type": "object",
                    "properties": {
                        "address": {"type": "string"},
                        "lat": {"type": "number"},
                        "lng": {"type": "number"},
                        "district_id": {"type": "number"},
                        "ward_code": {"type": "number"},
                        "weight": {"type": "number"}
                    },
                    "required": ["address","lat", "lng","ward_code", 
                                 "district_id", "weight"]
                }
            ]
        }
    },
    "required": ["token", "service_id", "path"]
}