【问题标题】:POSTMAN returns fail for schema validation testPOSTMAN 为模式验证测试返回失败
【发布时间】:2017-05-06 02:59:51
【问题描述】:

我有一个示例响应:

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

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

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]。测试中有哪些错误?

感谢您的回复!

【问题讨论】:

    标签: json schema postman tv4


    【解决方案1】:

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

    这段代码通过了测试:

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

    【讨论】:

    • 谢谢!你的回答对我帮助很大!!它比官方文档更容易理解。
    猜你喜欢
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 2018-10-03
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2021-03-16
    相关资源
    最近更新 更多