JSON 架构连接数组与属性

JSON schema connecting arrays with properties

我被要求创建 JSON 文件类型为 "zfs" 的架构,其中包含数组 属性 池,其中此类数组的每个项目都必须具有属性:名称(字符串), volumes(字符串数组)、sizeInGB(从 0 到 65536 的数字)和 numberOfFiles(从 0 到 4294967296 的整数)。 我想出的代码看起来像这样,

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "zfs",
"type": "array",
 "properties" : {
   "names":{"type": "string"},
   "volumes":{"type": "array"},
     "properties" : {"type": "string"},
         "sizeInGB":{
          "type": "number",
        "minimum": 0,
        "maximum": 65536
         },
         "numberOfFiles":{
          "type": "integer",
        "minimum": 0,
        "maximum": 4294967296
         }
         },
         "required": [ "names", "numberOfFiles","sizeInGB","volumes"],
}

但它在验证时会抛出 EOF 错误,尽管我知道该错误意味着什么,但我只是不知道如何处理它以使其正常工作。

Maciek,您的架构可能有问题。您定义:

"type" : "array",
"properties" : {
  "names" : {...},
  "volumes" : {
     "type" : array"
  },
  "properties" : { ...}
}

我知道你想要一个

array of objects where each objects contains: name and an array of objects

我希望在 JSON 架构中表述为:

"type" : "array",
"items" : {        <============ here
  "type" : "object",
  "properties" : {
    "names" : {...},
    "volumes" : {
      "type" : array"
      "items" : {   <============ and here
        "type" : "object",
        "properties" : { ... }
      }
    },
  },
}

在 JSON 模式中,您需要以某种方式在数组中定义内容 of/items 的模式(如果您想根据匹配的 JSON 模式验证 JSON 数组项)。您可以使用 "items" 关键字并使用元组语法(如果数组中的元素序列很重要)或作为对象数组,其中序列无关紧要,但每个对象必须符合特定的模式。是的,如果需要,您甚至可以拥有不同种类对象的数组。

请阅读:https://json-schema.org/understanding-json-schema/reference/array.html

https://json-schema.org/latest/json-schema-validation.html#rfc.section.6.4

的规范

希望对您有所帮助。