Need help looping through an object and checking the keys

My question: Hey I have a single json object that is returned and I want to be able to test that only the allowed keys are returned in that object, since the data will be different each time I only want to check the keys

Here is the object structure

[
{
“Id”: “75725702-50d8-41a8-963d-4aaaaaaaaaaa”,
“Number”: “500200”,
“Key”: 565465454,
“CategoryId”: 5,
“Option”: “5”,
“CreatedByUserId”: “4127”,
“CreatedDate”: “2020-08-04T00:38:44.988+12:00”,
“LastModifiedByUserId”: “4127”,
“LastModifiedDate”: “2020-08-04T00:38:44.988+12:00”
}
]

Here is the test progress I have so far - not much

pm.test(“Object only contains allowed keys”, () => {

var notesJsonStructure = [‘Id’, ‘Number’, ‘Key’, ‘CategoryId’, ‘Option’, ‘CreatedByUserId’, ‘CreatedDate’, ‘LastModifiedByUserId’, ‘LastModifiedDate’];

var responseObj = pm.response.json();

console.log(responseObj);

_.each(responseObj.response, (item) => {

    console.log("Test" + item);
    pm.expect(item).to.be.oneOf(notesJsonStructure);       
});

});

The line of console.log(“Test” + item); never outputs, so makes me think I am not even hitting that far

Thanks

Hmm seems like I could use schema validation but this fails as well

pm.test(“Object only contains allowed keys”, () => {

var schema = {
“Properties”:
{
“Id”: {“type”: “string”},
‘Number’, {“type”: “string”},
“Key”: {“type”: int
“CategoryId”: {“type”: “int”},
“Note”: {“type”: “string”},
“CreatedByUserId”: {“type”: “string”},
“CreatedDate”: {“type”: “string”},
“LastModifiedByUserId”: {“type”: “string”},
“LastModifiedDate”: {“type”: “string”}
}
}
var responseObj = pm.response.json();
console.log(responseObj);
console.log(schema);

pm.expect(responseObj,schema).to.be.true;
});

I got it working with the below, will this cover the case were the sending API inserts new keys that I don;t have on my list? I just checked and it does.

pm.test(“Response body must have valid keys”, function () {

var data = pm.response.json();

var notesJsonStructure = ['Id', 'Number', 'Key', 'CategoryId', 'Option', 'CreatedByUserId', 'CreatedDate', 'LastModifiedByUserId', 'LastModifiedDate'];

data.forEach(function(item) {

  pm.expect(item).to.have.all.keys(notesJsonStructure);     

});

});

While this works I would prefer schema validation most probably

Your instinct that this should be done by schema validation is spot on. The solution you posted is only happy path as far as I can tell - it won’t tell you about unexpected keys if all the expected ones are present.

Kk so made progress, biggest issue was that I didn’t understand what I was working with, once I started checking what was been returned by doing this I was able to make real progress

pm.expect(responseObject.AvailableEids[0]).to.be.an(“object”);