मेरे JSON स्ट्रिंग स्वरूपित किया जाएगा के रूप में:वस्तुओं की सरणी के लिए JSON स्कीमा कैसे लिखें?
{
"count":3,
"data":[
{
"a":{"ax":1}
},
{
"b":{"bx":2}
},
{
"c":{"cx":4}
}
]
}
data
सरणी कई a
और b
और c
शामिल हैं। और कोई अन्य प्रकार की वस्तुएं नहीं।
यदि count==0
, data
एक खाली सरणी []
होना चाहिए।
मैं रूबी में ऐसे JSON ऑब्जेक्ट्स को सत्यापित करने के लिए https://github.com/hoxworth/json-schema का उपयोग कर रहा हूं।
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
एक बार है।
सही स्कीमा क्या होना चाहिए?
@Phrogz मैं सवाल को अद्यतन किया है। आप उदाहरण देख सकते हैं। –