0

I need help in validating the presence of one key in the response. The response of the API looks like -

"persons": [
        {
            "id": "27",
            "source": {
                "personId": 281,
                "emailAddress": "abc@abc.com",
                "firstName": "Steve"
            }
        },
        {
            "id": "28", 
            "source": {
                "personId": 353,
                "emailAddress": "abcd@abc.com",
                "firstName": "John"
                "LastName" : "Cena"         
            }
        }
    ]
}

I want to assert if the source.LastName appears or not and if it does then it should always hold a string value.

The solution should be generic and it should work for 30 or 40 persons object also down the time, currently I am using karate version 0.9.4 and need a resolution for handling such scenarios.

Thanks in advance !

vdrulerz
  • 264
  • 3
  • 13

1 Answers1

2

In the schema "##string" validates that the field can be null or a string.

Sample Code:

Feature: Schema validation

    Scenario:
        * def resp =
            """
            {
            "persons": [
                    {
                        "id": "27",
                        "source": {
                            "personId": 281,
                            "emailAddress": "abc@abc.com",
                            "firstName": "Steve"
                        }
                    },
                    {
                        "id": "28", 
                        "source": {
                            "personId": 353,
                            "emailAddress": "abcd@abc.com",
                            "firstName": "John",
                            "LastName" : "Cena"         
                        }
                    }
                ]
            }
            """
        * def schema = 
        """
            {
                "id": "#string",
                "source": {
                    "personId": "#number",
                    "emailAddress": "#string",
                    "firstName": "#string",
                    "LastName": "##string"
                }
            }

        """
        * match each resp.persons == schema
Neodawn
  • 1,086
  • 1
  • 6
  • 9
  • what if the lastName key does not even exists in 3 person objects out of 20 ? – vdrulerz Feb 26 '20 at 12:29
  • We are validating each element against the schema, so the number of elements does not matter. The validation will still pass. You can modify the input and check it. – Neodawn Feb 26 '20 at 12:39
  • My use case is to check whether LastName key exists or not among all the persons object and if it does then check among those objects that it always holds a string value – vdrulerz Feb 26 '20 at 12:39
  • "##string" fuzzy matching pattern does both. Documentation link: https://github.com/intuit/karate#optional-fields – Neodawn Feb 26 '20 at 12:42
  • Yeah that I was aware of already, anywyz let me try it out once and then I will accept your answer accordingly. thanks ! – vdrulerz Feb 26 '20 at 12:43
  • Thanks @Neodawn, ur approach is useful – vdrulerz Feb 27 '20 at 10:28