需要节点错误级别的Json架构

时间:2017-02-03 17:15:40

标签: java json jsonschema

我正在尝试以下列方式创建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验证程序时,我收到错误,因为我指定了“ stdItem ”所需的“ stdAtrributes ”节点和验证程序无法掩饰它。

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

如果我将“ stdAttributes ”放在“ stdItem ”之外,那就可以了。

有谁知道如何为“ stdItem ”定义此要求?

1 个答案:

答案 0 :(得分:0)

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

但是这个模式没有根据需要定义具有stdItem属性的对象(查看数据),它定义了一个具有属性stdTypestdAttributes和{{1并且要求属性children存在。换句话说,它是以下数据的模式:

stdAttributes

对于您的数据,架构应为:

{
  "layoutItem": {
    "stdType": "CONTAINER",
    "stdAttributes": [],
    "children": []
  }
}
相关问题