邮递员Json架构验证失败
我有个小问题。我正在与postman一起构建API测试。 我的一个测试想要验证Json响应 这就是我收到的回应:邮递员Json架构验证失败,json,api,automated-tests,postman,Json,Api,Automated Tests,Postman,我有个小问题。我正在与postman一起构建API测试。 我的一个测试想要验证Json响应 这就是我收到的回应: { "comuni": [ { "istat": "015002", "code": "A010", "comune": "ABBIATEGRASSO", "provincia": "MI", "cap": "20081", "latitud
{
"comuni": [
{
"istat": "015002",
"code": "A010",
"comune": "ABBIATEGRASSO",
"provincia": "MI",
"cap": "20081",
"latitude": 45.393036,
"longitude": 8.919824,
"soppresso": false,
"regione": "Lombardia",
"parte_italia": "nord",
"is_provincia": 0,
"nome_provincia": "Milano"
},
...
...
]};
所以我收到了一个像上面这样的对象数组。
这是我写的测试:
var schema = {
"comuni" :
[
{
"istat" : {
"type" : "Integer"
},
"code" : {
"type" : "string"
},
"comune" : {
"type" : "string"
},
"provincia" : {
"type" : "string"
},
"cap" : {
"type" : "integer"
},
"latitude" : {
"type": "Number"
},
"longitude" : {
"type": "Number"
},
"soppresso": {
"tyoe" : "boolean"
},
"regione" : {
"type" : "string"
},
"parte_italia": {
"type": "string"
},
"is_provincia": {
"type": "integer"
},
"nome_provincia": {
"type": "string"
}
}]
}
pm.test("JSON schema validation", function() {
var paperwork = pm.response.json();
var result = tv4.validate(paperwork, schema, false, true);
if (result !== true) {
console.log('Schema validation failed:', tv4.error);
}
/*console.log(tv4.error.dataPath);*/
pm.expect(result).to.be.true;
console.log(JSON.stringify(result));
});
但测试失败了:
架构验证失败:未知属性(不在架构中)
很明显,我的模式有问题,但我不明白是什么。您的模式不正确。应该是这样的
{
"description": "Any validation failures are shown in the right-hand Messages pane.",
"type": "object",
"properties": {
"foo": {
"type": "number"
},
"bar": {
"type": "string",
"enum": [
"a",
"b",
"c"
]
}
}
}
数据应该是这样的
{
"foo": 12345,
"bar": "a"
}
更多信息,如数组/对象等,请参阅下面的链接。不,我没有写最后一个]}因为我只写了第一个对象。结果是“comuni”,这是一个对象数组,就像上面你说的,你没有写]},你到底是什么意思?您的数据中是否有这个问题,但您没有提出原始问题?根据POSTMAN使用的json-schema4.0版本,您的模式定义不正确。我将修改我的上述答案,并尝试修改您的模式,它应该会工作。