1

Want to validate in Karate framework For the below Json What I want to validate is,

if "isfilter_regex":0 then "msgtype": "##regex ^[A-Za-z0-9_.]-/*"
or if "isfilter_regex":1 then "msgtype": "#string" (when isfilter_regex = 1 then msgtype must be a regular expression)

In my case number of candidate s in candidates array is 180+ I tried lot of things I ended up failing can anybody help me here?

{
   "candidates":[
      {
         "candidate":{
            "name":"Alex",
            "category":[
               {
                  "category_name":"APCMRQ",
                  "filters":[
                     {
                        "isfilter_regex":0,
                        "msgtype":"APCMRQ"
                     }
                  ]
               },
               {
                  "category_name":"BIDBRQ",
                  "filters":[
                     {
                        "isfilter_regex":1,
                        "msgtype":"'(AMSCNQ(_[A-Za-z0-9]{1,3}){0,3})'"
                     }
                  ]
               }
            ]
         }
      }
   ]
}
  • I normally reply to questions where the example is simplified focusing on what the problem is. also please see other answers: https://stackoverflow.com/search?q=%5Bkarate%5D+array+conditional – Peter Thomas Sep 26 '22 at 17:44
  • What I want to check is, When "isfilter_regex": 0 then "msgtype": '##regex ^[A-Za-z0-9.\/\\s_\-]* ' And when "isfilter_regex": 1 then "msgtype": '#string ' I agree, sorry about that. Just wanted to show the complexity, so I took this approach. – user14719581 Sep 28 '22 at 05:19
  • if you can edit your question to be a 10-line example or less I'll look at it tomorrow – Peter Thomas Sep 28 '22 at 05:44
  • I have done edits, please have a look Sir – user14719581 Sep 28 '22 at 07:00

1 Answers1

0

I tried below way which works only when idex specified, but what to do if I want to do this check for entire array?,

* def msgRegexValue = response.candidates[150].candidate.category[0].filters[0].isfilter_regex
* def actualFilter = response.candidates[150].candidate.category[0].filters[0]
* def expectedFilter = actualFilter

    * def withRegex =
    """
    {
       "isfilter_regex": 0,
       "msgtype": '##regex ^[A-Za-z0-9_.]*-*/*'
    }
    """
    * def withoutRegex =
    """
    {
       "isfilter_regex": 1,
       "msgtype": '##string'
    }
    """
    * def expected = msgRegexValue == 0 ? withRegex: withoutRegex
    * match actualFilter == expected