POSTMAN - 即使是错误的响应数据也会传递模式验证

时间:2017-12-28 18:42:52

标签: json validation schema postman

tests [“Valid schema”] = tv4.validate(jsonData,schema);即使模式中缺少“error”和“responseType”,也会传递。如何确保响应和模式都与JSON模式匹配。

当我点击邮递员的邮寄请求时,以下是邮递员的响应主体

{  
  "statusCode": 400,
  "error": "Bad Request",
  "message": "Email/Phone number not found",
  "responseType": "EMAIL_NOT_FOUND",
  "arabicMessage": "البريد الإلكتروني / رقم الهاتف غير موجود"
  }

邮差测试

var jsonData=JSON.parse(responseBody)
var schema ={
    "statusCode": {"type":"integer"},
    "message": {"type":"string"},
    "arabicMessage":{"type":"string"},
    "data": {
        "accessToken": {"type":"string"},
        "userDetails": {
            "_id": {"type":"string"},
            "deviceType": {"type":"string"},
            "countryCode": {"type":"string"},
            "OTPCode": {"type":"integer"},
            "invitationCode": {"type":"string"},
            "availableCredits": {"type":"integer"},
            "totalBookings": {"type":"integer"},
            "promoCodes": {"type":"array"},
            "updatedAt": {"type":"string"},
            "createdAt": {"type":"string"},
            "language": {"type":"string"},
            "IsDeleted": {"type":"boolean"},
            "IsVerified": {"type":"boolean"},
            "IsBlock": {"type":"boolean"},
            "customerAddresses": {"type":"array"},
            "address":{"type":"string"},
            "phoneVerified": {"type":"boolean"},
            "currentLocation": {
                "type": "Point",
                "coordinates": [
                   {"type":"integer"},
                   {"type":"integer"}
                ]
            },
            "appVersion": {"type":"integer"},
            "profilePicURL": {
                "thumbnail": {"type":"string"},
                "original": {"type":"string"}
            },
            "password":  {"type":"string"},
            "socialId": {"type":"string"},
            "phoneNo": {"type":"integer"},
            "email": {"type":"string"},
            "LastName": {"type":"string"},
            "firstName": {"type":"string"},
            "__v": {"type":"integer"},
            "referralCode":  {"type":"string"},
            "accessToken": {"type":"string"},
            "deviceToken":  {"type":"string"}
        },
        "updateAvailable": {"type":"boolean"},
        "stateCallBookingIds":  {"type":"array"},
        "forceUpdate": {"type":"boolean"}
    }
 };
tests["Valid schema"] = tv4.validate(jsonData, schema);
//here the test is passing even with invalid jsonData which is the data                       
 console.log("Validation failed: ", tv4.error);

2 个答案:

答案 0 :(得分:2)

Postman github帐户中有很多关于jsonData模块的open issues

SO here上有一个类似的问题,您的"title": "Person", "type": "object", "properties": { "firstName": { "type": "string" }, "lastName": { "type": "string" }, "age": { "description": "Age in years", "type": "integer", "minimum": 0 } }, "required": ["firstName", "lastName"] } 可能与您的架构不同吗?

这是来自tv4 github页面链接的example

required

您可以尝试将这些字段添加为In [323]: arr = np.arange(12).reshape(3,4) In [324]: alist = list(range(3)) In [325]: np.concatenate((arr,alist)) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-325-9b72583c40de> in <module>() ----> 1 np.concatenate((arr,alist)) ValueError: all the input arrays must have same number of dimensions In [326]: arr.shape Out[326]: (3, 4)

答案 1 :(得分:1)

将它留在这里以防万一它可以帮助其他人。 tv4.validate还有两个布尔参数:checkRecursivebanUnkownProperties

特别是最后一个可以帮助在JSON响应中包含未在模式中定义的属性时查找错误。

Reference