[WS] Validate JSON string against a schema (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.
Description
Validate a JSON response body, request body, or string against a JSON schema. The JSON schema input can be a JSON string, URL, or file path.
Parameters
Validate a JSON Object with a JSON Schema
Parameter | Parameter Type | Mandatory | Description |
---|---|---|---|
jsonObject | String | Required | Specify the JSON object that needs to be validated |
jsonSchema | String | Required | Specify the JSON schema used to validate the JSON object. |
flowControl | FailureHandling | Optional | Specify failure handling schema to determine whether the execution should be allowed to continue or stop. |
Validate the Request Response with a JSON Schema
Parameter | Parameter Type | Mandatory | Description |
---|---|---|---|
response | ResponseObject | Required | Specify the response object that needs to be validated |
jsonSchema | String | Required | Specify the JSON schema used to validate the response object. |
flowControl | FailureHandling | Optional | Specify failure handling schema to determine whether the execution should be allowed to continue or stop. |
Returns
Parameter Type | Description |
---|---|
boolean |
|
Exception:
If Katalon Studio cannot find the schema file or the response doesn't pass the validation, throw: StepFailedException.
Example
JSON Object Validation
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
String jsonPass =
"""
{
"\$id": "https://example.com/person.schema.json",
"\$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}
"""
String jsonObject =
"""
{
"firstName": "White"
"lastName": "Walter"
"age": 52
}
"""
boolean successful = WS.validateJsonSchema(jsonObject,jsonPass)
Response Validation
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webservice.verification.WSResponseManager
ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()
String jsonPass =
"""
{
"\$id": "https://example.com/person.schema.json",
"\$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}
"""
boolean successful = WS.validateJsonSchema(response,jsonPass)