本文介绍了tv4.validate始终返回true。即使我的架构无效,也就是:var ==“ dummy”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tv4.validate将始终返回true。无论架构是有效的JSON还是仅仅是假字符串。我浏览了stackoverflow以查找相关问题,并且banUnknownProperties并没有帮助我

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

我告诉我什至尝试将架构变量更改为虚拟,并且tv4仍然找不到错误。那是我关于stackoverflow的第一篇文章。对不起,如果我的问题不清楚。

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.

有效响应如下:

[
  {
    "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"
  }
]

我使用由多个线程建议。
您可以粘贴有效响应并将此JSON推断到SChema。我不会在这里提供我的模式以节省空间。

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.

这是我的测试代码:

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

实际响应是:

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

tv4在这里看不到任何错误

And tv4 does not see any errors here

推荐答案

邮递员变量存储为字符串。
TV4需要一个对象。

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

因此,只需尝试在pm.variables.get上包装一个JSON.parse:

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.

这篇关于tv4.validate始终返回true。即使我的架构无效,也就是:var ==“ dummy”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 05:56