top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Understanding the Basics of JSON Schema Validation

JSON Schema is a powerful tool for validating the structure and contents of JSON data. It is commonly used to ensure that JSON data adheres to a specified format, making it essential for API validation, data interchange, and configuration files



Why Schema Validation in API Testing?

Schema validation in API testing is vital for ensuring data consistency and reliability. By confirming that the API responses adhere to a predefined structure and set of rules, schema validation helps catch errors early in the development cycle, reducing the risk of bugs and inconsistencies in production. This practice ultimately leads to more robust and stable software systems, enhancing the overall quality of the application.


Benefits of JSON Schema for Organizations

JSON Schema empowers organizations by:

  • Simplifying Testing and Validation: JSON Schema reduces code complexity and development time by simplifying validation logic. It defines constraints for data structures, enabling the detection and prevention of errors, inconsistencies, and invalid data.

  • Facilitating Seamless Data Exchange: JSON Schema establishes a common language for data exchange, no matter the complexity of your project. It defines precise validation rules for your data structures to create a shared understanding and increase interoperability across different systems and platforms.

  • Enhancing Data Documentation: JSON Schema enables the creation of clear and standardized representations of data. This improves understanding and collaboration among developers, stakeholders, and collaborators, enhancing organizational efficiency.

  • Access to a Vibrant Tooling Ecosystem: JSON Schema is supported by a diverse array of languages, libraries, and frameworks with community-driven tools. This vibrant ecosystem enhances development productivity and provides resources for effective schema implementation and utilization.


Key Concepts of JSON Schema:

Schema Definition: A JSON Schema is itself a JSON document that describes the structure and rules of JSON data. It defines properties, their types, required fields, and other constraints.

Validation: JSON Schema validation involves checking a JSON document against the schema to ensure it conforms to the specified rules.


Defining a JSON Schema:

Now let’s create a JSON Object and the corresponding  JSON Schema describing it.

The following is a simple JSON Object 

We could define its JSON Schema as follow:

We can use any of the "JSON to Schema Converter" tool, to generate Schema from JSON Object.


Let’s explain the keywords that we have used in our sample:

  • The $schema keyword states that this schema is written according to the draft v4 specification.

  • The type keyword defines the first constraint on our JSON data: it has to be a JSON Object.

For each Property, We can define the type. We defined  first_name,last_name  and email as String  

and skills as an Array

Finally, the Schema tells that first_ name, last_name , email and skills  are required.


Validation with JSON Schema:

With our JSON Schema put in place we can validate our JSON Object.

For that, we need to add the following dependency to our pom.xml:

 Finally keeping JOSN object and JSON Schema in src/test/resources folder

Finally we can validate our JSON Object against Schema using three different methods

  1. JSON Schema Validation in classpath

  2. JSON Schema Validation using Hamcrest Matcher

  3. JSON Schema Validation using Networknt


JSON Schema Validation in classpath:

For Validation of the schema stored in the resources of the project in REST assured, we use the matchesJsonSchemaInClasspath() method


JSON Schema Validation Using Hamcrest Matcher:

Hamcrest is a framework for writing matcher objects, allowing 'match' rules to be defined declaratively. It's primarily used in Java for writing tests, where it helps in making assertions more readable and providing better failure messages. While Hamcrest itself doesn't directly handle JSON Schema validation, it can be used in conjunction with libraries that do.


In the context of JSON Schema validation, Hamcrest matchers can be used to verify that JSON data adheres to certain expectations. This is typically done in Java unit tests, where you would combine Hamcrest with a JSON processing library like Jackson or a JSON Schema validation library.


Below is the example of validating JsonSchema using Hamcrest Matcher

 JSON Schema Validation Using Networknt

Networknt is a popular Java library for JSON Schema validation. It is part of the NetworkNT project, which provides various tools for building microservices and other network-based applications. The library allows you to validate JSON data against a defined JSON Schema, ensuring that the data conforms to the expected structure and constraints.


Key Features of networknt JSON Schema Validator
  • Compliance with JSON Schema Specifications: It supports multiple versions of the JSON Schema specification, including Draft 4, Draft 6, Draft 7, and the 2019-09 (Draft 8) versions.

  • Performance: The library is designed to be efficient and fast, making it suitable for high-performance applications.

  • Rich Validation Capabilities: It provides comprehensive validation features, including support for all standard JSON Schema keywords, custom formats, and custom keywords.

  • Error Reporting: The library provides detailed validation error messages, which help in debugging and understanding why a particular JSON instance does not conform to the schema.


Below is the example of validating JSONSchema using Networknt

Conclusion

Asserting JSON Schema for API testing with Java and RestAssured is a valuable practice that helps ensure data consistency and integrity in your API interactions. By adding JSON Schema validation to your API tests, you can catch potential issues early in the development process and provide more robust and reliable APIs to your users.

84 views1 comment

1 Comment

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Guest
May 22
Rated 5 out of 5 stars.

Very clear and nice explanation

Like
bottom of page