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.
1
GET https://reqres.in/api/users?page=2
2
3
Response:
4
{
5
"page": 2,
6
"per_page": 3,
7
"total": 12,
8
"total_pages": 4,
9
"data": [
10
{
11
"id": 4,
12
"email": "[email protected]",
13
"first_name": "Eve",
14
"last_name": "Holt",
15
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
16
},
17
{
18
"id": 5,
19
"email": "[email protected]",
20
"first_name": "Charles",
21
"last_name": "Morris",
22
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
23
},
24
{
25
"id": 6,
26
"email": "[email protected]",
27
"first_name": "Tracey",
28
"last_name": "Ramos",
29
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
30
}
31
]
32
}
Copied!
Expected Login Response Schema
feature/schema/reqres-response-schema.json
1
{
2
"definitions": {},
3
"$schema": "http://json-schema.org/draft-07/schema#",
4
"$id": "http://example.com/root.json",
5
"type": "object",
6
"title": "The Root Schema",
7
"required": [
8
"page",
9
"max_page",
10
"per_page",
11
"total",
12
"total_pages",
13
"data"
14
],
15
....
16
"items": {
17
"$id": "#/properties/data/items",
18
"type": "object",
19
"title": "The Items Schema",
20
"required": [
21
"id",
22
"email",
23
"first_name",
24
"last_name",
25
"avatar"
26
],
27
"properties": {
28
"id": {
29
"$id": "#/properties/data/items/properties/id",
30
"type": "string",
31
"default": 0,
32
"pattern": "^(.*)quot;
33
},
34
"email": {
35
...
36
}
Copied!
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.
1
@schemachecks
2
Feature: Bad Schema Feature Example
3
As Autokin tester
4
I want to verify that all API contracts are correct
5
6
Scenario: Perform check on bad schema
7
Given that a secure endpoint is up at reqres.in
8
Given I set Content-Type header to application/json
9
When I GET /api/users?page=2
10
Then response status code should be 200
11
Then I expect response data schema complies to "{schemaBasePath}/resreq-users-schema.json"
Copied!
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.
Bac Schema Errors
Last modified 8mo ago
Copy link