gpt4 book ai didi

javascript - 针对 JSON 模式验证 JS 测试

转载 作者:行者123 更新时间:2023-11-28 21:22:41 25 4
gpt4 key购买 nike

我有一个以格式返回响应的 API

[
{"id": 12345,
"value": "some_string",
"practice_id": "12344"},

{"id": 12346,
"value": "some_other_string",
"practice_id": "12345"},
]

我正在测试响应是否验证特定的 JSON 模式,我的模式测试是

response.body.should.have.schema({
type: 'array',
required: ['id', 'value', 'practice_id'],
properties: {
id: {
type: 'number',
},
value: {
type: 'string',
},
practice_id: {
type: 'string',
minLength: 5,
}
}
});

问题是,即使我将 id 的类型更改为 string 或将 practice_id 的值更改为 number,测试仍然通过,这是不正确的。

我在这里做错了什么?我正在使用 Postman-BDD验证响应。

最佳答案

我猜你的模式应该更像这样:

{
"type": "array",
"items":
{
"required":
[
"id",
"value",
"practice_id"
],
"properties":
{
"id":
{
"type": "number"
},
"value":
{
"type": "string"
},
"practice_id":
{
"type": "string",
"minLength": 5
}
}
}
}

您缺少实际定义数组内容的“items”关键字。而且这个模式还在 JSONBuddy 中给出了验证一些示例数据的错误: enter image description here

关于javascript - 针对 JSON 模式验证 JS 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47750305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com