Json 架构要求节点处于错误级别

Json schema requiring node in wrong level

我正在尝试通过以下方式创建 json 架构:

 {
  "$schema": "http://json-schema.org/schema#",
  "title": "Layout",
  "description": "The layout created by the user",
  "type": "object",
  "definitions": {
    "stdAttribute": {
      "type": "object",
      "properties": {
        "attributeValue": {
          "type": "object"
        },
        "attributeName": {
          "type": "string"
        }
      }
    },
    "stdItem": {
      "type": "object",
      "required" : ["stdAttributes"],
      "properties": {
        "stdType": {
          "enum": [
            "CONTAINER",
            "TEXT",
            "TEXTAREA",
            "BUTTON",
            "LABEL",
            "IMAGE",
            "MARCIMAGE",
            "DATA",
            "SELECT",
            "TABLE"
          ]
        },
        "stdAttributes": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/stdAttribute"
          }
        },
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/stdItem"
          }
        }
      }
    }
  },
  "properties": {
    "layoutItem": {
      "$ref": "#/definitions/stdItem"
    }
  }
}

我正在验证以下 json 反对它:

{
  "layoutItem": {
    "stdItem": {
      "stdType": "CONTAINER",
      "stdAttributes": [],
      "children": []
    }
  }
}

问题是,当我是 运行 java 验证器时,我得到了一个错误,因为我根据要求指定了“stdAtrributes”节点“stdItem”,验证器无法找到它。

我试图在属性中定义所需的数组,但架构无效。

如果我将“stdAttributes”放在“stdItem”之外,它会起作用。

有谁知道我如何为“stdItem”定义这个要求?

您的架构中的问题是您希望 layoutItem 的值根据 #/definitions/stdItem 处的架构有效。

但是这个模式没有定义一个你想要的 stdItem 属性 的对象(查看你的数据),它定义了一个具有 stdType, [=16] 属性的对象=] 和 children 并要求 属性 stdAttributes 存在。换句话说,它是以下数据的模式:

{
  "layoutItem": {
    "stdType": "CONTAINER",
    "stdAttributes": [],
    "children": []
  }
}

对于您的数据,模式应该是:

 {
  ...
  "definitions": {
    ...
    "stdItem": {
      "type": "object",
      "required" : ["stdItem"],
      "properties": {
        "stdItem": {
          "type": "object",
          "required" : ["stdAttributes"],
          "properties": {
            "stdType": { ... },
            "stdAttributes": { ... },
            "children": { ... }
          }
        }
      }
    }
  },
  ...
}