You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Katalon中无法调用validateJsonAgainstSchema方法的求助

问题解决步骤

1. 修正JSON Schema语法错误

你的JSON Schema多了一个多余的闭合括号},导致Schema本身不合法。修正后的Schema如下:

String jsonPass = """
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    }
  }
}
"""

2. 修正参数类型不匹配问题

resp.getResponseBodyContent()返回的是字符串类型,不能直接赋值给ResponseObject变量。根据你的需求,有两种正确写法:

写法一:直接传入ResponseObject实例

如果resp本身是通过WS.sendRequest获取的ResponseObject对象,直接传这个对象即可:

import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

String jsonPass = """
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    }
  }
}
"""
// 假设resp是已获取的ResponseObject实例
ResponseObject response = resp
boolean successful = WS.validateJsonAgainstSchema(response, jsonPass)

写法二:传入响应体字符串

如果需要用响应体的字符串内容验证,直接接收字符串并传入方法:

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

String jsonPass = """
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    }
  }
}
"""
String responseBody = resp.getResponseBodyContent()
boolean successful = WS.validateJsonAgainstSchema(responseBody, jsonPass)

3. 额外检查项

  • 确认你的Katalon版本支持WS.validateJsonAgainstSchema方法:该方法在较新的Katalon版本中引入,若版本过旧请升级到稳定版。
  • 提前验证JSON Schema合法性:可以用本地或离线的JSON Schema校验工具确认Schema语法无误,避免因Schema本身问题导致方法调用失败。

内容的提问来源于stack exchange,提问作者TaraNG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 00:05:21