简体   繁体   English

如何使用tv4在postman中测试JSON模式?

[英]How do I test JSON schema in postman using tv4?

This is what i am trying, but it always passes the test even for bad results. 这就是我正在尝试的,但它总是通过测试,即使是糟糕的结果。

pm.test("Schema is valid", function () {
    var data = pm.response.json();
    var schema = {
        ...
        my schema
        ...
    };
    tv4.validate(data, schema);
});

The reason this doesn't work is (in short) that the underlying library used by Postman (tv4) is no longer maintained. 这不起作用的原因(简而言之)是Postman(tv4)使用的底层库不再维护。 Having come across the problem earlier today, I found a solution : 在今天早些时候遇到这个问题后,我找到了一个解决方案

tv4.validate(data, schema, false, true)

The latter two parameters are checkRecursive and banUnknownProperties . 后两个参数是checkRecursive和banUnknownProperties Setting these two flags as shown above make the validation work as expected. 如上所示设置这两个标志使验证按预期工作。

You may also find this code snippet useful, which reports any validation errors via the console: 您可能还会发现此代码段很有用,它会通过控制台报告任何验证错误:

pm.test("Response body is valid", function() {
  var data = JSON.parse(responseBody);
  var valid = tv4.validate(data, schema, false, true);
  if (valid !== true) {
      console.log(tv4.error);
  }
  pm.expect(valid).to.be.true;
});

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

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