jsonschema - 如何在空手道特定的 json 模式中使用模板
问题描述
我在空手道下面尝试。
我在 .json 文件中有一个 json 模式(用于响应验证)。在许多模式中,很少有常见的 REGEX。我想将它们作为键值对提取到一个通用文件中,并在其他模式中使用它。可能吗?如果是这样,我该怎么做?json 模式中是否允许模板化?
例子:
示例 Json 架构文件 (sample-response.json):
{
"response": {
"name": "#string",
"amount": "#regex ^(-?)([0]|[1-9][0-9]{0,15})[.][0-9]{2}$"
}
}
特征文件
Feature: Example feature
Background:
* def sampleResponse = read('./sample-response.json');
Scenario: Example scenario
When url 'https://someurl.com'
And method get
Then status 200
And match response == sampleResponse
我想做什么?
我想将数量正则表达式存储在 json 文件中作为可重用变量,并使用 json 文件中的模板来替换它。可能吗?
{
"response": {
"name": "#string",
"amount": "{{get regex from this template}}"
}
}
解决方案
所以这样做:
{
"response": {
"name": "#string",
"amount": "#(amount)"
}
}
然后这样做:
Background:
* def amount = 100
* def sampleResponse = read('sample-response.json')
如果您希望amount
来自另一个 JSON 文件,为什么不呢,请说以下内容data.json
:
{ "amount": 100 }
然后你这样做:
Background:
* def data = read('data.json')
# you don't need the next line if you use "data.amount" as the embedded expression
* def amount = data.amount
* def sampleResponse = read('sample-response.json')
推荐阅读
- c++ - 为什么这段代码不直接使用 C++ 幂函数?有人可以帮助我使用此代码中的电源功能吗?
- python - 如何将 csv 转换并格式化为包含标题名称的列表列表
- kotlin - 为什么 Kotlin 的 Number-class 缺少运算符?
- r - 如何将原始向量从 sqlQuery 转换为数据框中的单个字符串?
- mysql - 一大列还是许多小行?
- c# - 如何使用 Microsoft Graph API 从包含嵌套组的组中仅获取用户?
- julia - (Julia 1.x) 获取#undef 变量的类型
- django - Django prefetch_related 似乎无法按预期工作
- vue.js - 如何从 Vuex 操作中的 axios 函数返回错误消息以调度 catch(error)?
- laravel - subCategories 函数已定义,但我得到的只是调用未定义的方法 Illuminate\Database\Eloquent\Builder::subCategories()"