繁体   English   中英

使用 Python 中的重复 json 响应验证 Json 模式

[英]Validate Json schema with repeating json response in Python

当我尝试使用来自 Jsonschema.validate 的 Validate 使用我的 Json 响应来验证我的 Json 架构时,我没有得到任何结果,而它在Z5E056C500A1C4B6A7110//www50D88807BADE上显示匹配。

Json 架构

 {
        "KPI": [{
            "KPIDefinition": {
                "id": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "version": {
                    "type": "number"
                },
                "description": {
                    "type": "string"
                },
                "datatype": {
                    "type": "string"
                },
                "units": {
                    "type": "string"
                }
            },
            "KPIGroups": [{
                "id": {
                    "type": "number"
                },
                "name": {
                    "type": "string"
                }
            }]
        }],
        "response": [{
            "Description": {
                "type": "string"
            }
        }]
    }

JSON 响应JSON 响应

{
  "KPI": [
    {
      "KPIDefinition": {
        "id": "2",
        "name": "KPI 2",
        "version": 1,
        "description": "This is KPI 2",
        "datatype": "1",
        "units": "perHour"
      },
      "KPIGroups": [
        {
          "id": 7,
          "name": "Group 7"
        }
      ]
    },
    {
      "KPIDefinition": {
        "id": "3",
        "name": "Parameter 3",
        "version": 1,
        "description": "This is KPI 3",
        "datatype": "1",
        "units": "per Hour"
      },
      "KPIGroups": [
        {
          "id": 7,
          "name": "Group 7"
        }
      ]
    }
  ],
  "response": [
    {
      "Description": "RECORD Found"
    }
  ]
}

代码

json_schema2 = {"KPI":[{"KPIDefinition":{"id_new":{"type":"number"},"name":{"type":"string"},"version":{"type":"number"},"description":{"type":"string"},"datatype":{"type":"string"},"units":{"type":"string"}},"KPIGroups":[{"id":{"type":"number"},"name":{"type":"string"}}]}],"response":[{"Description":{"type":"string"}}]}

json_resp = {"KPI":[{"KPIDefinition":{"id":"2","name":"Parameter 2","version":1,"description":"This is parameter 2 definition version 1","datatype":"1","units":"kN"},"KPIGroups":[{"id":7,"name":"Group 7"}]},{"KPIDefinition":{"id":"3","name":"Parameter 3","version":1,"description":"This is parameter 3 definition version 1","datatype":"1","units":"kN"},"KPIGroups":[{"id":7,"name":"Group 7"}]}],"response":[{"Description":"RECORD FETCHED"}]}

print(jsonschema.validate(instance=json_resp, schema=json_schema2))

验证未正确完成,我在响应中更改了数据类型和键名,但仍然没有引发异常或错误。

jsonschema.validate(..)不应该返回任何东西。

您的架构 object 和 JSON object 都可以,如果没有引发任何异常,验证已经通过 - 这似乎就是这种情况。

话虽如此,您应该将调用包装在 try-except 块中,以便能够捕获验证错误。

就像是:

try:
    jsonschema.validate(...)
    print("Validation passed!")
except ValidationError:
    print("Validation failed")
# similarly catch SchemaError too if needed.

更新:您的架构无效。 就目前而言,它将验证几乎所有输入。 一个模式 JSON 应该是一个 object (dict),它应该有像“type”这样的字段,并且根据类型,它可能有其他必需的字段,比如“items”或“properties”。 请阅读如何编写 JSONSchema。

这是我为您的 JSON 编写的架构:

{
  "type": "object",
  "required": [
    "KPI",
    "response"
  ],
  "properties": {
    "KPI": {
        "type": "array",
        "items": {
          "type": "object",
            "required": ["KPIDefinition","KPIGroups"],
            "properties": {
              "KPIDefinition": {
                "type": "object",
                "required": ["id","name"],
                "properties": {
                  "id": {"type": "string"},
                  "name": {"type": "string"},
                  "version": {"type": "integer"},
                  "description": {"type": "string"},
                  "datatype": {"type": "string"},
                  "units": {"type": "string"},
                },
                "KPIGroups": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "required": ["id", "name"],
                    "properties": {
                      "id": {"type": "integer"},
                      "name": {"type": "string"}
                    }
                  }
                }
              }
            }
        }
    },
    "response": {
        "type": "array",
        "items": {
            "type": "object",
            "required": ["Description"],
            "properties": {
              "Description": {"type": "string"}
            }
        }
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM