【问题标题】:setting required on a json-schema arrayjson-schema 数组所需的设置
【发布时间】:2013-07-29 16:56:05
【问题描述】:

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

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

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

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

    您的原始示例的问题是您正在为“数组”类型声明“属性”,而不是为数组声明“项目”,然后声明一个“对象”类型(带有“属性”)填充数组。这是原始模式 sn-p 的修订版:

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

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

    【讨论】:

      【解决方案2】:

      我使用this validator 让它工作,方法是将数组元素的架构部分嵌套在名为items 的对象内。该模式现在有两个嵌套的 items 字段,但这是因为一个是 JSONSchema 中的关键字,另一个是因为您的 JSON 实际上有一个名为 items 的字段

      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:

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

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

      【讨论】:

        【解决方案3】:

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

        required 的工作方式在 v3 和 v4 之间发生了变化:

        【讨论】:

        • json-schema.org 似乎正在使用 v4。
        猜你喜欢
        • 2012-07-28
        • 2019-01-19
        • 2015-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-27
        • 1970-01-01
        相关资源
        最近更新 更多