在python中使用jsonschema验证JSON时没有枚举错误

问题描述 投票:0回答:1

首先,我也没有在网络平台上得到适当的错误响应(https://jsonschemalint.com)。我在python中使用jsonschema,并有一个适当的json架构和json数据。

我想要解决的问题如下:在我们提供带有示例数据的JSON文件之前,我们需要通过SoapUI运行它们来测试它们是否正确,因为我们正在处理大文件,通常我们的开发人员可能会犯一些错误在生成它们时,我们做最后的检查。

我想创建一个脚本来自动执行此操作,避免使用SoapUI。所以在谷歌搜索后,我遇到了jsonschema,并尝试使用它。我得到了所有正确的结果等,当我像往常一样删除某些元素时出现错误,但最大的问题如下:

示例:我的JSON模式中有一个subsubsub对象,我们称之为Test1,其中包含以下内容:

**Schema**
    {
   "exname":"2",
   "info":{},
   "consumes":{},
   "produces":{},
   "schemes":{},
   "tags":{},
   "parameters":{},
   "paths":{},
   "definitions":{
      "MainTest1":{
         "description":"",
         "minProperties":1,
         "properties":{
            "test1":{
               "items":{
                  "$ref":"#//Test1"
               },
               "maxItems":10,
               "minItems":1,
               "type":"array"
            },
            "test2":{
               "items":{
                  "$ref":"#//"
               },
               "maxItems":10,
               "minItems":1,
               "type":"array"
            }
         }
      },
      "Test1":{
         "description":"test1des",
         "minProperties":1,
         "properties":{
            "prop1":{
               "description":"prop1des",
               "example":"prop1exam",
               "maxLength":10,
               "minLength":2,
               "type":"string"
            },
            "prop2":{
               "description":"prop2des",
               "example":"prop2example",
               "maxLength":200,
               "minLength":2,
               "type":"string"
            },
            "prop3":{
               "enum":[
                  "enum1",
                  "enum2",
                  "enum3"
               ],
               "example":"enum1",
               "type":"string"
            }
         },
         "required":[
            "prop3"
         ],
         "type":"object"
      }
   }
}

    **Proper example for Test1** 
    {
    "Test1": [{
        "prop1": "TestStr",
        "prop2": "Test and Test",
        "prop3": "enum1"
    }]
    }

    **Improper example that still passes validation for Test1** 
    {
    "test1": [{
        "prop1": "TestStr123456", [wrong as it passes the max limit]
        "prop2": "Test and Test",
        "prop3": " enum1" [wrong as it has a whitespace char before enum1]
    }]
    }

我遇到的第一个问题是prop3中的枚举未正确验证。因此,当我使用“enum1”或“enumruwehrqweur”或“字面上的任何东西”时,测试通过。此外,在我的JSON中不会检查min-max字符。无论我在任何领域使用多少个字符,我都不会收到错误。任何人都知道如何解决这个问题,或者有没有人找到一个更好的解决方法来做我想做的事情?先感谢您!

python json validation jsonschema json-schema-validator
1个回答
1
投票

您的架构存在一些问题。我将解决他们每个人。

在您的架构中,您有“Test1”。在您的JSON实例中,您有“test1”。案例很重要。我猜这只是创建你的例子时的一个错误。

在您的架构中,您在根级别拥有“Test1”。因为这不是架构关键字,所以它会被忽略,并且对验证没有影响。您需要将其嵌套在“属性”对象中,就像您在其他地方所做的那样。

{
  "properties": {
    "test1": {

您的验证仍然无法正常工作。如果要验证数组中的每个项目,则需要使用items关键字。

{
  "properties": {
    "test1": {
      "items": {
        "description": "test1des",

最后,你需要在required对象中嵌套typeitems关键词。

这是完整的架构:

{
  "properties": {
    "test1": {
      "items": {
        "description": "test1des",
        "minProperties": 1,
        "properties": {
          "prop1": {
            "description": "prop1des",
            "example": "prop1exam",
            "maxLength": 10,
            "minLength": 2,
            "type": "string"
          },
          "prop2": {
            "description": "prop2des",
            "example": "prop2example",
            "maxLength": 200,
            "minLength": 2,
            "type": "string"
          },
          "prop3": {
            "enum": [
              "enum1",
              "enum2",
              "enum3"
            ],
            "example": "enum1",
            "type": "string"
          }
        },
        "required": [
          "prop3"
        ],
        "type": "object"
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.