更正不同类型项目数组的 JSON 模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15396251/
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
Correct JSON Schema for an array of items of different type
提问by deepwinter
I have an unordered array of JSON items. According to the specification http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.5the json schema below will only validate if the objects in the array appear IN THAT ORDER. I don't want to specify an order, just validate the objects within the array, regardless of order or number of objects. From the spec I can't seem to understand how this is done.
我有一个无序的 JSON 项目数组。根据规范http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.5下面的 json 模式只会验证数组中的对象是否按该顺序出现。我不想指定顺序,只需验证数组中的对象,而不管对象的顺序或数量。从规范中我似乎无法理解这是如何完成的。
"transactions" : {
"type" : "array",
"items" : [
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BUILD", "REASSIGN"]
}
}
},
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BREAK"]
}
}
}
]
}
回答by deepwinter
I asked this same question on the JSON schema google group, and it was answered quickly. User fge asked that I post his response here:
我在 JSON schema google group 上问了同样的问题,很快就得到了回答。用户 fge 要求我在这里发布他的回复:
Hello,
The current specification is draft v4, not draft v3. More specifically, the validation specification is here:
http://tools.ietf.org/html/draft-fge-json-schema-validation-00
The web site is not up to date, I don't know why... I'll submit a pull request.
With draft v4 you can use this:
你好,
当前的规范是 v4 草案,而不是 v3 草案。更具体地说,验证规范在这里:
http://tools.ietf.org/html/draft-fge-json-schema-validation-00
该网站不是最新的,我不知道为什么...我将提交拉取请求。
使用draft v4,您可以使用它:
{
"type": "array",
"items": {
"oneOf": [
{"first": [ "schema", "here" ] },
{"other": [ "schema": "here" ] }
]
}
}
For instance, this is a schema for an array where items can be either strings or integers (it can be written in a more simple way though):
例如,这是一个数组的模式,其中项可以是字符串或整数(但可以用更简单的方式编写):
{
"type": "array",
"items": {
"oneOf": [
{"type": "string"},
{"type": "integer"}
]
}
}
This is the correct answer. My corrected schema now includes:
这是正确答案。我更正后的架构现在包括:
"transactions" : {
"type" : "array",
"items" : {
"oneOf" : [
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BUILD", "REASSIGN"]
}
}
},
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BREAK"]
}
}
}
]
}
}
回答by Jonas Kac
I've been looking into this for quite a while too. But haven't been able to find a working solution. It works fine if you have only one schema eg.
我也一直在研究这个问题。但一直无法找到有效的解决方案。如果您只有一个架构,例如,它可以正常工作。
"transactions" : {
"type" : "array",
"items" :
{
"type" : "object",
"properties" : {
"type" : {
"type" : "string",
"enum" : ["BREAK"]
},
}
}
Then you just skip the array brackets, and use an object. However if you want to do what you are doing, there seems to be no solid answer. This is the only thing that I've found so far: http://the-long-dark-tech-time.blogspot.se/2012/12/using-json-schema-with-array-of-mixed.html
然后你只需跳过数组括号,并使用一个对象。然而,如果你想做你正在做的事情,似乎没有可靠的答案。这是迄今为止我发现的唯一一件事:http: //the-long-dark-tech-time.blogspot.se/2012/12/using-json-schema-with-array-of-mixed.html
回答by Vdex
For anyone stuck with the draft 3 schema. There is a "Type" keyword that is equivalent to the "anyOf" in draft 4:
对于任何坚持草案 3 模式的人。有一个“Type”关键字相当于草稿 4 中的“anyOf”:
So you can use
所以你可以使用
{
"fooBar" : {
"type" : "array",
"items" : {
"type" : [{
"type" : "object",
"properties" : {
"foo" : {
"type" : "string"
}
}
}, {
"type" : "object",
"properties" : {
"bar" : {
"type" : "string"
}
}
}
]
}
}
}
回答by Cwellan
As a response to user Vdex:this is not equivalent, what you wrote means that array elements occur in this particular orderwithin the array.
作为对用户 Vdex 的回应:这不是等价的,你写的意思是数组元素在数组中以这个特定的顺序出现。
Subject to a correct implementation, if you use this schema validator.
With this schema:
使用此架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "boolean"
},
{
"type": "number"
},
{
"type": "string"
}
]
}
This JSON will be validated:
将验证此 JSON:
[
true,
5,
"a",
"6",
"a",
5.2
]
But not this one:
但不是这个:
[
5,
true,
"a",
"6",
"a",
5.2
]
Thus, the objective is totally different from keywords like "oneOf".
因此,目标与像“oneOf”这样的关键字完全不同。
回答by Phat Dang
In my case, I want the first element in the array has a specific format, the remain elements have another format. This is my solution:
就我而言,我希望数组中的第一个元素具有特定格式,其余元素具有另一种格式。这是我的解决方案:
my_schema = {
"type": "object",
"properties": {
"token": {"type": "string"},
"service_id": {"type": "string"},
"promo_code": {"type": "string"},
"path": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"address": {"type": "string"},
"lat": {"type": "number"},
"lng": {"type": "number"}
},
"required": ["address", "lat", "lng"]
},
{
"type": "object",
"properties": {
"address": {"type": "string"},
"lat": {"type": "number"},
"lng": {"type": "number"},
"district_id": {"type": "number"},
"ward_code": {"type": "number"},
"weight": {"type": "number"}
},
"required": ["address","lat", "lng","ward_code",
"district_id", "weight"]
}
]
}
},
"required": ["token", "service_id", "path"]
}
The above schema means that from the second element of the path, I required the district_id, the ward_code, the weight
上面的模式意味着从路径的第二个元素开始,我需要 District_id、ward_code、权重

