Test Overview
This test checks if the request body contains all required fields and valid data before sending the API request. It verifies that the body is correctly structured and prevents sending invalid data.
This test checks if the request body contains all required fields and valid data before sending the API request. It verifies that the body is correctly structured and prevents sending invalid data.
pm.test("Request body has required fields and valid data", () => { const body = pm.request.body.raw ? JSON.parse(pm.request.body.raw) : {}; pm.expect(body).to.have.property('username').that.is.a('string').and.is.not.empty; pm.expect(body).to.have.property('email').that.is.a('string').and.match(/^\S+@\S+\.\S+$/); pm.expect(body).to.have.property('age').that.is.a('number').and.is.above(0); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test script starts and parses the request body JSON | Postman request body is available as raw JSON string | Check if body is parsed without error | PASS |
| 2 | Check if 'username' field exists, is a string, and not empty | Parsed body object with fields | 'username' property exists and is a non-empty string | PASS |
| 3 | Check if 'email' field exists, is a string, and matches email pattern | Parsed body object with fields | 'email' property exists and matches email regex | PASS |
| 4 | Check if 'age' field exists, is a number, and greater than 0 | Parsed body object with fields | 'age' property exists and is a positive number | PASS |
| 5 | All validations passed, request body is valid | Request ready to be sent with valid body | All previous assertions passed | PASS |