How to validate the JSON response with expected JSON schema

I made a demo project on Github: https://github.com/kazurayam/MattThurmanJSONSchema


You shoud use org.json.JSONArray to parse your JSON Response, rather than org.json.JSONObject.

package com.ws

import org.everit.json.schema.Schema
import org.everit.json.schema.loader.SchemaLoader
import org.everit.json.schema.ValidationException
import org.json.JSONArray
import org.json.JSONObject
import org.json.JSONTokener
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.annotation.Keyword

class EveritValidator {
	@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))
			schema.validate(new JSONArray(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)
		}
	}
}