如何为对象数组编写JSON模式? [英] How to write a JSON schema for array of objects?

查看:93
本文介绍了如何为对象数组编写JSON模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON字符串的格式为:

My JSON string would be formatted as:

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

data数组包含许多abc.而且没有其他种类的对象.

The data array contains many a and b and c. And no other kinds of objects.

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

If count==0, data should be an empty array [].

我正在使用 https://github.com/hoxworth/json-schema 来验证以下JSON对象露比.

I'm using https://github.com/hoxworth/json-schema to validate such JSON objects in Ruby.

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来说,它将通过验证,而我认为它应该失败:

But this for test.json will pass the validation while I suppose it should fail:

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

test.json会失败,而我想它应该通过:

And this as test.json will fail, while I suppose it should pass:

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

似乎错误的模式正在验证data数组包含一次a,b,c.

Seems the wrong schema is validating that the data array contains a,b,c once.

正确的架构应该是什么?

What the right schema should be?

推荐答案

来自 JSON模式规范,第5.5节.项目:

From the JSON schema spec, section 5.5. items:

当此属性值是模式和实例的数组时
值是一个数组,实例数组中的每个位置都必须符合
到该数组在相应位置的模式.这个
称为元组键入.

When this attribute value is an array of schemas and the instance
value is an array, each position in the instance array MUST conform
to the schema in the corresponding position for this array. This
called tuple typing.

您的模式定义要求数组的前三个元素恰好是"a","b"和"c"元素.如果items保留为空,则允许使用任何数组元素.同样,如果additionalItems保留为空,则允许任何其他数组元素.

Your schema definition requires the first three elements of the array to be exactly those 'a', 'b' and 'c' elements. If items is left empty, any array element is allowed. Similarly, if additionalItems is left empty, any additional array element is allowed.

要获得所需的内容,您需要指定"additionalItems": false,对于items,我认为以下内容(比您的定义略有缩短)应该起作用:

To get what you want, you need to specify "additionalItems": false and for the items, I think the following (somewhat shortened from your definitions) should work:

"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"}}}}}
  ]
}

这篇关于如何为对象数组编写JSON模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆