【问题标题】:POSTMAN - Schema validation is passed even for bad response dataPOSTMAN - 即使响应数据错误,也会通过模式验证
【发布时间】:2018-06-09 07:28:10
【问题描述】:

tests["有效模式"] = tv4.validate(jsonData, schema);即使架构中缺少“error”和“responseType”,也会通过。如何确保响应和架构都与 JSON 架构匹配。

当我在 postman 上点击 post 请求时,以下是 postman 中的响应正文

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

Postman 中的测试

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

【问题讨论】:

  • 您能否发布一张 Postman 输出的图像,以便我们查看它的验证对象?
  • 仅供参考,我上面提到的响应主体本身就是邮递员输出@DannyDainton

标签: json validation schema postman


【解决方案1】:

Postman github 账号上有很多关于tv4 模块的open issues

在 SO here 上有一个类似的问题,您的 jsonData 可能与您的架构不同吗?

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

"title": "Person",
"type": "object",
"properties": {
    "firstName": {
        "type": "string"
    },
    "lastName": {
        "type": "string"
    },
    "age": {
        "description": "Age in years",
        "type": "integer",
        "minimum": 0
    }
},
"required": ["firstName", "lastName"]
}

您可以尝试将这些字段添加为required

【讨论】:

  • 谢谢,我再次尝试使用“属性”和“必需”。它现在正在工作。
  • @RanjithKasu 酷,这回答了问题吗?
【解决方案2】:

把这个留在这里,以防它帮助其他人。 tv4.validate 有两个额外的布尔参数:checkRecursivebanUnkownProperties

当 JSON 响应包含架构中未定义的属性时,尤其是最后一个可以帮助发现错误。

Reference

【讨论】:

    猜你喜欢
    • 2018-07-23
    • 2018-04-18
    • 1970-01-01
    • 2020-12-02
    • 2019-04-22
    • 2014-01-25
    • 1970-01-01
    • 2015-12-15
    • 2023-02-03
    相关资源
    最近更新 更多