简体   繁体   English

POSTMAN-即使对于错误的响应数据,也会通过架构验证

[英]POSTMAN - Schema validation is passed even for bad response data

tests["Valid schema"] = tv4.validate(jsonData, schema); tests [“有效模式”] = tv4.validate(jsonData,schema); is passed even if "error" and "responseType" is missing in the schema. 即使架构中缺少“错误”和“ responseType”,也会传递。 How to make sure that response and schema both are matching for JSON schema. 如何确保响应和模式都与JSON模式匹配。

when I hit post request on postman, the following is the Response Body in postman 当我在邮递员上遇到邮递请求时,以下是邮递员中的响应正文

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

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

There are lots of open issues on the Postman github account about the tv4 module. 关于tv4模块,Postman github帐户上存在许多未解决的问题

There is a similar question on SO here , could your jsonData be different than your schema? 有一个类似的问题,以便在这里 ,你可以jsonData比你的模式有什么不同?

This is an example from a link on the tv4 github page. 这是来自tv4 github页面上的链接的示例

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

You could try adding those fields as required ? 您可以尝试根据required添加这些字段吗?

Just leaving this here in case it helps anyone else. 只是把它留在这里,以防其他人受益。 tv4.validate has two additional boolean parameters: checkRecursive and banUnkownProperties . tv4.validate有两个附加的布尔参数: checkRecursivebanUnkownProperties

Especially the last one can help finding errors in JSON responses when they contain attributes that weren't defined in the schema. 特别是当最后一个包含模式中未定义的属性时,最后一个可以帮助发现JSON响应中的错误。

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM