@Ibus I have had some luck with the everit solution you provided above but most of my validations are failing. Can you (or someone) please take a look at what I have done?
Thanks,
Matt
When calling like this I get the following error: def responseJson = group_response.getResponseBodyContent().replace("[", “”).replace("]", “”)
com.kms.katalon.core.exception.StepFailedException: #: expected type: JSONArray, found: JSONObject
#: expected type: JSONArray, found: JSONObject
When calling like this I get the following error: def responseJson = group_response.getResponseBodyContent()
org.json.JSONException: A JSONObject text must begin with ‘{’ at 1 [character 2 line 1]
Here is my code, responses, etc.:
Test Code:
//schema file
def schema = new File('./resources/languageGroupsSchema.json').text
KeywordUtil.logInfo("Expected Schema: " + schema)
//send request
def group_response = WS.sendRequest(findTestObject('Object Repository/API/TransAPI/translations_groups'))
//validate schema
def responseJson = group_response.getResponseBodyContent().replace("[", "").replace("]", "")
CustomKeywords.'com.ws.EveritValidator.verifyJsonSchema'(responseJson, schema)
Keyword:
class EveritValidator {
/*** Send request and verify status code
* * @param stringJson Json to validate
* * @param stringSchema validation schema
* */
@Keyword
def verifyJsonSchema(String stringJson, String stringSchema) {
JSONObject rawSchema = new JSONObject(new JSONTokener(stringSchema))
Schema schema = SchemaLoader.load(rawSchema)
try {
schema.validate(new JSONObject(stringJson))
KeywordUtil.markPassed("Schema is valid - PASSED")
} catch (ValidationException e) {
StringBuffer outmessage = new StringBuffer()
outmessage << e.getMessage() << "\n"
e.getAllMessages().each { msg -> outmessage << "$msg \n" }
KeywordUtil.markFailed(outmessage as String)
}
}
}
My JSON Response:
[
{
"ID": 1,
"Name": "Warning"
},
{
"ID": 2,
"Name": “TextArea”
},
{
"ID": 3,
"Name": "LastActions"
},
{
"ID": 4,
"Name": "DayOfWeek"
},
{
"ID": 5,
"Name": "Icons"
},
{
"ID": 6,
"Name": "Direction"
},
{
"ID": 7,
"Name": "Tendency"
},
{
"ID": 8,
"Name": "AirTypes"
},
{
"ID": 9,
"Name": "UCCText"
},
{
"ID": 10,
"Name": "Statements"
}
]
My Schema:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "array",
"items": {
"$ref": "#/definitions/WelcomeElement"
},
"definitions": {
"WelcomeElement": {
"type": "object",
"additionalProperties": false,
"properties": {
"ID": {
"type": "integer"
},
"Name": {
"type": "string"
}
},
"required": [
"ID",
"Name"
],
"title": "WelcomeElement"
}
}
}