I have this little issue. I am building API tests with postman. One of my tests want to validate a JSON response.
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.