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})'"
                     }
                  ]
               }
            ]
         }
      }
   ]
}
4
  • I normally reply to questions where the example is simplified focusing on what the problem is. also please see other answers: stackoverflow.com/search?q=%5Bkarate%5D+array+conditional Commented Sep 26, 2022 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. Commented Sep 28, 2022 at 5:19
  • if you can edit your question to be a 10-line example or less I'll look at it tomorrow Commented Sep 28, 2022 at 5:44
  • I have done edits, please have a look Sir Commented Sep 28, 2022 at 7:00

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.