To Validate a field value in a List

[
{
“accepted”: true,
“bot”: {
“code”: “xxx”,
“key_types”: [
{
“code”: “xxx”,
“value”: “xxx”
}
]
},
“delete_application_code”: “xxx”,
“deleted”: true,
“deletion_refusal_reason”: “”,
“master_application_code”: “xxx”,
“refusal”: {
“code”: “1201”,
“reason”: “xxx-xxx-x-xxx-xxxx”
}
},
{
“accepted”: true,
“bot”: {
“code”: “xxx”,
“key_types”: [
{
“code”: “xxx”,
“value”: “xxx”
}
]
},
“delete_application_code”: “xxx”,
“deleted”: true,
“deletion_refusal_reason”: “”,
“master_application_code”: “xxx”,
“refusal”: {
“code”: “1201”,
“reason”: “xxx-xxx-x-xxx-xxxx”
}
},
{
“accepted”: true,
“bot”: {
“code”: “xxx”,
“key_types”: [
{
“code”: “xxx”,
“value”: “xxx”
}
]
},
“delete_application_code”: “xxx”,
“deleted”: true,
“deletion_refusal_reason”: “”,
“master_application_code”: “xxx”,
“refusal”: {
“code”: “1201”,
“reason”: “xxx-xxx-x-xxx-xxxx”
}
}
]

This is my response Body and here i want to validate the field value in this list " accepted": true"
Below is the code i wrote:
var jsonData = pm.response.json();

console.log(typeof jsonData);

var responseSchema={

"type": "object",

"title": "accepted",

"description": "This is an Delete Statuses Rest Service",

"required": [

    "accepted"

],

"properties": {

    "bot": {

        "type": "object"

    },

}

};

pm.test(“DELETE accepted should be TRUE”,function(){

pm.expect(tv4.validate(jsonData, responseSchema)).to.be.true;

});

But i see that the assertion is failing with below error even if the value is true for all the fields in the List
DELETE accepted should be TRUE | AssertionError: expected false to be true

Kindly suggest or correct me if anything is incorrect

First of all, I don’t recommend using tv4.
Second of all, your response body is an array but you have written the schema for a single object.

Here is a tutorial to get you started with schema validation in Postman:

After this, your schema should look more like this example (which is similar to your case):

const schema = {
    "type": "array",
    "items": { "$ref": "#/definitions/customer" },
    "definitions": {
        "customer": {
            "type": "object",
            "required": [ "id", "fistName", "lastName", "title", "gender" ],
            "properties": {
                "id": {
                    "type": "number",
                    "description": "internal id"
                },
                "fistName": {
                    "type": "string",
                    "description": "First name"
                },
                "lastName": {
                    "type": "string",
                    "description": "Last name"
                },
                "title": {
                    "type": "string",
                    "description": "Title"
                },
                "gender": {
                    "type": "string",
                    "description": "Gender (male / female)",
                    "enum": ["male", "female"]
                }
            }
        }
    }  
};

pm.test("Validate schema", () => {
    pm.response.to.have.jsonSchema(schema);
});
1 Like