2013-03-13 66 views
12

我有一个无序的JSON项目数组。根据规范http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.5,下面的json模式将只验证数组中的对象是否出现在该命令中。我不想指定一个订单,只需验证数组中的对象,而不管对象的顺序或数量。从规格我似乎无法理解这是如何完成的。正确的JSON模式为不同类型的项目数组

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

那么,这个JSON从一开始就不是有效的。 – 2013-03-27 21:28:57

+0

你能指点我具体的无效部分吗?这是一个更大的JSON模式文件的摘录,它本身通过json lint很好。也许有一个我看不到的错字?我认为这不值得赞扬 - 你可以建议编辑。 – deepwinter 2013-03-28 02:28:14

+0

发现无效 - 当我从较大的文件中摘录JSON时的副作用。 – deepwinter 2013-03-28 02:40:29

回答

27

我在JSON架构的谷歌组中询问了同样的问题,并很快得到了回复。用户FGE问我在这里发表他的回应:

你好,

当前的规范是V4草案,草案没有V3。更 具体而言,验证规范是在这里:

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

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

随着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"] 
        } 
       } 
      } 
     ] 
    } 
} 
2

我也一直在寻找这一点。但一直未能找到工作解决方案。它工作正常,如果你只有一个模式,例如。

"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

对于任何卡在草案3模式中的人。有一个“类型”的关键字是在草案4等同于“anyOf”:

所以,你可以使用

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

至于用户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”。