简体   繁体   English

如何在空手道中针对预定义的json模式验证响应?

[英]how can I verify response against a predefined json schema in karate?

Currently for checking answer response IO use below method: 当前用于检查答案响应IO的方法如下:

And match response ==
"""
  {
    "status":#number,
    "message":#string
  }
"""

Is there any way to do like below? 有什么办法可以像下面这样吗?

And match response == someJsonSchemaDefinedInKarateConfigFile

Yes, refer to the documentation on reading files . 是的,请参阅有关读取文件的文档。

And match response == read('my-schema.json')

(edit): There was a comment requesting how to initialize these in karate-config.js (编辑):有一条评论要求如何在karate-config.js初始化它们

karate-config.js is intended for 'global' config, I really don't recommend dumping schemas here unless you are sure it will be used by almost all of your tests. karate-config.js用于“全局”配置,我真的不建议在这里转储模式,除非您确定几乎所有测试都将使用它。 But if you are reading from a file, it might be ok as it won't be a time consuming operation, remember karate-config.js is re-loaded for every Scenario . 但是,如果您正在从文件中读取文件,那可能没问题,因为它不会花费很多时间,请记住,对于每个 Scenario都会重新加载karate-config.js

Within karate-config.js you can easily load a JSON or JS file by using karate.read() . karate.read() karate-config.js您可以使用karate.read()轻松加载JSON或JS文件。 This should answer your question: 这应该可以回答您的问题:

function() {
  var config = {

  };
  config.mySchema = karate.read('classpath:my-schema.json');
  return config;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式针对架构验证JSON字符串? - How can I validate a JSON string against a schema programmatically? 如何在 Jmeter 中使用 groovy 验证嵌套的 JSON 响应? - How can I verify a nested JSON response using groovy in Jmeter? 任何人都可以解释如何将JSON Schema链接到我的JSON以验证JSON - Can anyone explain how can i link the JSON Schema to my JSON to validate the JSON against 我应如何针对模式验证json - how shall i validate json against schema 如何针对摇摇欲坠的模式验证JSON模式的实例? - How do I validate an instance of JSON schema against a swagger schema? 如何验证 JSON 数据是否满足 JSON 架构规范? - How do I verify if a JSON Data satisfy a JSON Schema specification? 当响应多次包含相同的密钥时,如何使用 groovy 验证我的 JSON 响应? - How can I verify my JSON response using groovy when the response contains same key multiple times? 如何在空手道中将 Json Object 转换为 Json 数组? - How can I convert to Json Object to Json Array in Karate? 如何根据 XML Schema (XSD) 或 RelaxNG 验证 JSON? - How do I validate JSON against XML Schema (XSD) or RelaxNG? 如何验证 JSON 架构文件的有效性? - How do I verify the validity of a JSON Schema file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM