Javascript 根据JSON模式验证JS测试

Javascript 根据JSON模式验证JS测试,javascript,json,testing,postman,jsonschema,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',

我有一个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,测试也会通过,这是不正确的


我做错了什么?我正在使用验证响应。

我想您的模式应该更像这样:

{
  "type": "array",
  "items":
  {
    "required":
    [
        "id",
        "value",
        "practice_id"
    ],
    "properties":
    {
        "id":
        {
            "type": "number"
        },
        "value":
        {
            "type": "string"
        },
        "practice_id":
        {
            "type": "string",
            "minLength": 5
        }
    }
  }
}
您缺少实际定义数组内容的items关键字。此模式还提供了JSONBuddy在验证某些示例数据时的错误:

字符串与数字和id一起使用,但数字和id不能包含字符串。尝试将值设置为number,您应该会得到一个错误当响应是一个对象数组时,是否需要添加项?如果我对json对象响应使用模式,那么它的工作效果会更好。是的,如果要指定有效的数组元素,则需要对数组使用项。还请记住,JSON模式默认描述开放数据模型。这意味着空JSON模式允许任何内容。注意。非常感谢。