Saurabh
Saurabh

Reputation: 940

Karate - Validating schema for the entire json response

I am working on validating the entire json response for a GET request using Karate.

Here is the sample json response from the request (I have shown only two elements for items )

[
  {
    "items": [
      {
        "createdById": "ADMIN",
        "createdByName": "ADMIN",
        "changedByName": "ADMIN",
        "oid": "121212fgfg2123",
        "links": {
          "self": {
            "href": "/internal/organiz/12345"
          },
          "subcom": []
        },
        "name": "NewETLTesting"
      },
      {
        "createdById": "ADMIN",
        "createdByName": "ADMIN",
        "changedByName": "ADMIN",
        "oid": "1212dfffg45",
        "links": {
          "self": {
            "href": "/internal/organiz/5a6e0"
          },
          "subcom": []
        },
        "name": "FromKarateModified"
      }
    ]
  }
]

Here is how I am trying to validate:

 And match response.*.* ==
    """
  {
    "createdById" : '#string',
    "createdByName" : '#string',
    "changedByName" : '#string',
    "oid" : '#string',
    "links" : '#object',
    "name" : '#string'
  }
    """

However, I am getting an assertion error:reason: actual value is not map-like. If I try putting square brackets around the curly braces, I get reason: actual and expected arrays are not the same size. Also I tried to play around with response like $.[*]. etc but couldn't get it working.

Thanks in advance!!

Upvotes: 1

Views: 9639

Answers (2)

Jaimeen K Patel
Jaimeen K Patel

Reputation: 21

The below Schema will validate the entire reponse properly

* def refSubcom = {<object schema>}
* def refself = {href : '#string'}
* def refLinks = {self : '#object refself', subcom:'##[] refSubcom'}
* def optionalItemArr = 
    """
    {
        createdById:'#string',
        createdByName:'#string',
        changedByName:'#string',
        oid: '#string',
        links: '#object refLinks',
        name:'#string'
    }
    ###
* def itemData = 
    """
    {
        item : '##[] optionalItemArr'
    }
    """
* def reponseSchema = '##object itemData'

* def SuccessSchema = '##[] reponseSchema'

you can refer the below link : karate : Complex JSON Schema matching

Upvotes: 1

Peter Thomas
Peter Thomas

Reputation: 58153

You need to pay attention to your JSON structure and also understand JsonPath better. You can cut and paste the below and see it working:

* def response = 
"""
[
  {
    "items": [
      {
        "createdById": "ADMIN",
        "createdByName": "ADMIN",
        "changedByName": "ADMIN",
        "oid": "121212fgfg2123",
        "links": {
          "self": {
            "href": "/internal/organiz/12345"
          },
          "subcom": []
        },
        "name": "NewETLTesting"
      },
      {
        "createdById": "ADMIN",
        "createdByName": "ADMIN",
        "changedByName": "ADMIN",
        "oid": "1212dfffg45",
        "links": {
          "self": {
            "href": "/internal/organiz/5a6e0"
          },
          "subcom": []
        },
        "name": "FromKarateModified"
      }
    ]
  }
]
"""
And match each response[0].items ==
"""
  {
    "createdById" : '#string',
    "createdByName" : '#string',
    "changedByName" : '#string',
    "oid" : '#string',
    "links" : '#object',
    "name" : '#string'
  }
"""

Upvotes: 2

Related Questions