私の JSON 文字列は次のようにフォーマットされます。
{
"count":3,
"data":[
{
"a":{"ax":1}
},
{
"b":{"bx":2}
},
{
"c":{"cx":4}
}
]
}
data
配列には多くのとa
が含まれb
てい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 回含まれていることを検証しているようです。
適切なスキーマとは?