无法验证子 json-schema

Cant validate children json-schema

我正在处理这样的 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 --- error: object has missing required properties (["stdAttributes","stdType"]) level: "error" schema: {"loadingURI":"#","pointer":"/definitions/stdItem"} instance: {"pointer":"/stdItem/children/0"} domain: "validation" keyword: "required" required: ["stdAttributes","stdType"] missing: ["stdAttributes","stdType"] --- END MESSAGES ---

谁能指出我做错了什么?

当您声明 "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" }
  }    
}

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