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

≡放荡痞女 提交于 2019-12-11 02:00:33

问题


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


回答1:


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.




回答2:


@DieGraueEminenz's suggestion could be helpful, but I think there might also be something else going on there, I'm curious how you could get a reference to "JsonWebTokenError" with what you're doing, you should investigate that.

But regarding your schema problem, lets remove a few unknowns by using the following code in either the pre-request or test scripts of a dummy request (eg GET google.com)

var data = [
  {
    "dayOfWeek": "sundayX",
    "openTime": "10:00:00",
    "closeTime": "14:00:00"
  },
  {
    "dayOfWeek": "monday",
    "openTime": "9:00:00",
    "closeTime": "16:00:00"
  }
];

const schema = {
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "required": [ "dayOfWeek", "openTime", "closeTime" ],
    "properties": {
      "dayOfWeek":  { "enum": [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday",  ] },
      "openTime": { "type": "string", "pattern": "^\\d+:\\d\\d:\\d\\d$" },
      "closeTime": { "type": "string", "pattern": "^\\d+:\\d\\d:\\d\\d$" },
    }
  }
};

const result = tv4.validateResult(data, schema);  
console.log("Schema result:", result);

I reworked the schema you get from https://jsonschema.net/, (which actually only checks that you've got strings, nothing further) The enum above for dayOfWeek does a better check, the times values now need to be [n]n:nn:nn, and I removed a bunch of extra bits you don't need.

Also I'd use the tv4.validateResult(...) method because it gives you a lot more info on the errors.

The above example output is

to pick out the intentional error in data's first item.

Once you've got that going start connecting the data through from your response, and if you need to share the schema with other requests save it to the globals with

pm.globals.set("schemaJSON",JSON.stringify(schema));

and fetch it as follows

const schemaJSON = pm.globals.has("schemaJSON") && pm.globals.get("schemaJSON");
const schema = schemaJSON && JSON.parse(schemaJSON);


来源:https://stackoverflow.com/questions/56212298/tv4-validate-always-return-true-even-if-my-schema-not-valid-i-e-var-dummy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!