Json 递归结构的模式不起作用

Json schema for recursive structure not working

这是我的 json 架构。我想要一个递归的树状结构。但是即使我在所需的数组中有无效对象,响应也会通过。

{  
   "$schema":"http://json-schema.org/draft-03/schema",
   "properties":{  
      "Result":{  
         "type":"object",
         "properties":{  
            "Children":{  
               "$ref":"#/definitions/Node"
            }
         },
         "required":true
      }
   },
   "required":true,

   "type":"object",
   "definitions":{  
      "Node":{  
         "type":"array",
         "Items":{  
            "type":"object",
            "properties":{  
               "Children":{  
                  "$ref":"#/definitions/Node"
               }
            },
            "required":true
         }
      }
   }
}

为了检查 JSON 模式验证是否正确,我故意在响应中放置了一个无效对象 -

{"Result":{"title":"title","Children":[{"invalidobject":"invalidobject"}]}}

但它正在经过这里 - https://www.jsonschemavalidator.net/ 我真正想要的是 Children 也有一系列 Children 等等。因此,只允许 Children 拥有具有属性的对象 - title 和 Children。 它也通过了这个回应 -

{"Result":{"title":"title","Children":[{"title":45}]}}

试试这个架构。请注意,即使在第一级,此模式也将允许子数组为空。在第一级之后,您必须允许子数组为空,否则,架构将期望无限递归数据才能通过验证。

{
  "$schema": "http://json-schema.org/draft-03/schema",
  "type": "object",
  "properties": {
    "Result": {
      "$ref": "#/definitions/node",
      "required":true,
    }
  },
  "definitions": {
    "node": {
      "type": "object",
      "properties": {
        "title": {
          "type": "string",
          "required": true
        },
        "Children": {
          "type": "array",
          "required": true,
          "items": {
            "$ref": "#/definitions/node"
          }
        }
      },
      "additionalProperties": false
    }
  }
}

如果您不想让子数组在第一级为空,请尝试像这样单独定义第一级。

{
  "$schema": "http://json-schema.org/draft-03/schema",
  "type": "object",
  "properties": {
    "Result": {
      "type": "object",
      "required": true,
      "properties": {
        "title": {
          "$ref": "#/definitions/title"
        },
        "Children": {
          "minItems": 1,
          "$ref": "#/definitions/Children"
        }
      },
      "additionalProperties": false
    }
  },
  "definitions": {
    "title": {
      "type": "string",
      "required": true
    },
    "Children": {
      "type": "array",
      "required": true,
      "items": {
        "$ref": "#/definitions/node"
      }
    },
    "node": {
      "type": "object",
      "properties": {
        "title": {
          "$ref": "#/definitions/title"
        },
        "Children": {
          "$ref": "#/definitions/Children"
        }
      },
      "additionalProperties": false
    }
  }
}

此处样本输入 JSONs 的验证结果来自 https://www.jsonschemavalidator.net/

输入JSON:

{
  "Result": {
    "title": "title",
    "Children": [
      {
        "invalidobject": "invalidobject"
      }
    ]
  }
}

验证结果:

Message: Property 'invalidobject' has not been defined and the schema does not allow additional properties. Schema path: #/definitions/node/additionalProperties

Message: Required properties are missing from object: title, Children. Schema path: #/definitions/node/required

Inpot JSON:

{
  "Result": {
    "title": "title",
    "Children": [
      {
        "title": 45
      }
    ]
  }
}

验证结果:

Message: Invalid type. Expected String but got Integer. Schema path: #/definitions/node/properties/title/type

Message: Required properties are missing from object: Children. Schema path: #/definitions/node/required