mardi 23 février 2021

Postman Test. Looping through an array of nested objects to find an empty value

I would like to ask you for a favour/solution. I got a test when I have to make sure that each contact contains array 'phone_numbers' and 'email' and got some value (not empty array or value). I know there is syntax for test such as:

pm.test("Check for empty array's values", () => {
pm.expect(jsonData.contacts[1].phone_numbers).to.not.eql([]);
});

The problem is that I don’t want to check one by one because sometimes is very large. I’m pretty sure there an easy solution for that. I noticed that some contacts contain empty email, phone number or both. e.g. "id": "id": 33333564 which I cannot allow in the future.

Thanks in advance :-)

{
  "contacts": [
    {
      "id": 11121211,
      "direct_link": "https://example.example",
      "first_name": "test1",
      "last_name": "test",
      "company_name": "test",
      "information": null,
      "is_shared": true,
      "created_at": 1582798926,
      "updated_at": 1582798926,
      "emails": [],
      "phone_numbers": [
        {
          "id": 60065270,
          "label": "Work",
          "value": "+33134567666"
        }
      ]
    },
    {
      "id": 22222222,
      "direct_link": "https://example.example",
      "first_name": null,
      "last_name": null,
      "company_name": null,
      "information": null,
      "is_shared": true,
      "created_at": 1583686067,
      "updated_at": 1583686067,
      "emails": [],
      "phone_numbers": [
        {
          "id": 22266444,
          "label": "Work",
          "value": "+33134567899"
        }
      ]
    },
    {
      "id": 33333564,
      "direct_link": "https://example.example",
      "first_name": "Jessica",
      "last_name": "Biel",
      "company_name": "N-Sync",
      "information": null,
      "is_shared": true,
      "created_at": 1583686086,
      "updated_at": 1583686086,
      "emails": [],
      "phone_numbers": []
    },
    {
      "id": 45678901,
      "direct_link": "https://example.example",
      "first_name": null,
      "last_name": null,
      "company_name": null,
      "information": null,
      "is_shared": true,
      "created_at": 1583686105,
      "updated_at": 1583686105,
      "emails": [],
      "phone_numbers": [
        {
          "id": 22266444,
          "label": "Work",
          "value": "+33134567333"
        }
      ]
    },
    {
      "id": 56789123,
      "direct_link": "https://example.example",
      "first_name": "Jacky",
      "last_name": "Rowland",
      "company_name": "Test Company1",
      "information": "",
      "is_shared": true,
      "created_at": 1583745888,
      "updated_at": 1608556499,
      "emails": [
        {
          "id": 76594398,
          "label": "Work",
          "value": "mandatory_field@example.com"
        }
      ],
      "phone_numbers": [
        {
          "id": 60650277,
          "label": "Mobile",
          "value": "+33652556777"
        }
      ]
    }
  ],
  "meta": {
    "count": 6,
    "total": 241,
    "current_page": 1,
    "per_page": 5,
    "next_page_link": "https://example.example",
    "previous_page_link": null
  }
}

Aucun commentaire:

Enregistrer un commentaire