What is JSON Schema | Bondar Academy
Course: Playwright API Testing with TypeScript
Module: Schema Validation
Instructor: Artem Bondar
Lesson Summary
In this module, we explore JSON Schema and JSON Schema Validation . JSON Schema is a JSON object that describes the structure of another JSON object, defining the required properties, their types, and the overall structure. Importance of JSON Schema JSON Schema is crucial for preventing integration issues that arise when API responses change unexpectedly. For instance, if an API removes a property like age from its response, clients may encounter errors such as your age is undefined . With JSON Schema validation, we can ensure that the expected properties are present, thus avoiding such issues. Creating JSON Schema To create a JSON Schema, developers can use online tools such as a JSON to JSON Schema converter . This tool can automatically generate a schema based on a given JSON object. Example of JSON Schema Generation When a response contains a property text of type array , the schema specifies that each item in the array must be a string . If the response structure changes, such as returning a number instead of a string, the validation will fail. Handling Complex Objects For larger JSON objects, like a list of articles, JSON Schema can significantly reduce complexity. Instead of creating assertions for hundreds of properties, the schema can succinctly define the structure, making validation easier and more efficient. Contract Testing JSON Schema validation is a form of contract testing , specifically provider-driven contract testing . This ensures that the API responses conform to the expected schema, helping to catch integration issues early. In the next lesson, we will demonstrate how to validate JSON schemas using Playwright and automate schema generation.