How to validate attributes in response

How to validate attributes in response. Refer Screenshot

I would like to validate all attributes/keys present in training_details array like start date, end date etc [Note: I dont want to validate value of attributes/keys ]

Subtle difference but the training_details in your screenshot is a object, not an array.

Curly brackets are objects, square brackets are arrays.

It’s important to know the difference as certain JavaScript function are only available on arrays.

Moving on from that, I present you with three options for you to try.

let response = pm.response.json() // parse to just under the training_details object.

// method 1 - to.have.property

let keysToCheck = ["id", "start_date", "end_date", "invalid_key"];

keysToCheck.forEach(key => {
    pm.test(`${key} exists in training_details V2`, () => {
        pm.expect(response.training_details).to.have.property(key);
    });
});

// method 2 - object keys & to.include

let keys = Object.keys(response.training_details);

keysToCheck.forEach(keyToCheck => {
    pm.test(`${keyToCheck} exists in training_details V1`, () => {
        pm.expect(keys).to.include(keyToCheck);
    });
});

// method 3 - JSON Schema.

const schema = {
    "type": "object",
    "properties": {
        "training_details": {"type": "object",
            "properties": {
                "id": { "type": "string"},
                "start_date": { "type": "string"},
                "end_date": { "type": "string"},
            },
        "required": ["id", "start_date","end_date"]
        }
    },
};

pm.test('API response schema is valid', () => {
    pm.expect(response).to.have.jsonSchema(schema);
});

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.