How to validate elements in nested json array in karate?

Viewed 4812

I am using the karate framework for writing some automated test cases. I'd like to validate the schema for each element in a nested array list . For the example below, I would like to validate each child of each element in the returned array. Is there a way to get an array list of all children of all elements? I can do that by calling some java functions, but I was wondering if there's a way in karate to get that.

Something like "for each element in the returned array validate the schema of each of its children".

Thanks!

[
    {
        "id": "A",
        "children": [
            {
                "size": "10",
                "type": "A",                   
                "name": "B"
            },
            {
                "size": "10",
                "type": "A",                   
                "name": "B"
            }                            
        ]
    },
    {
        "id": "B",
        "children": [
            {
                "size": "10",
                "type": "A",                   
                "name": "B"
            }, 
            }
                "size": "3",
                "type": "C",                   
                "name": "D"
            }               
        ]
    }
]
1 Answers

match each will be more convenient for validating JSON array with a schema,

* def children = $response[*].children[*]
* def schema = { "name": "#string","size": "#string","type": "#string"}
* match each children == schema

This will extract all the values of the children and validate each child is matching with the schema

Related