POSTMAN 为模式验证测试返回失败 [英] POSTMAN returns fail for schema validation test

查看:40
本文介绍了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
    }
  ]
}

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

I've written a test for the response:

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

此测试返回 [FAIL].测试中有哪些错误?

This test returns [FAIL]. What wrongs in the test?

感谢您的回复!

推荐答案

tags的定义有问题,因为它是一个数组而不是一个对象.您应该将它的属性嵌套到它的 items 属性中.

There is a problem on the definition of tags, since it's an array instead of an object. You should nest its properties into its items properties.

这段代码通过了测试:

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

test_schema = {
    "type": "object",
    "properties": {
        "tags": {
            "type": "array",
            "items": {
                "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"] }
                }
            }
        }
    }
};
tests["Testing schema"] = tv4.validate(test_data, test_schema);
console.log("Validation errors: ", tv4.error);

这篇关于POSTMAN 为模式验证测试返回失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆