Search code examples
yamlcucumberkarate

How to validate with yml file a karate structure response


recently I started working with Karate and Yaml for the first time. I was able to validate simple response structures where all the answer data were on the same level. But now I have to validate a more complicated structure and I spent a lot of time without success.

When I perform a GET request I receive the next answer:

[
    {
        "factories": [
            {
                "id": 1,
                "scopes": [
                    {
                        "id": null,
                        "name": "name 1",
                        "expireD": 10,
                        "isExpired": true
                    }
                ],
                "companyName": "TEST",
            },
            {
                "id": 2,
                "scopes": [
                    {
                        "id": null,
                        "name": "name 2",
                        "expireD": 7,
                        "isExpired": true
                    }
                ],
                "companyName": "TEST2",
            }
        ],
        "scopeId": null
    }
]

The structure validation is not directly in the karate code. It is on a yml file like this:

operationId: getTest
statusCode: 200
params:
body: null
matchResponse: true
responseMatches:
  scopeId: '##number'
  factories:
    companyName: '#string'
    id: '#number'
    scopes:
      expireD: '#number'
      name: '#string'
      id: '#null'
      isExpired: '#boolean'

I review the structure around 100 times and I have the same error all the time when I arrive here:

* match response contains responseMatches

The error is the next one: $[1].factories | data types don't match (LIST:MAP)

I tried to use match each, ignore one by one the structures to see which one is failing and also reduce the validations as #array and it is not working.

Any help will be more than welcome. Thank you.


Solution

  • Finally, a solution was found. The Karate documentation offers an idea about how to use it defining a structure of data that could be used as a type. I tried before but I added before the responseMatches section. The yml file looks like this:

    operationId: getTest
    statusCode: 200
    params:
    body: null
    matchResponse: true
    responseMatches:
      scopeId: '##number'
      factories: '#[_ <= 5] factoryStructure'
    
    factoryStructure:
        companyName: '#string'
        id: '#number'
        scopes: '#[] scopeStructure'
    
    scopesStructure:
      expireD: '#number'
      name: '#string'
      id: '#null'
      isExpired: '#boolean'