简体   繁体   English

使用 postman 和 tv4 针对具有多个元素的 json 数组验证 jsonschema

[英]Validate jsonschema against a json array having multiple elements using postman and tv4

Below is my JSON schema下面是我的 JSON 模式

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "stations": {
          "type": "array",
          "items": [
            {
              "type": "object",
              "properties": {
                "id": {
                  "type": "integer"
                },
                "serial_number": {
                  "type": "string"
                },
                "name": {
                  "type": "string"
                }
              },
              "required": [
                "id",
                "serial_number",
                "name"
              ]
            }
          ]
        }
      },
      "required": [
        "id",
        "name",
        "stations"
      ]
    }
  ]
}

Below is the json to validate下面是要验证的json

[
    {
        "id": 1,
        "name": "Test location",       
        "stations": [
            {
                "id": 1,
                "serial_number": "TEST001",
                "name": "TEST-STN!"                
            }
        ]

    },
    {
        "id": 2,
        "name": "Test location2"    
    }

]

Here the element "stations" is marked as required in schema, but it is missing in the second Item of json.这里元素“stations”在模式中被标记为必需,但它在json的第二个项目中缺失。 still tv4 validation is passed.仍然 tv4 验证通过。

What we really required is, it should fail the validation because the station element is missing in the second Item我们真正需要的是,它应该无法通过验证,因为第二个 Item 中缺少 station 元素

The observation is IF station element is NOT present in any of the JSON Item, then validation is failing.观察结果是 IF 站元素不存在于任何 JSON 项目中,则验证失败。 But if the station element is present in one of the item then validation is passed但是,如果站元素存在于其中一项中,则验证通过

pm.test("Login Validation", function() { pm.expect(tv4.validate(pm.response.json(), pm.environment.get('schema.json'), true, true), tv4.error).to.be.true;});

I tried tv4 option "checkRecursive" with value both true and false...Still it passing the validation我尝试了 tv4 选项“checkRecursive”,其值为 true 和 false ......它仍然通过了验证

Any help is appreciated任何帮助表示赞赏

I think something like this would work for you and show the issue:我认为这样的事情对您有用并显示问题:

let schema = {
    "type": "array",
    "items": {
        "type": "object",
        "required": [
            "id",
            "name",
            "stations"
        ],
        "properties": {
            "id": {
                "type": "integer"
            },
            "name": {
                "type": "string"
            },
            "stations": {
                "type": "array",
                "items": {
                    "type": "object",
                    "required": [
                        "id",
                        "serial_number",
                        "name"
                    ],
                    "properties": {
                        "id": {
                            "type": "integer"
                        },
                        "serial_number": {
                            "type": "string"
                        },
                        "name": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }
}

pm.test("Check Schemat", () => {
    pm.response.to.have.jsonSchema(schema)
}) 

I've included the jsonSchema() Postman function as this uses AJV rather than the older and not currently maintained tv4 module.我已经包含了jsonSchema() Postman 函数,因为它使用 AJV 而不是旧的且当前未维护的 tv4 模块。

The items keyword can take a schema, or an array of schemas and it has different semantics depending on which version is being used. items关键字可以采用模式或模式数组,并且根据使用的版本具有不同的语义。

When items takes a single schema, it's describing an array where all items in the array must conform to the given schema.items采用单个模式时,它描述了一个数组,其中数组中的所有项都必须符合给定的模式。

{
  "type": "array".
  "items": { "type": "string" }
}

["a", "b", "c"]

When items takes an array of schemas, it's describing a tuple where each schema in items is compared with the corresponding item in the instance array.items采用模式数组时,它描述了一个元组,其中items中的每个模式都与实例数组中的相应项进行比较。

{
  "type": "array",
  "items": [{ "type": "string" }, { "type": "integer" }, { "type": "boolean }]
}

["a", 1, false]

You are confused because you are using the wrong form of items .你很困惑,因为你使用了错误的items形式。 Because you used the array form, only the first element in your array is being validated.因为您使用了数组形式,所以只会验证数组中的第一个元素。 The validator ignores the rest of the items.验证器忽略其余项目。

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

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