【问题标题】:Json schema requiring node in wrong levelJson 模式需要错误级别的节点
【发布时间】:2017-02-03 17:15:40
【问题描述】:

我正在尝试通过以下方式创建 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”定义这个要求?

【问题讨论】:

    标签: java json jsonschema


    【解决方案1】:

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

    但是这个模式并没有根据你的需要定义一个具有stdItem 属性的对象(查看你的数据),它定义了一个具有stdTypestdAttributeschildren 属性的对象,并要求属性@ 987654327@ 存在。换句话说,它是以下数据的架构:

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

    对于您的数据,架构应该是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2018-04-06
      • 2013-05-11
      • 1970-01-01
      • 2017-05-01
      • 2021-09-26
      相关资源
      最近更新 更多