Validating object's properties in response

How can I validate an object’s properties in response after I updated/created properties in PUT request using variables?

I have updated the object (employee):

[
    "id": 123
    "externalId": "Employee1",
    "code": "AAPI",
    "description": "John Smith",
    "isValid": true
  }
]

Now I request the list of all employees and I need to check if the employee was updated successfully with all his properties I will use this test in future many times as the regression test so I need to save variables

I’ve tried two ways but I’m not sure if they both are correct:

  1. I save the employee id as a variable using the chain method
bodyData = JSON.parse(responseBody),
employeeId = bodyData.items[0].id;
pm.environment.set("employeeId", employeeId);

and the rest properties values I don’t save I just validate them using

pm.test("Employee's body is correct", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.items[0].code).to.eql("AAPI")
  1. I saved all properties’ values to variables and used these variables in validating but it doesn’t seem correct as I validate what I’ve just saved to variables
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

bodyData = JSON.parse(responseBody),
employeeId = bodyData.items[0].id;
pm.environment.set("employeeId", employeeId);

bodyData = JSON.parse(responseBody),
employeeDescription = bodyData.items[0].description;
pm.environment.set("employeeDescription", employeeDescription);

bodyData = JSON.parse(responseBody),
employeeIsValid = bodyData.items[0].isValid;
pm.environment.set("employeeIsValid", employeeIsValid);

bodyData = JSON.parse(responseBody),
employeeExternalId = bodyData.items[0].externalId;
pm.environment.set("employeeExternalId", employeeExternalId);

bodyData = JSON.parse(responseBody),
employeeCode= bodyData.items[0].code;
pm.environment.set("employeeCode", employeeCode);

pm.test("Employee's body is correct", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.items[0].description).to.eql(pm.environment.get("employeeDescription"));
    pm.expect(jsonData.items[0].isValid).to.eql(pm.environment.get("employeeIsValid"));
    pm.expect(jsonData.items[0].id).to.eql(pm.environment.get("employeeId"));
    pm.expect(jsonData.items[0].externalId).to.eql(pm.environment.get("employeeExternalId"));
    pm.expect(jsonData.items[0].code).to.eql(pm.environment.get("employeeCode"));
});

I would appreciate any help. Thank you

A small tip in automation testing.

You need to setup and tear down the test data as part of the automation, so you can run the test at any time you like without needing to clear down the database or other invasive methods (like using environment variables to keep track of where you are).

For this type of request within Postman,
I would recommend sending a POST request to create the initial employee record. (Setup step)
Your PUT request would come next.
You can have a GET request follow that to confirm that it’s updated the info correctly.
Finally, you delete the request\test data you just entered. (Tear-down step)
This way, you can run the test repeatedly without using variables to track where you are (you will still need to use some variables).

Same test, same test data each time.

This can be done in a couple of ways, either by putting the test in its own folder, with individual requests and using the pre-request and tests tab to pass the data between the requests.

Or a single request, using the setup and tear down steps using the pre-request and tests tab with the Postman sendRequest function. (Personally, I find it easier to just use individual requests as its less code).

Think of the Collection as being the Test Suite, the Folder being the Test Case, and the individual tests being the Test Steps.

In this instance, think of your tests as being the following.

pm.expect(actualResult).to.eql(expectedResult));

The actual result should come from the response body and the expected results should be defined beforehand. You are correct, in that your current code will always pass. (Please remember to make your tests fail to prove that the test is valid, and you don’t have a false positive). The expectedResults should be defined at the top of the tests tab, before you parse any data.