NashTech Blog

JSON Schema for API Testing: Error Handling and Versioning

Table of Contents

Introduction

Using JSON Schema for error handling and versioning in API testing offers several benefits, such as enhanced error management and simplified version control. Additionally, by defining schemas and incorporating validation mechanisms, we can improve resilience and flexibility in API development & testing. Consequently, this leads to a better user experience and drives business success. Embracing it, can accelerate API testing and unlock opportunities in modern software architecture.

Understanding of JSON Schema

Basically, JSON Schema establishes rules for the structure. Data can be validated against these rules using various tools. JSON Schema seamlessly integrates into development workflows.

There are three primary reasons why it is crucial:

  • Validation: Guarantees that JSON data adheres to predefined rules, thereby preserving the integrity of the data.
  • Clarity & Documentation: It acts as a comprehensive documentation tool, assisting developers in comprehending the structure of JSON.
  • Interoperability: JSON Schema establishes a standardized data structure, promoting compatibility among various platforms.

How does JSON Schema work?

  • JSON Schema is described using JSON to define the anticipated structure and limitations of JSON data. Various tools and libraries validate JSON data based on the schema guidelines.
  • Data that complies with the schema is considered valid, whereas data that does not adhere to the schema results in validation errors.
  • JSON Schema seamlessly integrates with a variety of programming languages and frameworks, facilitating the straightforward implementation of validation processes in development workflows.

Error Handling with JSON Schema

It is utilized for error handling to specify the structure of error responses in APIs, guaranteeing consistency, thorough documentation, and ease of validation.

  • Consistency: Across various API endpoints is maintained to guarantee that error responses follow a uniform structure.
  • Validation: It is conducted to verify error responses adhere to a predetermined schema, ensuring they align with the anticipated format.
  • Documentation: Serves as a guide for developers, outlining the various error types, error codes, and supplementary details.

Developers make use of JSON Schema to systematically define and validate error responses, thereby fostering consistency and standardization across the API.

Define Error Schemas

Initiate the process by outlining JSON schemas that depict different error responses that our API might come across. Firstly, these schemas need to cover key elements like error codes, messages, and extra metadata. Secondly, through the establishment of error schemas, we can guarantee consistency in error responses, making error handling and troubleshooting procedures more straightforward.

{
"type": "object",
"properties": {
"error": {
"type": "object",
"properties": {
"code": { "type": "integer" },
"message": { "type": "string" }
},
"required": ["code", "message"]
}
},
"required": ["error"]
}
Validate Error Responses

Utilizing JSON Schema for error handling provides a systematic method for defining and verifying error responses within API development. Furthermore, utilizing error handling through it guarantees uniform and logical error messages throughout API endpoints, enhancing user contentment and system dependability. In addition, developers use JSON Schema to systematically describe and validate error responses, ensuring consistency and uniformity across the API. Specifically, JSON Schema is utilized to define and validate error types, including validation errors, authentication failures, and server errors. Consequently, this ensures that these errors are handled and communicated appropriately.

// Require the Ajv library to validate JSON data against schemas
const JsonValidator = require("ajv");
// Create a new instance of Ajv
const ajv = new JsonValidator();
// Define the JSON Schema for error responses
const errorSchema = {
type: "object", // The response should be an object
properties: {
error: { // The object should have an 'error' property
type: "object", // 'error' property should itself be an object
properties: {
code: { type: "integer" }, // 'code' property should be an integer
message: { type: "string" } // 'message' property should be a string
},
required: ["code", "message"] // Both 'code' and 'message' are required properties
}
},
required: ["error"] // The response object must contain an 'error' property
};
// Sample error response from the API
const errorResponse = {
error: {
code: 401, // Sample error code (e.g., 401 for Unauthorized)
message: "Unauthorized access" // Sample error message
}
};
// Validate the error response against the defined schema
const isValid = ajv.validate(errorSchema, errorResponse);
// Log whether the error response is valid according to the schema
console.log("Is error response valid?", isValid);

Basically, The utilization of standardized error schemas improves documentation, resulting in increased efficiency and user-friendliness. This allows developers and API consumers to promptly grasp error formats and effectively resolve issues.

Examples of error responses (Valid/ Invalid)

Valid error response
{
"error": {
"code": 400,
"message": "Bad Request"
}
}
Invalid error response
{
"error": {
"message": "Bad Request"
}
}
Upon validation against the error schema, an error would be generated:
Invalid error response: 
[{"keyword":"required","dataPath":".error","schemaPath":"#/required","params":{"missingProperty":"code"},
"message":"should have required property 'code'"}]

Versioning with JSON Schema

What is versioning?

JSON Schema versioning requires the creation of various versions of your schemas to accommodate modifications in data structures as time progresses. This practice guarantees that both past and current versions of your JSON data can be verified against their corresponding schemas.

Why versioning is important?
  • Backwards and Forward Compatibility is guaranteed by making sure that modifications to the schema do not disrupt the compatibility with current JSON data.
  • Clear Documentation enhances the comprehensibility of data structures’ development, facilitating developers’ understanding and manipulation of the data.
  • Data Validation verifies the data against the appropriate schema version, guaranteeing that the data adheres to the anticipated structure and limitations.
How to implement versioning?
Define schema (version1)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
},
"required": ["id", "name"]
}
Create a new schema (version 2)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
}
},
"required": ["id", "name", "email"]
}
Use of versioning
// Importing Ajv library for Schema validation
const Ajv = require("ajv");
const ajv = new Ajv();

// Load the JSON schemas
const schemaV1 = require("./schemaV1.json");
const schemaV2 = require("./schemaV2.json");

// Create the validateData function in order to verify the data
function validateData(data, version) {
let validate;

// Identify schema to utilize depending on the version
switch (version) {
case "1":
// Compile version 1
validate = ajv.compile(schemaV1);
break;
case "2":
// Compile version 2
validate = ajv.compile(schemaV2);
break;
default:
// Error will be thrown if the schema version is invalid
throw new Error("Invalid schema version");
}
// Verify the data in accordance with the compiled schema
const isValid = validate(data);
// In case of invalid data, an error should be thrown along with the validation errors
if (!isValid) {
throw new Error(`Invalid data: ${JSON.stringify(validate.errors)}`);
}
}

// Example usage
const dataV1 = {
"id": 1,
"name": "Robin"
};

const dataV2 = {
"id": 1,
"name": "Robin",
"email": "Robin@example.com"
};
// Validate the example data by utilizing the validateData function
try {
validateData(dataV1, "1");
validateData(dataV2, "2");
} catch (error) {
// Check and log errors that occur during validation
console.error(error.message);
}
Conclusion

Versioning using JSON Schema is a reliable method for handling modifications in data formats as time progresses. Moreover, it guarantees coherence across various iterations of your JSON data and offers transparent documentation and validation processes. Furthermore, adhering to a systematic versioning plan enables you to uphold the reliability and uniformity of your data models throughout the evolution of your application.

Below is the end-to-end flowchart of utilizing Schema for error handling and versioning in API testing

 

Every phase or action in the end-to-end process of utilizing it for error handling. Versioning in API testing is represented by a distinct step in the flowchart.

  • Define API Specifications: PExplain the concepts of endpoints, formats, error handling, and versioning.
  • Create JSON Schemas: Develop schemas for validation.
  • Implement API Endpoints: Create endpoints based on specifications.
  • Error Handling: Ensure that error responses are by schemas.
  • Test Environment Setup: Create a replica for extensive testing.
  • Validation: Verify requests, responses, and errors.
  • Test Case Development: Examine the various API features and versions available.
  • Execute Test Suites: Validate endpoints by utilizing schemas.1
  • Error Analysis: Analyze errors about the adherence to handling protocols.
  • Version Compatibility Testing: Guarantee consistency of schema between different versions.
  • CI/CD Pipeline Integration: Streamline API testing within pipelines through automation.
  • Regression Testing: Incorporate API tests to identify regression issues.
  • Monitoring and Maintenance: Monitor the performance, fine-tune the schemas, and optimize the tests.

Conclusion

Utilizing Schema for error handling and versioning in API testing not only improves error management, but also streamlines version control, enhances resilience, and increases adaptability in API development. This ultimately leads to a better user experience, improved business success, faster API testing, and the discovery of new software architecture possibilities.

For more details, please visit – https://assertible.com/blog/testing-and-validating-api-responses-with-json-schema

Picture of Ankit Kumar

Ankit Kumar

Leave a Comment

Suggested Article

Discover more from NashTech Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading