问题
I have a sample response:
{
"tags": [
{
"id": 1,
"name": "[String]",
"user_id": 1,
"created_at": "2016-12-20T15:50:37.000Z",
"updated_at": "2016-12-20T15:50:37.000Z",
"deleted_at": null
}
]
}
I've written a test for the response:
var schema = {
"type": "object",
"properties": {
"tags": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"user_id": { "type": "number" },
"created_at": { "type": "string" },
"updated_at": { "type": "string" },
"deleted_at": { "type": ["string", "null"] }
}
}
}
};
var data = JSON.parse(responseBody);
tests["Valid schema"] = tv4.validate(data, schema);
This test returns [FAIL]. What wrongs in the test?
Thank you for a respond!
回答1:
There is a problem on the definition of tags
, since it's an array instead of an object. You should nest its properties into its items properties.
This code is passing the test:
test_data = {
"tags": [
{
"id": 1,
"name": "[String]",
"user_id": 1,
"created_at": "2016-12-20T15:50:37.000Z",
"updated_at": "2016-12-20T15:50:37.000Z",
"deleted_at": null
}
]
}
test_schema = {
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"user_id": { "type": "number" },
"created_at": { "type": "string" },
"updated_at": { "type": "string" },
"deleted_at": { "type": ["string", "null"] }
}
}
}
}
};
tests["Testing schema"] = tv4.validate(test_data, test_schema);
console.log("Validation errors: ", tv4.error);
来源:https://stackoverflow.com/questions/41250036/postman-returns-fail-for-schema-validation-test