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

标签 javascript json testing postman jsonschema

我有一个以格式返回响应的 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/

相关文章:

javascript - JSON 属性最佳实践

测试策略 : Assert on 'features' of response or assert on full response?

javascript - 在没有代码库的情况下测试 JavaScript

javascript - 使用css或js关闭时如何将bootstrap 4模态对话框向右滑动

javascript - 函数调用作为参数值?

javascript - jQuery 事件绑定(bind)不一致

javascript - 如何从菜单预加载 html 链接并使用 javascript 隐藏/显示内容?

jquery - 从发布的 JSON 获取经典 ASP 变量

javascript - Node.js 的 REST API 无法同时向自身发出请求

python - 使用单击选项 python 测试 cli 命令