我的 JSON 字符串的格式如下:
{
"count":3,
"data":[
{
"a":{"ax":1}
},
{
"b":{"bx":2}
},
{
"c":{"cx":4}
}
]
}
The data
数组包含很多a
and b
and c
。并且没有其他种类的物体。
If count==0
, data
应该是一个空数组[]
.
我在用着https://github.com/hoxworth/json-schema https://github.com/hoxworth/json-schema在 Ruby 中验证此类 JSON 对象。
require 'rubygems'
require 'json-schema'
p JSON::Validator.fully_validate('schema.json',"test.json")
The schema.json
is:
{
"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
once.
正确的模式应该是什么?