Всем привет
Пытаюсь сравнить структуру ответа в postman с документацией (нужно проверить тип полей)
В ответ приходит сообщение, которое может иметь 0 или несколько файлов и квитанций структура у всех прикрепленных файлов или квитанций одинаковая, но их количество постоянно меняется, т.к. сообщение приходит рандомное.
Пример Response:
"Message": null, "Files": [] }, { "Id": "string", "ReceiveTime": "string", "StatusTime": "string", "Status": "error", "Message": "string", "Files": [ { "Id": "string", "Name": "string", "Description": null, "Size": "integer", "RepositoryInfo": [ { "Path": "string", "Host": "string", "Port": "integer", "RepositoryType": "string" } ] }, { "Id": "string", "Name": "string", "Description": null, "Size": "integer", "RepositoryInfo": [ { "Path": "string", "Host": "string", "Port": "integer", "RepositoryType": "string" } ] }, { "Id": "string", "Name": "string", "Description": null, "Size": "integer", "RepositoryInfo": [ { "Path": "string", "Host": "string", "Port": "integer", "RepositoryType": "string" } ] }, { "Id": "string", "Name": "string", "Description": null, "Size": "integer", "RepositoryInfo": [ { "Path": "string", "Host": "string", "Port": "integer", "RepositoryType": "string" } ] } ] } ] }
Через это https://www.jsonschema.net/ я преобразую , во что, сам не знаю, но так работает ))))), и после кидаю схему в postman test:
var schema = { "definitions": {}, "$schema": "http://json-schema.org/draft-07/schema#", "$id": "http://example.com/root.json", "type": "object", "title": "The Root Schema", "required": [ "Id", "CorrelationId", "GroupId", "Type", "Title", "Text", "CreationDate", "UpdatedDate", "Status", "TaskName", "RegNumber", "TotalSize", "Sender", "Files", "Receipts" ], "properties": { "Id": { "$id": "#/properties/Id", "type": "string", "title": "The Id Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "CorrelationId": { "$id": "#/properties/CorrelationId", "type": "null", "title": "The Correlationid Schema", "default": null, "examples": [ null ] }, "GroupId": { "$id": "#/properties/GroupId", "type": "null", "title": "The Groupid Schema", "default": null, "examples": [ null ] }, "Type": { "$id": "#/properties/Type", "type": "string", "title": "The Type Schema", "default": "", "examples": [ "outbox" ], "pattern": "^(.*)$" }, "Title": { "$id": "#/properties/Title", "type": "string", "title": "The Title Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "Text": { "$id": "#/properties/Text", "type": "string", "title": "The Text Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "CreationDate": { "$id": "#/properties/CreationDate", "type": "string", "title": "The Creationdate Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "UpdatedDate": { "$id": "#/properties/UpdatedDate", "type": "string", "title": "The Updateddate Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "Status": { "$id": "#/properties/Status", "type": "string", "title": "The Status Schema", "default": "", "examples": [ "error" ], "pattern": "^(.*)$" }, "TaskName": { "$id": "#/properties/TaskName", "type": "string", "title": "The Taskname Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "RegNumber": { "$id": "#/properties/RegNumber", "type": "null", "title": "The Regnumber Schema", "default": null, "examples": [ null ] }, "TotalSize": { "$id": "#/properties/TotalSize", "type": "string", "title": "The Totalsize Schema", "default": "", "examples": [ "integer" ], "pattern": "^(.*)$" }, "Sender": { "$id": "#/properties/Sender", "type": "object", "title": "The Sender Schema", "required": [ "Inn", "Ogrn", "Bik", "RegNum", "DivisionCode" ], "properties": { "Inn": { "$id": "#/properties/Sender/properties/Inn", "type": "string", "title": "The Inn Schema", "default": "", "examples": [ "integer" ], "pattern": "^(.*)$" }, "Ogrn": { "$id": "#/properties/Sender/properties/Ogrn", "type": "null", "title": "The Ogrn Schema", "default": null, "examples": [ null ] }, "Bik": { "$id": "#/properties/Sender/properties/Bik", "type": "null", "title": "The Bik Schema", "default": null, "examples": [ null ] }, "RegNum": { "$id": "#/properties/Sender/properties/RegNum", "type": "null", "title": "The Regnum Schema", "default": null, "examples": [ null ] }, "DivisionCode": { "$id": "#/properties/Sender/properties/DivisionCode", "type": "null", "title": "The Divisioncode Schema", "default": null, "examples": [ null ] } } }, "Files": { "$id": "#/properties/Files", "type": "array", "title": "The Files Schema", "items": { "$id": "#/properties/Files/items", "type": "object", "title": "The Items Schema", "required": [ "Id", "Name", "Description", "Encrypted", "SignedFile", "Size", "RepositoryInfo" ], "properties": { "Id": { "$id": "#/properties/Files/items/properties/Id", "type": "string", "title": "The Id Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "Name": { "$id": "#/properties/Files/items/properties/Name", "type": "string", "title": "The Name Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "Description": { "$id": "#/properties/Files/items/properties/Description", "type": "null", "title": "The Description Schema", "default": null, "examples": [ null ] }, "Encrypted": { "$id": "#/properties/Files/items/properties/Encrypted", "type": "boolean", "title": "The Encrypted Schema", "default": false, "examples": [ true ] }, "SignedFile": { "$id": "#/properties/Files/items/properties/SignedFile", "type": "null", "title": "The Signedfile Schema", "default": null, "examples": [ null ] }, "Size": { "$id": "#/properties/Files/items/properties/Size", "type": "string", "title": "The Size Schema", "default": "", "examples": [ "integer" ], "pattern": "^(.*)$" }, "RepositoryInfo": { "$id": "#/properties/Files/items/properties/RepositoryInfo", "type": "array", "title": "The Repositoryinfo Schema", "items": { "$id": "#/properties/Files/items/properties/RepositoryInfo/items", "type": "object", "title": "The Items Schema", "required": [ "Path", "Host", "Port", "RepositoryType" ], "properties": { "Path": { "$id": "#/properties/Files/items/properties/RepositoryInfo/items/properties/Path", "type": "string", "title": "The Path Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "Host": { "$id": "#/properties/Files/items/properties/RepositoryInfo/items/properties/Host", "type": "string", "title": "The Host Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "Port": { "$id": "#/properties/Files/items/properties/RepositoryInfo/items/properties/Port", "type": "string", "title": "The Port Schema", "default": "", "examples": [ "integer" ], "pattern": "^(.*)$" }, "RepositoryType": { "$id": "#/properties/Files/items/properties/RepositoryInfo/items/properties/RepositoryType", "type": "string", "title": "The Repositorytype Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" } } } } } } }, "Receipts": { "$id": "#/properties/Receipts", "type": "array", "title": "The Receipts Schema", "items": { "$id": "#/properties/Receipts/items", "type": "object", "title": "The Items Schema", "required": [ "Id", "ReceiveTime", "StatusTime", "Status", "Message", "Files" ], "properties": { "Id": { "$id": "#/properties/Receipts/items/properties/Id", "type": "string", "title": "The Id Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "ReceiveTime": { "$id": "#/properties/Receipts/items/properties/ReceiveTime", "type": "string", "title": "The Receivetime Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "StatusTime": { "$id": "#/properties/Receipts/items/properties/StatusTime", "type": "string", "title": "The Statustime Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "Status": { "$id": "#/properties/Receipts/items/properties/Status", "type": "string", "title": "The Status Schema", "default": "", "examples": [ "string" ], "pattern": "^(.*)$" }, "Message": { "$id": "#/properties/Receipts/items/properties/Message", "type": "null", "title": "The Message Schema", "default": null, "examples": [ null ] }, "Files": { "$id": "#/properties/Receipts/items/properties/Files", "type": "array", "title": "The Files Schema" } } } } } } pm.test('Shema Valid', () => { var carrier = JSON.parse(responseBody); pm.expect(tv4.validate(carrier, schema)).to.be.true })
В схеме большинство полей удаляю, оставляю только поле type, пример:
"Message": { "type": "null", }
Теперь вопросы:
1) Есть ли способ проще в написании чем выбрал я c написанием этой схемы(https://www.jsonschema.net/)?
2) Как сделать проверку файлов, квитанций, если их будет 10-20шт, не дублируя их? через цикл? а эта схема(https://www.jsonschema.net/), на сколько я понимаю не съест цикл javascript-a
2) Правильную ли я выбрал форму проверки в postman-e?
3) Если поля обязательные или необязательные, как это прописать в тесте(pm.expect(tv4.validate(carrier, shema)).to.be.true)?
4) Если сообщении присутствует файл и отсутствует квитанция, как это протестировать?) (Пометить массив квитанций как необязательный?)
Уф, надеюсь более менее понятно написал )))
Заранее спасибо )