0
0
Postmantesting~8 mins

Body validation before sending in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Body validation before sending
Folder Structure
PostmanProject/
├── collections/
│   └── api-requests.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── tests/
│   └── bodyValidationTests.js
├── scripts/
│   └── validators.js
├── globals.json
└── README.md

This structure organizes Postman collections, environment files, test scripts for body validation, and reusable validation scripts.

Test Framework Layers
  • Collections: Store API requests with pre-request scripts and test scripts.
  • Environments: Manage variables like base URLs, tokens, and credentials for different setups.
  • Test Scripts: JavaScript files under tests/ that contain body validation logic using Postman test API.
  • Reusable Validators: Helper functions in scripts/validators.js to check JSON schema, required fields, or value formats before sending requests.
  • Globals: Global variables accessible across collections and environments.
Configuration Patterns
  • Environment Variables: Use environment files to switch base URLs, authentication tokens, and other settings without changing requests.
  • Pre-request Scripts: Validate and modify request body before sending. For example, check required fields or data types and abort sending if invalid.
  • Global Variables: Store common values like API keys or user IDs accessible across collections.
  • Data Files: Use CSV or JSON files for data-driven testing to validate multiple body payloads.
Test Reporting and CI/CD Integration
  • Postman Test Results: View pass/fail status of body validation tests in Postman Test Results tab after running collections.
  • Newman CLI: Run collections with body validation tests in command line and generate JSON, HTML, or JUnit reports.
  • CI/CD Integration: Integrate Newman runs in pipelines (GitHub Actions, Jenkins, GitLab CI) to automatically validate request bodies before deployment.
  • Alerts: Configure pipeline to fail and notify team if body validation tests fail.
Best Practices
  1. Validate Early: Use pre-request scripts to check request body before sending to avoid unnecessary API calls.
  2. Use JSON Schema: Define and validate request body structure with JSON schema for clear expectations.
  3. Reusable Validators: Create helper functions for common validations to keep scripts clean and maintainable.
  4. Environment Separation: Keep environment-specific data separate to avoid accidental data leaks or wrong endpoints.
  5. Automate Reporting: Use Newman and CI/CD to automate validation and get quick feedback on body correctness.
Self Check

Where in this folder structure would you add a new validation function to check if a request body contains all required fields before sending?

Key Result
Use pre-request scripts and reusable validators in Postman collections to validate request bodies before sending.