RestAssured #18 - Schema Validation

Introduction

When working with APIs, ensuring the integrity and structure of data exchanged between clients and servers is crucial. One effective way to achieve this is through schema validation. Schema validation involves comparing the structure of the API response against a predefined schema or specification, verifying that the data adheres to the expected format. This process helps catch errors, inconsistencies, and unexpected changes in the API's response data.

Benefits of Schema Validation

Schema validation offers several benefits for both API providers and consumers:

  1. Data Consistency: Schema validation ensures that the data being exchanged adheres to a consistent structure, reducing the chances of data integrity issues.

  2. Early Detection of Issues: Schema validation can catch errors in the API response early in the development cycle, making debugging and troubleshooting more efficient.

  3. Version Compatibility: As APIs evolve, schema validation helps ensure that newer versions of the API still comply with the original schema, avoiding compatibility problems.

  4. Clear Communication: By defining a schema, API providers communicate the expected structure to consumers, promoting understanding and correct usage.


Dependency

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>5.3.1</version>
</dependency>

Create Schema

Endpoint - https://test-api.k6.io/public/crocodiles/

Generate schema of Json response using https://jsonschema.net/app/schemas/0

{
    "type": "array",
    "default": [],
    "title": "Root Schema",
    "items": {
        "type": "object",
        "default": {},
        "title": "A Schema",
        "required": [
            "id",
            "name",
            "sex",
            "date_of_birth",
            "age"
        ],
        "properties": {
            "id": {
                "type": "integer",
                "default": 0,
                "title": "The id Schema",
                "examples": [
                    1
                ]
            },
            "name": {
                "type": "string",
                "default": "",
                "title": "The name Schema",
                "examples": [
                    "Bert"
                ]
            },
            "sex": {
                "type": "string",
                "default": "",
                "title": "The sex Schema",
                "examples": [
                    "M"
                ]
            },
            "date_of_birth": {
                "type": "string",
                "default": "",
                "title": "The date_of_birth Schema",
                "examples": [
                    "2010-06-27"
                ]
            },
            "age": {
                "type": "integer",
                "default": 0,
                "title": "The age Schema",
                "examples": [
                    13
                ]
            }
        },
        "examples": [{
            "id": 1,
            "name": "Bert",
            "sex": "M",
            "date_of_birth": "2010-06-27",
            "age": 13
        }]
    },
    "examples": [
        [{
            "id": 1,
            "name": "Bert",
            "sex": "M",
            "date_of_birth": "2010-06-27",
            "age": 13
        }]
    ]
}

Here's a breakdown of the components of this JSON schema:

  1. type: Specifies that the root element of the JSON should be an array.

  2. default: Provides a default empty array.

  3. title: A human-readable title for the schema.

  4. items: Describes the schema for the individual objects within the array.

    • type: Specifies that the array contains objects.

    • default: Provides a default empty object for each individual object.

    • title: A title for the individual object schema.

    • required: An array listing the properties that are required for each individual object.

    • properties: Defines the properties of each individual object.

      • Each property has a type, default value, title, and example values.
  5. examples: Contains an example array of individual objects that conform to the defined schema.


Validating schema in Rest Assured using JsonSchemaValidator

Add schema under the test resources folder

@Test
public void crocodilesSchemaTest(){
    RestAssured.baseURI = "https://test-api.k6.io";
    RestAssured.given()
            .when()
            .get("/public/crocodiles/")
            .then().log().all()
            .assertThat()
            .body(JsonSchemaValidator.matchesJsonSchemaInClasspath("crocodiles_schema.json"));
}

Did you find this article valuable?

Support SUBODH SINGH by becoming a sponsor. Any amount is appreciated!