标签: tv4

邮递员:更具描述性的TV4验证错误消息

我正在使用邮递员来验证从api返回的json数据的架构。

我有一个通过基本的HTTP验证运行的测试,然后以:

if (tv4.error){
    console.log("Validation failed: ", tv4.error);
}
Run Code Online (Sandbox Code Playgroud)

我回来的错误很难理解。

验证失败:12:22:41.316
对象:{}
消息:“无效的类型:数字(预期的字符串)”
名称:“ ValidationError”
类型:“ Error”

但是我需要知道验证失败的领域。我如何获得此信息?tv4npm页面建议该错误消息应更具描述性。

api json node.js postman tv4

7
推荐指数
1
解决办法
2088
查看次数

oneOf 的 JSON 架构自定义消息

有没有办法为 json 模式 (tv4) 设置自定义消息,以便在字段失败时使用oneOf

我看到大约一年前在这里这里出现了一个针对自定义消息的问题,但是有没有办法让这个问题适用于这样的事情?

{
    "id": "code",
    "description": "Schema for request.body - pin for logging into the bank",
    "oneOf": [
        {
            "type": "string",
            "pattern": "^.*\\S.*$"
        },
        {
            "type": "object",
            "properties": {
                "type": {
                    "type": "string",
                    "pattern": "^(encrypted|not_encrypted)$"
                },
                "value": {
                    "type": "string",
                    "pattern": "^.*\\S.*$"
                }
            }
        }
    ],
    "messages": {
        "oneOf": "Code does not match schema"
    }
}
Run Code Online (Sandbox Code Playgroud)

与仅仅看到不同Data does not match any schemas from \"oneOf\",你可以看到Code does not match …

jsonschema tv4

5
推荐指数
1
解决办法
5445
查看次数

如何使用tv4在邮递员中测试JSON模式?

这就是我正在尝试的方法,但是即使结果不好也总是可以通过测试。

pm.test("Schema is valid", function () {
    var data = pm.response.json();
    var schema = {
        ...
        my schema
        ...
    };
    tv4.validate(data, schema);
});
Run Code Online (Sandbox Code Playgroud)

postman tv4

4
推荐指数
1
解决办法
6168
查看次数

POSTMAN返回模式验证测试失败

我有一个示例回复:

{
  "tags": [
    {
      "id": 1,
      "name": "[String]",
      "user_id": 1,
      "created_at": "2016-12-20T15:50:37.000Z",
      "updated_at": "2016-12-20T15:50:37.000Z",
      "deleted_at": null
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我已经为响应写了一个测试:

var schema = {
    "type": "object",
    "properties": {
        "tags": {
            "type": "object",
            "properties": {
                "id": { "type": "integer" },
                "name": { "type": "string" },
                "user_id": { "type": "number" },
                "created_at": { "type": "string" },
                "updated_at": { "type": "string" },
                "deleted_at": { "type": ["string", "null"] }
            }
        }
    }
};
var data = JSON.parse(responseBody);

tests["Valid schema"] = tv4.validate(data, schema);
Run Code Online (Sandbox Code Playgroud)

此测试返回[FAIL].测试中有什么错误? …

schema json postman tv4

2
推荐指数
1
解决办法
4280
查看次数

标签 统计

tv4 ×4

postman ×3

json ×2

api ×1

jsonschema ×1

node.js ×1

schema ×1