1

So I have a response with the field role is of type enum

'{
    "content": [
        {
            "id": "1",
            "roles": [],
        },
        {
            "id": "2",
            "roles": [
                "manager"
            ]
        },
        {
            "id": "3",
            "roles": [
                "client"
            ],
        }
    ],
}'

I want to validate that the role in response is of type enum so I was trying something like

* match each response..role == ['#? enumRoles.contains(_)'}

But kept getting errors. The solution I was trying is dervied from How to validate multiple possible values in Karate using a schema. Then I found out that the accepted solution there was also yielding unexpected end of file and message not supported errors.

Any help is appreciated. I am using 1.2.0.RC1

1 Answers1

0

This is more trickier than I expected, but here is a solution. Please read the docs to understand what the API does.

* def response =
"""
{
    "content": [
        {
            "id": "1",
            "roles": [],
        },
        {
            "id": "2",
            "roles": [
                "manager"
            ]
        },
        {
            "id": "3",
            "roles": [
                "client"
            ],
        }
    ]
}
"""
* def roles = ['manager', 'client']
* def temp1 = $..roles[*]
* def temp2 = karate.distinct(temp1)
* match roles contains temp2

There is no such thing as enum in plain JSON. Consider writing custom JS functions if needed, here is an example: https://stackoverflow.com/a/62567412/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248