简体   繁体   English

邮递员:即使响应错误,模式验证也会通过

[英]Postman: schema validation passes even with a wrong response

I have the following schema for bellow happy path response我有以下模式用于波纹管快乐路径响应

var responseSchema = 
{
   "type": "object",
   "properties": {
      "value": {
        "type": "object",
        "properties":{
           "items": {
              "type": "array",
              "items": {
                 "type": "object",
                 "properties": {
                    "patientGuid": {"type": "string" },
                    "givenName": {"type": "string" },
                    "familyName": {"type": "string" } ,
                    "combinedName" : {"type": "string" }
                 },
                 "required": ["patientGuid","givenName","familyName"]
              }                 
           }
        }
      }
   }
};

Happy path response:快乐路径 回复:

{
    "value": {
       "items": [
           {
              "patientGuid": "e9530cd5-72e4-4ebf-add8-8df51739d15f",
              "givenName": "Vajira",
              "familyName": "Kalum",
              "combinedName": "Vajira Kalum"
           }
        ],
        "href": "http://something.co",
        "previous": null,
        "next": null,
        "limit": 10,
        "offset": 0,
        "total": 1
    },
    "businessRuleResults": [],
    "valid": true
}

I check if condition to validate whether response schema is correct:我检查是否有条件来验证响应模式是否正确:

if(responseBody !== null & responseBody.length >0)
{
    var responseObject = JSON.parse(responseBody);

    if(tv4.validate(responseObject, responseSchema))
    {  
      //  do something
    } 
    else
    {
      // log some msg
    }
}

Schema validation condition (nested if) get passed even when I get bellow bad response.即使我得到了糟糕的响应,架构验证条件(嵌套 if)也会通过。

{
    "value": {
        "items": [],
        "href": "http://something.co",
        "previous": null,
        "next": null,
        "limit": 10,
        "offset": 0,
        "total": 0
    },
    "businessRuleResults": [],
    "valid": true
}

Why response schema validation not failed as a response not has required fields?为什么响应架构验证没有失败,因为响应没有必填字段?

When I ran your code and removed a property from the response data it seemed to work ok:当我运行您的代码并从响应数据中删除一个属性时,它似乎工作正常:

邮递员

I would suggest a couple of things though - The tv4 module is not fantastic, it's not actively being worked on (for a couple of years I think) and there's a bunch of complaints/issue raised on the Postman project about how poor it is and asking for it to be replaced in the native app.不过,我会建议一些事情 - tv4模块并不出色,它并没有被积极开发(我认为有几年了)并且 Postman 项目提出了一堆关于它有多差的投诉/问题以及要求在本机应用程序中替换它。

Have you considered creating a test to check the schema instead?您是否考虑过创建一个测试来检查架构? This is very basic and could be easily refactored and wrapped in some logic but it would check the different value types of your response.这是非常基本的,可以很容易地重构并包装在一些逻辑中,但它会检查响应的不同值类型。

pm.test("Response data format is correct", () => {
    var jsonData = pm.response.json()

    // Check the type are correct
    pm.expect(jsonData).to.be.an('object')
    pm.expect(jsonData.value).to.be.an('object')
    pm.expect(jsonData.value.items).to.be.an('array')
    pm.expect(jsonData.value.items[0]).to.be.an('object')

    // Check the value types are correct
    pm.expect(jsonData.value.items[0].combinedName).to.be.a('string')
    pm.expect(jsonData.value.items[0].givenName).to.be.a('string')
    pm.expect(jsonData.value.items[0].familyName).to.be.a('string')
    pm.expect(jsonData.value.items[0].patientGuid).to.a('string')
});

Also the syntax that you're using is the older style now and you don't need to JSON.parse(responseBody) the response body anymore as pm.response.json() is basically doing the same thing.此外,您现在使用的语法是旧样式,您不再需要JSON.parse(responseBody)响应正文,因为pm.response.json()基本上是在做同样的事情。

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

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