Justify, a JSON Schema Validator, in Katalon Studio

I have made a demo project on GitHub

Motivation

JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. JSON Schema is a nice tool for Web Service testing in Katalon Studio.

As the post in Katalon Forum “JSON Schema validation broken after 5.9.0” reports, everit-org/json-schema, a JSON Schema validator, once worked prior to KS 5.9.0. But as of KS 5.9.0 it does not work. Ibus reported the reason. For the time being, everit-org/json-schema is not an option for users.

JSON Schema project’s Implementation page list an alternative JSON Schema validator in Java/Groovy: Justify Justify uses javax.json library for parsing JSON. It does NOT depend on the org.json library which caused problem for everit-org/json-schema in Katalon Studio. Therefore, I suppose, Justify can be an alternative to everit/json-schema usable in Katalon Studio.

So I made a demo project to verify Justify in Katalon Studio.

How to run the demonstration

(1) download the latest zip of this project from Releases page, and unzip it

(2) you need to download external dependencies (jar files) from the Maven Central repository into the Drivers dir. I made build.gradle file. By doing the following operation in command line (here I assume that java is installed in your PC), you can let Gradle to download them automatically:

$ cd <JustifyInKatalonStudio directory>
$ .\gradlew.bat katalonCopyDependencies

Once finished, you will find the Drivers directoy containing the following jars:

  • katalon_generated_icu4j-63.1.jar
  • katalon_generated_javax.json-1.1.4.jar
  • katalon_generated_javax.json-api-1.1.4.jar
  • katalon_generated_justify-0.16.0.jar

(3) start Katalon Studio, open the project

(4) open Test Cases/TC1 and run it.

What TC1 does

TC1 is a bit lengthy code. But it is essentially simple. It reads a JSON Schema from file, reads a JSON data from file, parse it and apply schema. It prints the data in 2 formats (custom format and pretty-print format). If any problem against schema found, report it. The core part is as follows:

Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path credSchemaPath = projectDir.resolve('Include').resolve('resources').resolve('credential-schema.json')
Path credDataPath   = projectDir.resolve('Include').resolve('resources').resolve('credential.json')
Path coordDataPath  = projectDir.resolve('Include').resolve('resources').resolve('geographical-coordinates.json')

JsonValidationService service = JsonValidationService.newInstance()

// Reads the JSON schema
JsonSchema schema = service.readSchema(credSchemaPath);

// Problem handler which will just print messages to stdout.
MyProblemHandler handler1 = new MyProblemHandler()

WebUI.comment("Reads the credentail.json file, validate it to be VALID, print the contents")
service.createReader(credDataPath, schema, handler1).with { reader ->
	try {
		JsonValue value = reader.readValue()
		println MyJsonNavigator.navigate(value)
		println MyJsonPrinter.prettyPrint(value)
	} finally {
	    reader.close()
	}
}
handler1.flush()
println handler1.getOutput()

Json Schema file is:

Sample Json Data files are:

Both data files are found INVALID against the schema. When executed, TC1 emits following output in the console view:

OBJECT
Key firstName: STRING John
Key lastName: STRING Doe
Key age: NUMBER 59

{
    "firstName": "John",
    "lastName": "Doe",
    "age": 59
}

<table>
<thead>
<tr><th>Line No.</th><th>Column No.</th><th>Message</th><th>Assertion Keyword</th></tr>
</thead>
<tbody>
<tr><td>4</td><td>11</td><td>数値は35以下でなければいけません。</td><td>maximum</td></tr>
</tbody>
</table>




OBJECT
Key coordinates: OBJECT
Key latitude: NUMBER 48.858093
Key longitude: NUMBER 2.294694

{
    "coordinates": {
        "latitude": 48.858093,
        "longitude": 2.294694
    }
}

<table>
<thead>
<tr><th>Line No.</th><th>Column No.</th><th>Message</th><th>Assertion Keyword</th></tr>
</thead>
<tbody>
<tr><td>6</td><td>1</td><td>オブジェクトはプロパティ"firstName"を持たなければいけません。</td><td>required</td></tr>
<tr><td>6</td><td>1</td><td>オブジェクトはプロパティ"lastName"を持たなければいけません。</td><td>required</td></tr>
</tbody>
</table>

Justify emits error messages in English or Japanese depending on the locale of you PC.

Conclusion

Justify just works well in Katalon Studio. I think that Justify is a good alternative to the everit/json-schema library.

Personally I would prefer Justify to everit. The API design of Justify looks new, well conformant to the Java 8
and newer API.

6 Likes

@kazurayam I am getting error in the below GIT Project. Could you please help.

"
2021-08-30 22:03:29.603 DEBUG testcase.TC1                             - 4: responseJson = responseBodyContent
2021-08-30 22:03:29.605 DEBUG testcase.TC1                             - 5: result = com.kazurayam.jsonschema4ks.EveritValidator.validate(responseJson, schema)
validate step5
2021-08-30 22:03:29.827 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - ❌ groovy.lang.MissingMethodException: No signature of method: org.json.simple.parser.ParseException.getAllMessages() is applicable for argument types: () values: []
Possible solutions: getMessage()
2021-08-30 22:03:29.844 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/main/TC1 FAILED.
Reason:
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: org.json.simple.parser.ParseException.getAllMessages() is applicable for argument types: () values: []
Possible solutions: getMessage()
	at com.kazurayam.jsonschema4ks.EveritValidator.invokeMethod(EveritValidator.groovy)
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)
	at TC1.run(TC1:58)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:336)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:327)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:306)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:298)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:232)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1630341205910.run(TempTestCase1630341205910.groovy:21)
Caused by: groovy.lang.MissingMethodException: No signature of method: org.json.simple.parser.ParseException.getAllMessages() is applicable for argument types: () values: []
Possible solutions: getMessage()
	at com.kazurayam.jsonschema4ks.EveritValidator.validate(EveritValidator.groovy:48)
	at com.kazurayam.jsonschema4ks.EveritValidator.invokeMethod(EveritValidator.groovy)
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:49)
	at Script1554683586072.run(Script1554683586072.groovy:58)
	... 11 more

2021-08-30 22:03:29.849 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/main/TC1

https://github.com/kazurayam/MattThurmanJSONSchema.

I checked this and made some change:

But unfortunately it does not work.

I have a doubt in the quality of the Everit v1.5.1 library.

In the Maven Central repository, I found another repository named “Everit JSON Validator”

https://mvnrepository.com/artifact/com.github.erosb/everit-json-schema

This repository seemed to be maintained in 2021.

I tried to use the lastest version of “Everit JSON Validator” in my project. I have made:

But When I execute it it throws Exception, I do not see how to fix it. I’m not interested in Everit JSON Validator. I’m not going work on this project.