更正不同类型的项目数组的JSON模式

时间:2013-03-13 20:53:55

标签: json validation jsonschema

我有一个无序的JSON项目数组。根据规范http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.5,下面的json模式将仅验证数组中的对象是否显示为IN THAT ORDER。我不想指定一个订单,只需验证数组中的对象,无论对象的顺序或数量如何。根据规范,我似乎无法理解这是如何完成的。

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

5 个答案:

答案 0 :(得分:41)

我在JSON架构google群组中提出了同样的问题,并且很快得到了解答。用户fge要求我在这里发布他的回复:

  

你好,

     

目前的规范是草案v4,而不是草案v3。更多   具体来说,验证规范在这里:

     

http://tools.ietf.org/html/draft-fge-json-schema-validation-00

     

该网站不是最新的,我不知道为什么......我会提交一个拉   请求。

     

使用草案v4,您可以使用:

{
    "type": "array",
    "items": {
        "oneOf": [
            {"first": [ "schema", "here" ] }, 
            {"other": [ "schema": "here" ] }
        ]
    }  
}
  

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

{
    "type": "array",
    "items": {
        "oneOf": [
            {"type": "string"},
            {"type": "integer"}
        ]
    }
}

这是正确的答案。我更正后的架构现在包括:

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

答案 1 :(得分:3)

我也一直在研究这个问题。但一直未能找到有效的解决方案。如果你只有一个模式,它工作正常,例如。

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

然后你跳过数组括号,并使用一个对象。但是,如果你想做你正在做的事情,似乎没有可靠的答案。这是我到目前为止唯一发现的事情:http://the-long-dark-tech-time.blogspot.se/2012/12/using-json-schema-with-array-of-mixed.html

答案 2 :(得分:1)

对于那些坚持草案3架构的人。有一个“Type”关键字相当于草案4中的“anyOf”:

所以你可以使用

{
    "fooBar" : {
        "type" : "array",
        "items" : {
            "type" : [{
                    "type" : "object",
                    "properties" : {
                        "foo" : {                           
                            "type" : "string"
                        }
                    }
                }, {
                    "type" : "object",
                    "properties" : {
                        "bar" : {
                            "type" : "string"
                        }
                    }
                }
            ]
        }
    }
}

答案 3 :(得分:1)

作为对用户Vdex的响应:这不等同,您所写的内容意味着数组元素在数组中以此特定顺序出现。

如果您使用this schema validator

,请遵循正确的实施

使用此架构:

{
  "$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
]

因此,目标与“oneOf”等关键字完全不同。

答案 4 :(得分:0)

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

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"]
}

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