【问题标题】:Json schema for recursive structure not working递归结构的 Json 模式不起作用
【发布时间】:2019-08-10 14:23:34
【问题描述】:

这是我的 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 Schema 验证是否正确,我故意在响应中放置了一个无效对象 -

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

但它正在这里经过 - https://www.jsonschemavalidator.net/ 我真正想要的是儿童也有一系列儿童等等。因此,应该只允许拥有具有属性的对象的子项 - 标题和子项。 它也传递了这个响应 -

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

【问题讨论】:

    标签: json json.net jsonschema


    【解决方案1】:

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

    {
      "$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
        }
      }
    }
    

    这里是从https://www.jsonschemavalidator.net/ 验证的示例输入 JSON 的验证结果

    输入 JSON:

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

    验证结果:

    消息: 尚未定义属性“invalidobject”,并且架构不允许附加属性。 架构路径: #/definitions/node/additionalProperties

    消息: 对象缺少必需的属性:title、Children。 架构路径: #/定义/节点/必需

    内置 JSON:

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

    验证结果:

    消息: 类型无效。预期字符串,但得到整数。 架构路径: #/定义/节点/属性/标题/类型

    消息: 对象缺少必需的属性:儿童。 架构路径: #/定义/节点/必需

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 2015-01-12
      • 2014-01-05
      • 1970-01-01
      • 2015-04-25
      相关资源
      最近更新 更多