2

I'm having trouble validating a schema in Postman using tv4 inside the tests tab - it is always returning a true test, no matter what I feed it. I am at a complete loss and could really use a hand - here is my example JSON Response, and my tests:

I've tried a ton of variations from every Stack Overflow/tutorial I could find and nothing will work - it always returns true.

//Test Example 

var jsonData = JSON.parse(responseBody);
const schema = {
"required" : ["categories"],
"properties": {
"categories": {
    "required" : ["aStringOne", "aStringTwo", "aStringThree" ],
    "type": "array",
    "properties" : {
        "aStringOne": {"type": "string" },
        "aStringTwo": {"type": "null" },
        "aStringThree": {"type": "boolean" }
    }
}
}
};

pm.test('Schema is present and accurate', () => {
var result=tv4.validateMultiple(jsonData, schema);
console.log(result);
pm.expect(result.valid).to.be.true;
});

//Response Example

{
"categories": [
{
    "aStringOne": "31000",
    "aStringTwo": "Yarp",
    "aStringThree": "More Yarp Indeed"
}
]
}

This should return false, as all three properties are strings but its passing. I'm willing to use a different validator or another technique as long as I can export it as a postman collection to use with newman in my CI/CD process. I look forward to any help you can give.

Haney
  • 23
  • 1
  • 6

1 Answers1

6

I would suggest moving away from using tv4 in Postman, the project isn't actively supported and Postman now includes a better (in my opinion), more actively maintained option called Ajv.

The syntax is slightly different but hopefully, this gives you an idea of how it could work for you.

I've mocked out your data and just added everything into the Tests tab - If you change the jsonData variable to pm.response.json() it will run against the actual response body.

var jsonData = {
    "categories": [
        {
            "aStringOne": "31000",
            "aStringTwo": "Yarp",
            "aStringThree": "More Yarp Indeed"
        }
    ]
}



var Ajv = require('ajv'),
    ajv = new Ajv({logger: console, allErrors: true}),
    schema =  {
    "type": "object",
    "required": [ "categories"],
    "properties": {
      "categories": {
          "type": "array",
          "items": {
              "type": "object",
              "required": [ "aStringOne", "aStringTwo", "aStringThree" ],
              "properties": {
                  "aStringOne": { "type": "string" },
                  "aStringTwo": { "type": "integer"},
                  "aStringThree": { "type": "boolean"},
         }
       }
     }
   }
}

pm.test('Schema is valid', function() {
    pm.expect(ajv.validate(schema, jsonData), JSON.stringify(ajv.errors)).to.be.true
});

This is an example of it failing, I've included the allErrors flag so that it will return all the errors rather than just the first one it sees. In the pm.expect() method, I've added JSON.stringify(ajv.errors) so you can see the error in the Test Result tab. It's a little bit messy and could be tidied up but all the error information is there.

Postman Failing 1

Setting the properties to string show the validation passing:

Postman Passing

If one of the required Keys is not there, it will also error for this too:

Postman Failing 2

Working with schemas is quite difficult and it's not easy to both create them (nested arrays and objects are tricky) and ensure they are doing what you want to do.

There are occasions where I thought something should fail and it passed the validation test. It just takes a bit of learning/practising and once you understand the schema structures, they can become extremely useful.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80