POSTMAN returns fail for schema validation test

只愿长相守 提交于 2019-12-24 01:17:04

问题


I have a sample response:

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

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

Thank you for a respond!


回答1:


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.

This code is passing the test:

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


来源:https://stackoverflow.com/questions/41250036/postman-returns-fail-for-schema-validation-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!