# +ive, -ive test for Response body containing element & array

**URL:** <https://community.postman.com/t/ive-ive-test-for-response-body-containing-element-array/58907>\
**Category:** Help Hub\
**Tags:** tests, responseassertion\
**Created:** [March 16, 2024, 6:18pm UTC](https://community.postman.com/t/ive-ive-test-for-response-body-containing-element-array/58907 "2024-03-16T18:18:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shailendrakumaromkar](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@shailendrakumaromkar](https://community.postman.com/u/shailendrakumaromkar)\
**Post date:** [March 16, 2024, 6:18pm UTC](https://community.postman.com/t/ive-ive-test-for-response-body-containing-element-array/58907/1 "2024-03-16T18:18:52Z")

</div>

My response body is below, how can I write below tests -

1. Response body contains elements (‘oldestBlock’,‘reward’, ‘baseFeePerGas’,‘gasUsedRatio’), in a single test  
I tried below but it should fail instead of pass because “gasUsedRatios” contains extra ‘s’ at end, correct value = gasUsedRatio.

pm.expect(pm.response.text()).to.includes(“oldestBlock”,“reward”, “baseFeePerGas”,“gasUsedRatios”)

1. Response body does not contains elements except above ones.

2. reward is an array & shouldn’t be empty

3. value in reward array should begin with “0x”

4. Below Json schema validation

```auto
{
    "jsonrpc": "2.0",
    "id": "1",
    "result": {
        "oldestBlock": "0x128c528",
        "reward": [
            [
                "0xc2f6ed",
                "0xc2f6ed"
            ],
            [
                "0xbd4bf3",
                "0xc2f6ed"
            ],
            [
                "0xbd1d99",
                "0xbd4bf3"
            ],
            [
                "0xbd1d99",
                "0xbd1d99"
            ],
            [
                "0xf4240",
                "0xb79e27"
            ]
        ],
        "baseFeePerGas": [
            "0xb3a42c70d",
            "0xb367ecc49",
            "0xaaeb73d77",
            "0xa3480338a",
            "0xa6f4fc367",
            "0xa98e65669"
        ],
        "gasUsedRatio": [
            0.49475963333333334,
            0.3107929,
            0.3212392,
            0.5900452,
            0.5622738
        ]
    }
}

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [March 17, 2024, 2:36pm UTC](https://community.postman.com/t/ive-ive-test-for-response-body-containing-element-array/58907/2 "2024-03-17T14:36:00Z")

</div>

You can use the Ajv JSON Schema validator.

[Validate Response Structure - Postman Learning Center](https://learning.postman.com/docs/writing-scripts/script-references/test-examples/#validate-response-structure)

You’ll have to search the web for guides as the Postman Learning Center example is fairly basic.

Required field will cover test 1, you can set additionalProperties to false to cover test 2. You can also use regex patterns to validate test 4.

Also have a look at the following.

> <https://stackoverflow.com/questions/16491973/using-regex-in-json-schema>

---

<div class="post-metadata">

**Author:** ![shailendrakumaromkar](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@shailendrakumaromkar](https://community.postman.com/u/shailendrakumaromkar)\
**Post date:** [March 17, 2024, 8:26pm UTC](https://community.postman.com/t/ive-ive-test-for-response-body-containing-element-array/58907/4 "2024-03-17T20:26:14Z")

</div>

Thank You, will check out.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [April 16, 2024, 8:26pm UTC](https://community.postman.com/t/ive-ive-test-for-response-body-containing-element-array/58907/5 "2024-04-16T20:26:24Z")

</div>

Closed on April 16, 2024, 8:26pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
