/

JSON Schema Comparison

Validation of REST API response using JSON Schema


It is important that contracts among consumer of the API ensure that the data expected are always correct. With this JSON Schema comparison, we can add assertion on the response data if it conforms to expected schema. We can store the schema to a file and use that as expected schema.

    GET https://reqres.in/api/users?page=2

    Response:
    {
    "page": 2,
    "per_page": 3,
    "total": 12,
    "total_pages": 4,
    "data": [
        {
            "id": 4,
            "email": "eve.holt@reqres.in",
            "first_name": "Eve",
            "last_name": "Holt",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
        },
        {
            "id": 5,
            "email": "charles.morris@reqres.in",
            "first_name": "Charles",
            "last_name": "Morris",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
        },
        {
            "id": 6,
            "email": "tracey.ramos@reqres.in",
            "first_name": "Tracey",
            "last_name": "Ramos",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
        }
    ]
}

Expected Login Response Schema

feature/schema/reqres-response-schema.json

{
    "definitions": {},
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "title": "The Root Schema",
    "required": [
        "page",
        "max_page",
        "per_page",
        "total",
        "total_pages",
        "data"
    ],
    ....
               "items": {
                "$id": "#/properties/data/items",
                "type": "object",
                "title": "The Items Schema",
                "required": [
                    "id",
                    "email",
                    "first_name",
                    "last_name",
                    "avatar"
                ],
                "properties": {
                    "id": {
                        "$id": "#/properties/data/items/properties/id",
                        "type": "string",
                        "default": 0,
                        "pattern": "^(.*)$"
                    },
                    "email": {
    ...
}

In the above Schema, it expects that max_page should be required and must be with the response, however the example of our response does not contain it, so we expect that this will be reported on our test. Aside from that, in the schema it also described that id in the items are string but we will be receiving number instead hence this should also be reported.

@schemachecks
Feature: Bad Schema Feature Example
	As Autokin tester
	I want to verify that all API contracts are correct

    Scenario: Perform check on bad schema
        Given that a secure endpoint is up at reqres.in
        Given I set Content-Type header to application/json
        When I GET /api/users?page=2
        Then response status code should be 200
        Then I expect response data schema complies to "{schemaBasePath}/resreq-users-schema.json"

The last line of our test is a check for Schema compliance. Here Autokin will validate if the response does conforms with our expected schema, if not it should report all errors. Below is an example, where exact error will be presented along with the line numbers if available, so that it will be easier for us to debug and identify the problem.

Bad Schema - JSON Line Details