14.2 C
New York
Sunday, September 8, 2024

validation – Is it potential to validate response with json schema file in karate?


Sure, it is potential to validate simply the construction of a response towards a schema in Karate with out validating the info values.

Karate DSL is designed to be versatile sufficient to permit for schema validation (i.e., construction validation) in addition to precise worth validation. To focus solely on the construction, you may make the most of the match key phrase together with kind assertion markers similar to #notnull, #string, #quantity, and so forth.

These markers validate the presence and kind of a worth with out asserting on the precise knowledge values.

Here is a step-by-step information on attaining construction validation:

Outline Your Anticipated Schema: Define the anticipated construction of your API response in a Karate characteristic file. You need to use Karate’s particular markers to specify the kind of worth anticipated with out specifying the precise knowledge. For non-obligatory fields, Karate gives ## (e.g., ##string for an non-obligatory string).

Use the match Key phrase: Apply Karate’s match key phrase to validate the response construction towards your schema definition. The match key phrase facilitates the comparability of the particular API response together with your schema expectations.

Make use of Karate’s Kind Assertion Markers: Whereas defining your schema, use Karate’s kind assertion markers to make sure the fields are of the proper kind. A few of these markers embrace:
#string to point a string worth
#quantity for quantity values
#boolean for boolean values
#array for arrays
#object for JSON objects
#notnull to easily assert the sphere is just not null, no matter its precise worth or kind

State of affairs: Validate API response construction
    * def expectedSchema = 
    """
    {
      "id": "#quantity",
      "title": "#string",
      "isActive": "#boolean",
      "scores": "#array",
      "particulars": {
        "deal with": "#string",
        "cellphone": "##string" // Non-compulsory area
      }
    }
    """

    * url 'http://instance.com/api/knowledge'
    * technique GET
    * standing 200
    * match response == expectedSchema

Within the above instance, expectedSchema describes the anticipated construction of the API response. The main target is just not on the precise values of fields like id, title, isActive, and so forth., however quite on guaranteeing they’re current and of the proper kind within the response. The # prefix is used to indicate the anticipated kind of every worth, whereas ## is used for non-obligatory fields.

By following this method, you may guarantee your API responses match the anticipated construction with out tightly coupling your assessments to particular knowledge values, thus making your assessments extra adaptable and resilient to knowledge modifications in your API.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles