Skip to main content

Validate JSON/XML string against a schema for API testing (PoC)

Important:
  • 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.

  1. 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.

    new web service request

  2. Switch to the Validation tab.

    validation

  3. To add new validation, click on Add and input the below information:

    OptionDescription
    NameName of the validation.
    Type
    • JSON Schema
    • XML Schema
    • Auto Detect
    Data Type
    • Text: Commonly used for simple schemas.
    • File: JSON or XML schema file.
    • Auto detect
    DataInput 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.
    Target
    • Response Body: check the response body.
    • Request Body: check the request body.
    • Request/Response: check both response and request body.
  4. 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.

    test request and verify

  5. View validation results:

    In the Result column of the Validation tab, the schema validation results appear:

    result

    • 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.

    result detail

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:

You can add these keywords directly in a test case to validate against schemas:

test case with validation keywords

View the test in script mode:

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);
Note:
  • You can also add the sendRequestAndVerify keyword as a test step. This action also sends the current request and executes verification snippets. Learn more about this keyword at our javadoc Send Request And Verify and Verification Snippets.

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 passed:

    validation passed

  • Validation failed:

    validation failed

  • Validation failed with root causes:

    validation failed with root causes