20

我想弄清楚如何设置required我的 json-schema 对象数组。该required属性适用于对象而不是数组。

这是我的 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
    }

这是我发送过来的 json 数组。json 验证应该失败,因为我没有在这些项目中传递描述。

       "items": [
        {
            "item_id": 1,
            "quantity": 3,
            "price": 30,
            "title": "item1 new name"
        },
        {
            "item_id": 1,
            "quantity": 16,
            "price": 30,
            "title": "Test Two"
        }
    ]
4

3 回答 3

26

我通过将数组元素的模式部分嵌套在一个名为items. 模式现在有两个嵌套items字段,但这是因为一个是 JSONSchema 中的关键字,另一个是因为您的 JSON 实际上有一个名为items

JSON模式:

{
   "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:

{
   "items":[
      {
         "item_id":1,
         "quantity":3,
         "price":30,
         "title":"item1 new name"
      },
      {
         "item_id":1,
         "quantity":16,
         "price":30,
         "title":"Test Two"
      }
   ]
}

输出有关缺少描述字段的两个错误:

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

尝试将以上内容粘贴到此处以查看生成的相同输出。

于 2013-07-30T07:25:55.107 回答
8

也许您的验证器仅支持 JSONSchema v3?

requiredv3 和 v4 之间的工作方式发生了变化:

于 2013-07-29T20:29:49.317 回答
8

我意识到这是一个旧线程,但由于这个问题是从 jsonschema.net 链接的,我认为它可能值得加入......

原始示例的问题是您正在为“数组”类型声明“属性”,而不是为数组声明“项目”,然后声明填充数组的“对象”类型(带有“属性”) . 这是原始架构片段的修订版本:

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

我建议不要使用术语“项目”作为数组的名称,以避免混淆,但没有什么能阻止你这样做......

于 2016-07-16T01:14:12.947 回答