[英]Postman Json Schema validation failed
I have this little issue. 我有这个小问题。 I am building API tests with postman.
我正在与邮递员建立API测试。 One of my tests want to validate a Json response.
我的测试之一想验证Json响应。
This is the kind of response that I have received: 这是我收到的回应:
{
"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"
},
...
...
]};
So I receive an array of objects like this one above. 因此,我在上面收到了类似这样的对象数组。 This is the test that I wrote:
这是我编写的测试:
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));
});
But the test fails: 但是测试失败:
Schema validation failed: unknown property (not in schema)
模式验证失败:未知属性(不在模式中)
Obviously I am doing something wrong with the schema, but I do not understand what. 显然,我在架构上做错了事,但是我不明白是什么。
Your schema is incorrect. 您的架构不正确。 It should be like this.
应该是这样
{
"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"
]
}
}
}
And data should look like, 数据应该看起来像
{
"foo": 12345,
"bar": "a"
}
Refer below link for more examples , like Array/Objects etc. 请参阅下面的链接以获取更多示例 ,例如数组/对象等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.