由网友(陌颜〆)分享简介:我有一个示例响应:{"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": nul...
我有一个示例响应:
{
"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);
此测试返回 [FAIL].测试中有哪些错误?
This test returns [FAIL]. What wrongs in the test?
感谢您的回复!
推荐答案
tags
的定义有问题,因为它是一个数组而不是一个对象.您应该将它的属性嵌套到它的 items 属性中.
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.
这段代码通过了测试:
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);
相关推荐
最新文章