Validate JSON/ XML string against a schema for API testing (PoC)
- This Proof of Concept (PoC) is not ready for production use. We recommend using this PoC for evaluation purposes only.
- Download Katalon Studio version 8.3.1.alpha.
Testing API responses against a schema is an efficient and effective way to make sure that APIs are working as expected.
This document shows you how to validate a JSON/ XML response body, request body, or string against a JSON/ XML schema for API testing in Katalon Studio. You can find a sample project for this PoC on our GitHub repository: Schema Validation Sample Project.
With this PoC, you can:
- Use JSON schema to validate Restful response or request body content.
- Use XML schema to validate Restful and SOAP response or request body content.
What is a schema?
A schema concisely describes the structure of other instances, which can be used to require that an instance satisfies a certain number of criteria. The document being validated or described is called the instance, and the document containing the description is called the schema. A schema validation asserts constraints on the structure of the instance data. Recently, there has been a lot of interest in using schemas, such as a JSON or XML schema, as a basis for contract testing.
The infographic below demonstrates the role of JSON/XML schema in API testing:
Infographic based on the Automation Step By Step course: What is JSON Schema
You can learn more from the JSON Schema documentation: What is a schema?
Validate against a JSON/ XML schema in the web service object view
This section walks you through adding schema validation to a web service request and viewing results.
Open or create a new Web Service Request. To create a new Web Service Request, go to Test Explorer > Object Repository. Right-click and choose New > Web Service Request.
Switch to the Validation tab.
To add new validation, click on Add and input the below information:
|Name||Name of the validation.|
|Data||Input a schema, URL to a schema, or file path of a schema. If the Data Type is a file, you can click on Browse on the Data cell to browse to the schema file location.|
Validate against the schema:
When you're done adding validation, hit Save. Click on the dropdown menu of the Test Request, choose Test Request And Verify.
View validation results:
In the Result column of the Validation tab, the schema validation results appear:
- Pass: the response passes the validation.
- Issue(s): the response does not pass the validation.
Click on the result cell to view more details. The Problem table appears with a list of issues. For example:
$.date is missing but it is required, or
$.year: integer found, string expected.
You've successfully validated a JSON/ XML string against a schema using Katalon Studio.
Validate against schemas in a test case
In this PoC, there are two new keywords for validating against a JSON/ XML schema:
- [WS] Validate a JSON string against a schema (PoC)
- [WS] Validate an XML string against a schema (PoC)
You can add these keywords directly in a test case to validate against schemas:
View the test in script mode:
res = WS.sendRequest(findTestObject('XML')) String xml = '''<?xml version="1.0" encoding="utf-8"?> <List> <item> <id>3</id> <username>James Johnson</username> <password>789</password> <gender>FEMALE</gender> <age>75</age> <avatar/> </item> </List>''' String xmlFile = FileUtils.readFileToString(new File("example/xml/person.xml")); WS.validateXmlSchema(res, "example/xml/person.xsd"); WS.validateXmlSchema(xml, "example/xml/person.xsd"); WS.validateXmlSchema(xmlFile, "http://localhost:8080/api/users/xsd", FailureHandling.STOP_ON_FAILURE);
When you execute your test, in the Log Viewer, you can see the result of those validation steps and a list of issues (if any).
Validation failed with root causes: