【问题标题】:Cant validate children json-schema无法验证儿童 json 模式
【发布时间】:2017-03-21 17:45:07
【问题描述】:

我正在处理这样的 json 架构:

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

我正在尝试使用上述方案验证以下 json:

{
  "stdItem": {
    "stdType": "CONTAINER",
    "stdAttributes": [
      {
        "attributeName": "ola",
        "attributeValue": "teste"
      }
    ],
    "children": [
      {
        "stdItem": {
          "stdType": "TEXT",
          "stdAttributes": [
            {
              "attributeName": "ola",
              "attributeValue": "teste"
            }
          ],
          "children": []
        }
      }
    ]
  }
}

我收到一条错误消息,告诉我路径 stdItem/children/0 缺少必需属性 stdTypestdAttributes。如您所见,属性在那里,它们并没有丢失。

我尝试更改属性的顺序,但仍然不起作用。我不断收到以下错误:

--- BEGIN MESSAGES --- 错误:对象缺少必需的属性 (["stdAttributes","stdType"]) 级别:“错误” 架构:{"loadingURI":"#","pointer":"/definitions/stdItem"} 实例:{“指针”:“/stdItem/children/0”} 域:“验证” 关键词:“必需” 必需:["stdAttributes","stdType"] 缺少:["stdAttributes","stdType"] --- 结束信息 ---

谁能指出我做错了什么?

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    当您声明“children”属性时,您是在说它是“stdItem”,因此它期望那里具有 stdAttributes 和 stdType 属性。相反,您在 json 中拥有的是 stdItem 类型的“stdItem”属性。 因此,您的架构中缺少该属性 (stdItem) 的声明。

    此架构将验证您的 json:

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

    请注意,我正在向具有“stdItem”属性的“children”的item 规范添加一个对象。 (我没有按要求声明它,但您可能想添加它)

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多