Проверка схемы JSON не выполняется в Postman, даже если она действительна

Мне нужно проверить следующий ответ на схему JSON. Проблема в том, что он всегда дает сбой, даже если схема действительна.

{
    "items": [
        {
            "uuid": "f68ad4ba-a11e-485d-a2d7-17b9b07bd8d3",
            "name": "Code",
            "type": "app_code",
            "description": "Code 1",
            "content_type": "application/javascript",
            "audit": {
                "created_date": "2017-11-02T00:16:58.000Z",
                "created_by": "e7c97dc1-08eb-45ef-b883-d100553bac5c"
            }
        },
        {
            "uuid": "3c9e59b0-f6c7-4788-8a14-0b3e99fc4306",
            "name": "Object demo 2",
            "type": "app_code",
            "description": "Object demo 2 description",
            "content_type": "application/javascript",
            "audit": {
                "created_date": "2017-11-02T13:48:22.000Z",
                "created_by": "e7c97dc1-08eb-45ef-b883-d100553bac5c"
            }
        },
        {
            "uuid": "e2f54e6c-a158-4f43-a332-1c99bb76684e",
            "name": "toolbox_test_results",
            "type": "toolbox_tests",
            "description": "This is snapshots for React-OSS Toolbox",
            "content_type": "application/json",
            "audit": {
                "created_date": "2017-11-07T11:29:02.000Z",
                "created_by": "e7c97dc1-08eb-45ef-b883-d100553bac5c"
            }
        }
    ],
    "metadata": {
        "total": 124,
        "count": 3
    },
    "links": [
        {
            "rel": "self",
            "href": "http://microsvcs.star2star.net:10010/users/e7c97dc1-08eb-45ef-b883-d100553bac5c/objects?load_content=false&limit=3&offset=0"
        },
        {
            "rel": "first",
            "href": "http://microsvcs.star2star.net:10010/users/e7c97dc1-08eb-45ef-b883-d100553bac5c/objects?load_content=false&limit=3&offset=0"
        },
        {
            "rel": "next",
            "href": "http://microsvcs.star2star.net:10010/users/e7c97dc1-08eb-45ef-b883-d100553bac5c/objects?load_content=false&limit=3&offset=3"
        },
        {
            "rel": "last",
            "href": "http://microsvcs.star2star.net:10010/users/e7c97dc1-08eb-45ef-b883-d100553bac5c/objects?load_content=false&limit=3&offset=123"
        }
    ]
}

Я использую следующий код для проверки в Postman:

// Определяем схему JSON

const objectSchema = {
  "items": [
    {
      "audit": {
        "created_by": "string",
        "created_date": "string",
        "updated_by": "string",
        "updated_date": "string"
      },
      "content": {},
      "content_type": "string",
      "description": "string",
      "name": "string",
      "type": "string",
      "uuid": "string"
    }
  ],
  "links": [
    {
      "href": "string",
      "rel": "string",
      "templated": true
    }
  ],
  "metadata": {}
};


pm.test("JSON schema validation", function() {
  var responseData = JSON.parse(responseBody);
  var result = tv4.validate(responseData, objectSchema, false, true);
  if (result !== true) {
      console.log('Schema validation failed:', tv4.error);
  }
  pm.expect(result).to.be.true;
  console.log(JSON.stringify(result));
});

Как можно получить подробную ошибку в консоли (например: какое поле имеет неправильный тип или отсутствует)? Фактическая ошибка с консоли:

message:"Unknown property (not in schema)"
name:"ValidationError"
type:"Error

«Заранее благодарю за ответы!


person Julia    schedule 12.02.2018    source источник
comment
Привет, Павел! где вы пробовали запустить вышеуказанный скрипт? В почтальоне? Спасибо   -  person Julia    schedule 13.02.2018


Ответы (1)


Работа над комментарием @Pavlo:

В вашем тесте:

pm.test("JSON schema validation", function() {
  var responseData = JSON.parse(responseBody);
  var result = tv4.validate(responseData, objectSchema, false, true);
  if (result !== true) {
      console.log('Schema validation failed:', tv4.error);
  }
  pm.expect(result).to.be.true;
  console.log(JSON.stringify(result));
});

строку var responseData = JSON.parse(responseBody); следует заменить на:

var responseData = pm.response.json();

Кроме того, из этого ответа вы можете добавить следующая строка, чтобы получить дополнительную информацию о том, где произошел сбой схемы:

console.log(tv4.error.dataPath);
person jasonscript    schedule 04.04.2018