DJ342
DJ342

Reputation: 59

Schema Validation - Karate expression to check if value exists in array

Sample Response

{
    "data": [
    {
        "name": "DJ", 
        "status": "ACTIVE"
    } 
    ]
}

Sample Feature File

@ignore
Feature: Sample

  @smoke
  Scenario: Karate expression to check if value exists in array 
    Given url url
    And path '/test'
    When method GET
    Then status 200
    And def users = response.data
    And def possibleStatus = ["ACTIVE", "INACTIVE"]

    And def schema =
    """
    {
      name: '#string',
      status: ? 
    }
    """
    And match each users contains schema

Is there a way to check if status is either ACTIVE or INACTIVE using karate expression ?

NOTE: It can be achieved by writing custom JS function.

Upvotes: 1

Views: 1166

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

* def statuses = [ 'ACTIVE', 'INACTIVE' ]
* def response = [{ name: 'DJ', status: 'ACTIVE' }, { name: 'PJ', status: 'INACTIVE' }]
* match each response == { name: '#string', status: '#? statuses.contains(_)' }

Upvotes: 3

Related Questions