0
0
Postmantesting~15 mins

Schema validation basics in Postman - Build an Automation Script

Choose your learning style9 modes available
Validate JSON response schema for user data API
Preconditions (2)
Step 1: Send a GET request to https://api.example.com/users/1
Step 2: Capture the JSON response
Step 3: Validate the response against the predefined JSON schema
✅ Expected Result: The response JSON matches the schema with correct data types and required fields
Automation Requirements - Postman test scripts
Assertions Needed:
Response status code is 200
Response body matches the JSON schema
Required fields 'id', 'name', 'email' are present and of correct type
Best Practices:
Use pm.response.to.have.status for status code assertion
Use pm.expect with tv4 or ajv for JSON schema validation
Define schema as a separate variable for readability
Write clear and descriptive assertion messages
Automated Solution
Postman
const schema = {
  type: 'object',
  required: ['id', 'name', 'email'],
  properties: {
    id: { type: 'integer' },
    name: { type: 'string' },
    email: { type: 'string', format: 'email' }
  }
};

pm.test('Status code is 200', () => {
  pm.response.to.have.status(200);
});

pm.test('Response matches user schema', () => {
  pm.response.to.have.jsonSchema(schema);
});

The schema variable defines the expected JSON structure with required fields and their types.

The first test checks that the response status code is 200, ensuring the request was successful.

The second test uses Postman's built-in to.have.jsonSchema method to validate the response body against the schema.

This approach keeps tests clear and maintainable by separating schema definition and assertions.

Common Mistakes - 3 Pitfalls
Not checking the status code before validating the schema
Defining the schema inside the test block repeatedly
Using incorrect data types in the schema (e.g., string instead of integer)
Bonus Challenge

Now add data-driven testing with 3 different user IDs to validate their responses against the schema

Show Hint