针对 JSON 模式验证 JS 测试

Validating JS Tests against a JSON Schema

我有一个 API returns 格式的响应

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

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

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

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 中给出了验证一些示例数据的错误: