6

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);
});
srayner
  • 1,799
  • 4
  • 23
  • 39
  • Possible duplicate of [POSTMAN - Schema validation is passed even for bad response data](https://stackoverflow.com/questions/48012722/postman-schema-validation-is-passed-even-for-bad-response-data) – Danny Dainton Jan 12 '18 at 17:54

1 Answers1

12

The reason this doesn't work is (in short) that the underlying library used by Postman (tv4) is no longer maintained. 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. 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;
});
  • I`m not able to comment yet to comment on @Justin Finkelstein answer, so : The answer he provide and all other related articles answers does not work. It will still return true. Even if your schema is : ``` var schema = "dummy text, not real schema " ``` it will still say that your schema is valid I`m tired looking for solution. Spend whole 12 hours – Denys Moroz May 20 '19 at 02:26