# JSON Schema Comparison

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.&#x20;

![Bac Schema Errors](https://www.autokinjs.com/docs/4b9f3d9e024af07eae9fbc25e338ea92/bad_schema.png)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://autokin.gitbook.io/docs/features/json-schema-comparison.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
