使用邮递员和tv4对具有多个元素的json数组验证jsonschema

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

下面是我的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"
      ]
    }
  ]
}

下面是要验证的json

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

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

]

这里元素“ stations”在模式中被标记为必需,但是在json的第二项中丢失。仍然通过了tv4验证。

我们真正需要的是,它应该通过验证,因为第二个项目中缺少station元素

该观察结果是,如果任何json项中的IF站元素为不存在,则验证失败。但是,如果station元素在其中一项中为present,则通过验证

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

我尝试了tv4选项“ checkRecursive”,其值为true和false ...仍然通过验证]]

感谢您的任何帮助

[下面是我的json模式{“ $ schema”:“ http://json-schema.org/draft-04/schema#”,“ type”:“ array”,“ items”:[{“ type”: “ object”,“ properties”:{“ id”:{“ type”:...

postman jsonschema tv4
1个回答
0
投票

items关键字可以采用一个模式或模式数组,并且根据所使用的版本,它具有不同的语义。

© www.soinside.com 2019 - 2024. All rights reserved.