1

I need to validate a JSON response (like below) and check whether ANY one of the items in the JSON array has today's date as createdDate.

"createdDate" is actually returned as a timestamp, but I dont have the actual value. The only thing I need to validate is today's date. Order of the items is also not guaranteed. I looked at javascript macros specified at https://intuit.github.io/karate/, but it does not have an example of what I am trying to do. Please let me know any suggestions.

{
    "items": [
      {
        "id": "...",
        "name": "...",
        "createDate": "03-30-2020:10:12:30"

      },
      {
        "id": "...",
        "name": "...",
        "createDate": "03-31-2020:10:12:30"
      }
}

2 Answers2

0

Here you go, one possible solution:

* def sdf = new java.text.SimpleDateFormat("MM-dd-yyyy")
* def today = sdf.format(new java.util.Date())
* def list = $response..createDate
* def fun = function(x){ return x.startsWith(today) }
* def filtered = karate.filter(list, fun)
* assert filtered.size() != 0

For more ideas, see:

https://stackoverflow.com/a/55938480/143475

https://stackoverflow.com/a/52892797/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
0
 {
    "id": "...",
    "name": "...",
    "createDate": "#regex <your regex string>"
 }

For example if the date is expected in yyyy-mm-dd format

 "createDate": "#regex \\d{4}-\\d{2}-\\d{2}"
wherath
  • 71
  • 1
  • 3
  • 1
    Hi wherath! I know this might be obvious to you, but would you mind editing your answer and adding a few words of explanation? – Mike Szyndel Mar 20 '23 at 10:48