How to match string and ignore the case in karate?

Viewed 709

There is a case where one value sometimes is lower case and sometimes it's upper case. this is the response coming from an API and we have to match if every field in response is correct by ignoring some values. The error text in response sometimes has one keyword in lower and some scenarios it is upper case. How we can ignore one keyword in a string to not match? I don't want to ignore whole text as it works fine if I ignore whole string but is it possible to ignore one keyword only?

  Scenario: string matching
    * def test =
    """

    {
  "sourceType": "Error",
  "id": "123456",
  "type": "searchuser",
  "total": 0,
  "value": [
    {
      "details": "this is the user search case",
      "source": {
        "sourceType": "Error",
        "id": "77200203043",
        "issue": [
          {
            "severity": "high",
            "code": "678",
            "message": {
              "text": "No matching User details found"
            },
            "errorCode": "ERROR401"
          }
        ]
      },
      "user": {
        "status": "active"
      }
    }
  ]
}
    """
    * match test ==
    """
    {
  "sourceType": "Error",
  "id": "#present",
  "type": "searchuser",
  "total": 0,
  "value": [
    {
      "details": "#present",
      "source": {
        "sourceType": "Error",
        "id": "#ignore",
        "issue": [
          {
            "severity": "high",
            "code": "678",
            "message": {
              "text": "No matching User details found"
            },
            "errorCode": "ERROR401"
          }
        ]
      },
      "user": {
        "status": "active"
      }
    }
  ]
}
    """

How to ignore the case only for user here? I tried below but it treats #ignore as a value.

 "text": "No matching #ignore details found"
1 Answers

I'm not looking at your payload dump but providing a simple example. Use karate.lowerCase():

* def response = { foo: 'Bar' }
* match karate.lowerCase(response) == { foo: 'bar' }

EDIT: you can also extract one value at a time and do a check only for that:

* def response = { foo: 'Bar' }
* def foo = response.foo
* match karate.lowerCase(foo) == 'bar' 
Related