如何为对象数组编写JSON模式?

时间:2012-05-30 03:01:23

标签: ruby json jsonschema

我的JSON字符串格式为:

{
    "count":3,
    "data":[
        {
            "a":{"ax":1}
        },
        {
            "b":{"bx":2}
        },
        {
            "c":{"cx":4}
        }
    ]
}

data数组包含许多ab以及c。而且没有其他类型的对象。

如果count==0,则data应为空数组[]

我正在使用https://github.com/hoxworth/json-schema来验证Ruby中的这些JSON对象。

require 'rubygems'
require 'json-schema'

p JSON::Validator.fully_validate('schema.json',"test.json")

schema.json是:

{
  "type":"object",
  "$schema": "http://json-schema.org/draft-03/schema",
  "required":true,
  "properties":{
     "count": { "type":"number", "id": "count", "required":true },
     "data": { "type":"array", "id": "data", "required":true,
       "items":[
           { "type":"object", "required":false, "properties":{ "a": { "type":"object", "id": "a", "required":true, "properties":{ "ax": { "type":"number", "id": "ax", "required":true } } } } },
           { "type":"object",  "required":false, "properties":{ "b": { "type":"object", "id": "b", "required":true, "properties":{ "bx": { "type":"number", "id": "bx", "required":true } } } } },
           { "type":"object",  "required":false, "properties":{ "c": { "type":"object", "id": "c", "required":true, "properties":{ "cx": { "type":"number", "id": "cx", "required":true } } } } }
       ]
     }
  }
}

但是test.json的这个将通过验证,而我认为它应该失败:

{
  "count":3,
  "data":[
      {
          "a":{"ax":1}
      },
      {
          "b":{"bx":2}
      },
      {
          "c":{"cx":2}
      },
      {
          "c": {"z":"aa"}
      }
   ]
}

这个test.json会失败,而我认为它应该通过:

{
  "count":3,
  "data":[
      {
          "a":{"ax":1}
      },
      {
          "b":{"bx":2}
      }
   ]
}

似乎错误的架构验证data数组包含a,b,c一次。

正确的架构应该是什么?

1 个答案:

答案 0 :(得分:30)

来自JSON schema spec,第5.5节。项目:

  

当此属性值是模式数组和实例
时   value是一个数组,实例数组中的每个位置必须符合   到此数组的相应位置的架构。这
  称为元组打字。

您的架构定义要求数组的前三个元素恰好是'a','b'和'c'元素。如果items保留为空,则允许任何数组元素。同样,如果additionalItems保留为空,则允许任何其他数组元素。

要获得您想要的内容,您需要指定"additionalItems": false,而对于items,我认为以下内容(稍微缩短了您的定义)应该有效:

"items": {
  "type": [
     {"type":"object", "properties": {"a": {"type": "object", "properties": {"ax": { "type":"number"}}}}},
     {"type":"object", "properties": {"b": {"type": "object", "properties": {"bx": { "type":"number"}}}}},
     {"type":"object", "properties": {"c": {"type": "object", "properties": {"cx": { "type":"number"}}}}}
  ]
}
相关问题