简体   繁体   English

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

[英]No enum error when validating JSON using jsonschema in python

First of all, I am not getting a proper error reponse on the web platform as well ( https://jsonschemalint.com ). 首先,我在Web平台( https://jsonschemalint.com )上也没有得到正确的错误响应。 I am using jsonschema in python, and have a proper json schema and json data that works. 我在python中使用jsonschema,并且具有正确的json模式和json数据有效。

The problem I'd like to solve is the following: Before we deliver JSON files with example data, we need to run them through SoapUI to test if they are proper, as we are dealing with huge files and usually our devs may make some errors in generating them, so we do the final check. 我要解决的问题如下:在传递带有示例数据的JSON文件之前,我们需要通过SoapUI运行它们以测试它们是否正确,因为我们正在处理巨大的文件,通常我们的开发人员可能会出错。在生成它们时,我们进行最后检查。

I'd like to create a script to automate this, avoiding SoapUI. 我想创建一个脚本来自动执行此操作,避免使用SoapUI。 So after googling, I came across jsonschema, and tried to use it. 因此,在谷歌搜索之后,我遇到了jsonschema,并尝试使用它。 I get all the proper results,etc, I get errors when I delete certain elements as usual, but the biggest issues are the following: 我得到了所有正确的结果,等等,像往常一样删除某些元素时出现错误,但是最大的问题如下:

Example : I have a subsubsub object in my JSON schema, let's call it Test1, which contains the following : 示例:我的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]
    }]
    }

The first issue I ran across is that enum in prop3 isn't validated correctly. 我遇到的第一个问题是prop3中的枚举未正确验证。 So, when I use " enum1" or "enumruwehrqweur" or "literally anything", the tests pass. 因此,当我使用“ enum1”或“ enumruwehrqweur”或“任何文字”时,测试就会通过。 In addition, that min-max characters do not get checked throughout my JSON. 另外,整个我的JSON都不会检查最小-最大字符。 No matter how many characters I use in any field, I do not get an error. 无论我在任何字段中使用多少个字符,我都不会出错。 Anyone has any idea how to fix this, or has anyone found a better workaround to do what I would like to do? 任何人都知道如何解决此问题,还是有人找到了更好的解决方法来完成我想做的事情? Thank you in advance! 先感谢您!

There were a few issues with your schema. 您的架构存在一些问题。 I'll address each of them. 我将分别解决每个问题。

In your schema, you have "Test1". 在您的架构中,您具有“ Test1”。 In your JSON instance, you have "test1". 在您的JSON实例中,您具有“ test1”。 Case is important. 案例很重要。 I would guess this is just an error in creating your example. 我想这只是创建您的示例时的错误。

In your schema, you have "Test1" at the root level. 在您的架构中,您在根级别具有“ Test1”。 Because this is not a schema key word, it is ignored, and has no effect on validation. 因为这不是架构关键字,所以将其忽略,并且对验证没有影响。 You need to nest it inside a "properties" object, as you have done elsewhere. 您需要像在其他地方一样将其嵌套在“属性”对象中。

{
  "properties": {
    "test1": {

Your validation would still not work correctly. 您的验证仍无法正常工作。 If you want to validate each item in an array, you need to use the items keyword. 如果要验证数组中的每个项目,则需要使用items关键字。

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

Finally, you'll need to nest the required and type key words inside the items object. 最后,你需要嵌套requiredtype里面的关键词items对象。

Here's the complete schema: 这是完整的架构:

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

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

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