简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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