postmanjson-schema-validatortv4

tv4.validate always return true. Even if my schema not valid i.e.: var == "dummy"


tv4.validate will always return true. No matter if schema is valid JSON or even just dummy string. I browse stackoverflow for related issues and banUnknownProperties does not help me

As i told i even tried to change schema variable to "dummy" and tv4 still does not find error. That`s my first post on stackoverflow. Sorry if my question not clear.

Valid response will be as following

[
  {
    "dayOfWeek": "sunday",
    "openTime": "10:00:00",
    "closeTime": "14:00:00"
  },
  {
    "dayOfWeek": "monday",
    "openTime": "9:00:00",
    "closeTime": "16:00:00"
  },
  {
    "dayOfWeek": "tuesday",
    "openTime": "9:00:00",
    "closeTime": "16:00:00"
  },
  {
    "dayOfWeek": "wednesday",
    "openTime": "9:00:00",
    "closeTime": "16:00:00"
  },
  {
    "dayOfWeek": "thursday",
    "openTime": "9:00:00",
    "closeTime": "16:00:00"
  },
  {
    "dayOfWeek": "friday",
    "openTime": "9:00:00",
    "closeTime": "16:00:00"
  },
  {
    "dayOfWeek": "saturday",
    "openTime": "7:00:00",
    "closeTime": "19:00:00"
  }
]

I got my schema variable using https://jsonschema.net/ which was suggested by multiple threads. You can paste valid response and infer this JSON to SChema. I`m not going to provide my schema here to save space.

Here is my test code:

var data = JSON.parse(responseBody);
var schema =pm.variables.get("getHoursSchema"); // copy paste schema from https://jsonschema.net/ and assigned to 'getHoursSchema' environment variable

tests["Valid 'Get business hours' schema"] = tv4.validate(data, schema, false, true);  
console.log("Schema error description, if any:" + tv4.error);

Actual response is :

{
    "error": {
        "name": "JsonWebTokenError",
        "message": "jwt malformed"
    }
}

And tv4 does not see any errors here


Solution

  • Postman Variables are stored as strings. TV4 expects an Object.

    So just try to wrap a JSON.parse over your pm.variables.get:

    var schema = JSON.parse(pm.variables.get("getHoursSchema")); 
    

    With this change, i've got a schema validation error as expected.