Vya*_*lav 2 schema json postman tv4
我有一个示例回复:
{
"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
}
]
}
Run Code Online (Sandbox Code Playgroud)
我已经为响应写了一个测试:
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);
Run Code Online (Sandbox Code Playgroud)
此测试返回[FAIL].测试中有什么错误?
谢谢你的回复!
小智 7
定义存在问题tags,因为它是一个数组而不是一个对象.您应该将其属性嵌套到其items属性中.
这段代码通过了测试:
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4280 次 |
| 最近记录: |