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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10809459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:21:38  来源:igfitidea点击:

How to write a JSON schema for array of objects?

rubyjsonjsonschema

提问by Green Su

My JSON string would be formatted as:

我的 JSON 字符串将被格式化为:

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

The dataarray contains many aand band c. And no other kinds of objects.

data阵列包含许多abc。并且没有其他种类的物体。

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

如果count==0,data应该是一个空数组[]

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

我正在使用https://github.com/hoxworth/json-schema在 Ruby 中验证此类 JSON 对象。

require 'rubygems'
require 'json-schema'

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

The schema.jsonis:

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

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

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

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

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

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

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

Seems the wrong schema is validating that the dataarray contains a,b,conce.

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

What the right schema should be?

正确的模式应该是什么?

回答by Confusion

From the JSON schema spec, section 5.5. items:

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

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.

当这个属性值是一个模式数组并且实例
值是一个数组时,实例数组中的每个位置都必须符合
该数组对应位置中的模式。这
称为元组类型。

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

您的架构定义要求数组的前三个元素恰好是“a”、“b”和“c”元素。如果items留空,则允许任何数组元素。同样,如果additionalItems留空,则允许任何额外的数组元素。

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

为了得到你想要的东西,你需要指定"additionalItems": falseitems,我认为以下(从你的定义中略有缩短)应该有效:

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