【问题标题】:JSON tiny validator test doesn't work properly in POSTMANJSON 微型验证器测试在 POSTMAN 中无法正常工作
【发布时间】:2017-04-14 20:55:06
【问题描述】:

[https://i.stack.imgur.com/qeFuv.png][1]

我对 CODE sn-ps,特别是 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 数据,而不是在正文选项卡中),测试仍然通过。 [https://i.stack.imgur.com/8Dq7B.png][1]

有什么问题???你能展示一下如何创建那种东西的例子吗? 最好的祝福, 阿尔塞姆。

【问题讨论】:

    标签: json postman


    【解决方案1】:

    我遇到了类似的问题,下面来自here 的示例帮助我解决了它。您似乎缺少“属性”实体。

    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);  
    

    【讨论】:

    • 你节省了我的时间,朋友。请注意,当前 JSON Schema 草案为 07:"$schema": "http://json-schema.org/draft-07/schema#"
    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多