json-schema 数组所需的设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17931874/
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
setting required on a json-schema array
提问by ipengineer
I am trying to figure out how to set requiredon my json-schema array of objects. The requiredproperty works fine on an object just not an array.
我想弄清楚如何设置required我的 json-schema 对象数组。该required属性在对象上工作正常,而不是数组。
Here is the items part of my json schema:
这是我的 json 架构的项目部分:
"items": {
"type": "array",
"properties": {
"item_id": {"type" : "number"},
"quantity": {"type": "number"},
"price": {"type" : "decimal"},
"title": {"type": "string"},
"description": {"type": "string"}
},
"required": ["item_id","quantity","price","title","description"],
"additionalProperties" : false
}
Here is the json array I am sending over. The json validation should fail since I am not passing a description in these items.
这是我发送的 json 数组。json 验证应该失败,因为我没有在这些项目中传递描述。
"items": [
{
"item_id": 1,
"quantity": 3,
"price": 30,
"title": "item1 new name"
},
{
"item_id": 1,
"quantity": 16,
"price": 30,
"title": "Test Two"
}
]
回答by theon
I got it to work using this validatorby nesting the part of the schema for the array elements inside a object with the name items. The schema now has two nested itemsfields, but that is because one is a keyword in JSONSchema and the other because your JSON actually has a field called items
我通过将数组元素的架构部分嵌套在名称为 的对象中,使其使用此验证器工作items。该架构现在有两个嵌套items字段,但这是因为一个是 JSONSchema 中的关键字,另一个是因为您的 JSON 实际上有一个名为的字段items
JSONSchema:
JSONSchema:
{
"type":"object",
"properties":{
"items":{
"type":"array",
"items":{
"properties":{
"item_id":{
"type":"number"
},
"quantity":{
"type":"number"
},
"price":{
"type":"number"
},
"title":{
"type":"string"
},
"description":{
"type":"string"
}
},
"required":[
"item_id",
"quantity",
"price",
"title",
"description"
],
"additionalProperties":false
}
}
}
}
JSON:
JSON:
{
"items":[
{
"item_id":1,
"quantity":3,
"price":30,
"title":"item1 new name"
},
{
"item_id":1,
"quantity":16,
"price":30,
"title":"Test Two"
}
]
}
Output with two errors about missing description fields:
输出有两个关于缺少描述字段的错误:
[ {
"level" : "error",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/items/items"
},
"instance" : {
"pointer" : "/items/0"
},
"domain" : "validation",
"keyword" : "required",
"message" : "missing required property(ies)",
"required" : [ "description", "item_id", "price", "quantity", "title" ],
"missing" : [ "description" ]
}, {
"level" : "error",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/items/items"
},
"instance" : {
"pointer" : "/items/1"
},
"domain" : "validation",
"keyword" : "required",
"message" : "missing required property(ies)",
"required" : [ "description", "item_id", "price", "quantity", "title" ],
"missing" : [ "description" ]
} ]
Try pasting the above into hereto see the same output generated.
尝试将上述内容粘贴到此处以查看生成的相同输出。
回答by theon
Maybe your validator only supports JSONSchema v3?
也许您的验证器仅支持 JSONSchema v3?
The way requiredworks changed between v3 and v4:
requiredv3 和 v4 之间的工作方式发生了变化:
- In v3
requiredis a boolean: http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5.7 - In v4
requiredis an array of strings (like in your example): http://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.4.3
- 在 v3 中
required是一个布尔值:http: //tools.ietf.org/html/draft-zyp-json-schema-03#section-5.7 - 在 v4 中
required是一个字符串数组(如您的示例中所示):http: //tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.4.3
回答by Greg Stumph
I realize this is an old thread, but since this question is linked from jsonschema.net, I thought it might be worth chiming in...
我意识到这是一个旧线程,但由于这个问题是从 jsonschema.net 链接的,我认为它可能值得一提......
The problem with your original example is that you're declaring "properties" for an "array" type, rather than declaring "items" for the array, and then declaring an "object" type (with "properties") that populates the array. Here's a revised version of the original schema snippet:
原始示例的问题在于,您为“数组”类型声明“属性”,而不是为数组声明“项”,然后声明填充数组的“对象”类型(带有“属性”) . 这是原始架构片段的修订版:
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"item_id": {"type" : "number"},
"quantity": {"type": "number"},
"price": {"type" : "decimal"},
"title": {"type": "string"},
"description": {"type": "string"}
},
"required": ["item_id","quantity","price","title","description"],
"additionalProperties" : false
}
}
I would recommend against using the term "items" for the name of the array, to avoid confusion, but there's nothing stopping you from doing that...
我建议不要使用术语“项目”作为数组的名称,以避免混淆,但没有什么可以阻止你这样做......

