0
0
Postmantesting~10 mins

JSON Schema validation in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if the API response matches the expected JSON schema. It verifies the structure and data types of the response fields.

Test Code - Postman
Postman
pm.test('Response matches JSON 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'],
    additionalProperties: false
  };
  pm.response.to.have.jsonSchema(schema);
});
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test startsPostman test runner is ready to execute the test script-PASS
2Send API request and receive responseResponse received with JSON body containing id, name, email, and isActive fields-PASS
3Validate response JSON against the defined schema using pm.response.to.have.jsonSchemaResponse JSON is checked for correct types and required fieldsCheck that id is integer, name is string, email is valid email string, isActive is boolean, and no extra fields existPASS
4Test completes with assertion resultTest runner shows test passed if schema validation succeededSchema validation passedPASS
Failure Scenario
Failing Condition: Response JSON does not match the schema, e.g., missing required field or wrong data type
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify in the API response?
AThe response matches the expected JSON schema
BThe response time is less than 2 seconds
CThe response status code is 404
DThe response contains a specific header
Key Result
Validating API responses against a JSON schema ensures the data structure and types are correct, preventing errors in later processing.