Need check all dates in array. They must to be above today

Hi everybody! i have response like this and i need check all expirationDate. They must be above today date. Can’t find here how can i check all of this, help me please) thanks

[
{
“id”: “ec1e91b3-518e-47f7-9ac2-37ac3736cde9”,
“subscriber”: “user”,
“expirationDate”: “2023-01-19”,
“payload”: “TEST 53885635”,
“format”: “310”,
“creationDate”: “2023-01-19T14:36:22.558643”,
“modificationDate”: “2023-01-19T14:36:22.55865”,
“isRead”: false
},
{
“id”: “1ca74ba7-851a-4328-8f41-8aaed95f9f9d”,
“subscriber”: “user”,
“expirationDate”: “2023-01-19”,
“payload”: “TEST 80144708”,
“format”: “792”,
“creationDate”: “2023-01-19T14:16:42.000603”,
“modificationDate”: “2023-01-19T14:16:42.000623”,
“isRead”: false
}
]

i have allready trying check one object of array, but its failed “AssertionError: expected ‘2023-01-19’ to be a number or a date”

const responseJson = pm.response.json();
var moment = require(‘moment’);
pm.test(“x”, function () {
pm.expect(responseJson[0].expirationDate).to.be.above(moment);
});

That’s not how you use moment.

Please review the following example.

When working with dates, you need to ensure that both dates are in the same format.

If you change the dates to YYYYMMDD and parse them to an integer, then its just a number comparison after this.

Your response is an array, so I’ve put the tests inside a loop.

It will test each date individually. I’m using the payload information in the test name, so you can determine which test has failed. Feel free to change to another field\element.

const response = pm.response.json();

var moment = require('moment');

var today = parseInt(moment(new Date()).format("YYYYMMDD"));
// console.log(today);

response.forEach(record => {

    var expirationDate = parseInt(moment(record.expirationDate).format("YYYYMMDD"));
    // console.log(expirationDate)
    
    let payload = record.payload
    // console.log(payload);
 
    pm.test(`${payload} expiration date (${expirationDate}) to be above today`, () => {
        pm.expect(expirationDate).to.be.above(today);
    });
    
});
1 Like

it worked! thank you so much!