[https://i.stack.imgur.com/qeFuv.png][1]
我对CODE片段,特别是JSON微型验证器(2)和测试标签(1)几乎没有疑问 我正在发送以下POST请求({{host}}:{{port}} / landlords / {{DC_id}} / apartments)来创建公寓的实例。 请求正文如下:
{
"address": "Zaharova Street 30",
"price": 200,
"square": 20,
"features": [
"Good Shopping Facilities","Metro","Recreation area nearby","Friendly and calm neighboors"
],
"active": true
}
确切的JSON验证测试如下:
var schema = {
"items": {
"address": "Zaharova Street 30",
"price": 200,
"square": 20,
"features": [
"Good Shopping Facilities","Metro","Recreation area nearby","Friendly and calm neighboors"
],
"active": true
}
};
var data1 = [true, false];
var data2 = [true, 123];
tests["Valid Data1"] = tv4.validate(data1, schema);
tests["Valid Data2"] = tv4.validate(data2, schema);
console.log("Validation failed: ", tv4.error);
我决定使用此测试以确保公寓是使用请求正文中使用的确切参数创建的。
但我发现如果我在JSON验证测试(1)中更改任何值,并重新发送响应(我只在测试选项卡中更改了JSON数据,而不是在body选项卡中),测试仍然被传递。
[https://i.stack.imgur.com/8Dq7B.png][1]
有什么问题?你能举例说明如何创造这种东西吗? 最好的祝福, Artsem。
答案 0 :(得分:6)
我遇到了类似的问题,here下面的例子帮助我解决了这个问题。你似乎失踪了#34;属性"实体。
var schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"type" : "array",
"items" : {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"title": {
"type": "string"
},
"url": {
"type": "string"
},
"state": {
"type": "string"
},
"body": {
"type": "string"
},
"user": {
"type" : ["null", "object"],
"properties" : {
"id": {
"type": "integer"
},
"login": {
"type": "string"
}
},
"additionalProperties": true,
"required": [ "id", "login" ]
},
},
"additionalProperties": true,
"required": [ "id", "title", "state", "body", "user", "url"]
},
}
tests["Valid issues schema"] = tv4.validate(issues, schema);
console.log(tv4.error);