tv4.validate 将始终返回 true。无论模式是有效的 JSON 还是只是虚拟字符串。我浏览 stackoverflow 以查找相关问题,而 banUnknownProperties 对我没有帮助
正如我所说,我什至尝试将模式变量更改为“虚拟”,而 tv4 仍然没有发现错误。那是我在 stackoverflow 上的第一篇文章。对不起,如果我的问题不清楚。
有效回复如下
[
{
"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"
}
]
我使用多线程建议的https://jsonschema.net/获得了我的模式变量。您可以粘贴有效响应并将此 JSON 推断为 Schema。我不会在这里提供我的架构以节省空间。
这是我的测试代码:
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 在这里看不到任何错误