1

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:

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

This should return false, as all three properties are strings but its passing. Here is the example response:

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

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.

0

Hi you should first parse the schema to json, else it will be considered as empty data in some cases.

The correct code is as below:

let jsonData = JSON.parse(responseBody);
schema = JSON.parse(schema); 

Now you have to pass recursive validation and nondefined property check to the tv4.validation function.

    pm.test('Response schema type nodes verification', function(){ 
pm.expect(tv4.validate(pm.response.json(), schema, true, true)).to.be.true; });

.....

tv4.validate(pm.response.json(), schema, true, true)

will check the json data recursively and if any new property is present in the resonse data, it fail the validation.

-1

Please try with this Schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "categories": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "aStringOne": {
              "type": "string"
            },
            "aStringTwo": {
              "type": "string"
            },
            "aStringThree": {
              "type": "string"
            }
          },
          "required": [
            "aStringOne",
            "aStringTwo",
            "aStringThree"
          ]
        }
      ]
    }
  },
  "required": [
    "categories"
  ]
}
  • 1
    This needs some explanation to be a useful answer. Answering with a block of code and no explanation does not actually answer the question. – c32hedge Oct 7 at 13:11
  • 1
    Please explain how your schema will fix the OP's problem. Without the explanation, your answer might be technically correct, but it doesn't help anyone understand what the problem is or how to adapt your answer to their situation. – Kate Paulk Oct 7 at 14:10
  • I also tried with the schema that was defined in the question but it doesn't work but when I generated a new schema using the JSON provided and used it, it worked for me. Next time I will take care of the explanation too. – Abhishek Kumar Oct 9 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.