Test Overview
This test sends a request to an API endpoint and checks if the response JSON matches the expected schema. It verifies that the response has the correct structure and data types.
Jump into concepts and practice - no test required
This test sends a request to an API endpoint and checks if the response JSON matches the expected schema. It verifies that the response has the correct structure and data types.
pm.test("Response matches schema", () => { const schema = { type: "object", properties: { id: { type: "integer" }, name: { type: "string" }, email: { type: "string", format: "email" }, isActive: { type: "boolean" } }, required: ["id", "name", "email", "isActive"] }; pm.response.to.have.jsonSchema(schema); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman is ready to send the API request | — | PASS |
| 2 | Send GET request to API endpoint | API server receives request and processes it | — | PASS |
| 3 | Receive JSON response from API | Response body contains JSON with fields id, name, email, isActive | — | PASS |
| 4 | Run schema validation using pm.response.to.have.jsonSchema(schema) | Postman validates response JSON against defined schema | Check that response JSON matches schema types and required fields | PASS |
| 5 | Test completes with assertion result | Test report shows pass if schema matches | Schema validation passed | PASS |
schema variable?pm.response.to.have.jsonSchema(schema); to validate JSON schema.{
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"}
},
"required": ["id", "name"]
}{"id": 10, "name": "Alice"}?const schema = {
type: "object",
properties: {
age: { type: "integer" }
},
required: ["age"]
};
pm.test("Schema is valid");
pm.response.to.have.jsonSchema(schema);age. What is the likely error?pm.test("name", () => { assertion }); but the code calls pm.test("Schema is valid"); without the required callback function.pm.response.to.have.jsonSchema(schema); is outside the pm.test callback and will not be properly associated with the test.pm.test callback, but the code uses pm.test incorrectly -> Option Aid (integer) and optional email (string). Which JSON schema correctly validates this response array?