0
0
Postmantesting~8 mins

Schema validation basics in Postman - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Schema validation basics
Folder Structure
postman-project/
├── collections/
│   └── api-collection.json       # Postman collection with requests and tests
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── schemas/
│   └── response-schema.json      # JSON Schema files for response validation
├── tests/
│   └── schema-validation-tests.js # Optional external test scripts if using Newman + JS
├── reports/
│   └── test-report.html          # Generated test reports
├── newman-config.json            # Newman run configuration
└── README.md
  
Test Framework Layers
  • Collections: Contains API requests with embedded test scripts for schema validation.
  • Environments: Holds environment-specific variables like base URLs and credentials.
  • Schemas: JSON Schema files defining expected response structure for validation.
  • Tests: Optional JavaScript files for advanced schema validation using Newman or external runners.
  • Reports: Stores test execution reports generated by Newman or other tools.
  • Config: Newman configuration files to define run parameters like environment, reporters, iteration count.
Configuration Patterns
  • Environment Files: Use separate Postman environment JSON files for dev, staging, and prod to manage variables.
  • Schema Files: Store JSON Schema files separately and reference them in test scripts for maintainability.
  • Newman Config: Use a JSON config file to specify environment, collection, reporters, and iteration count for CLI runs.
  • Credentials: Store sensitive data in environment variables, never hard-coded in collections or scripts.
Test Reporting and CI/CD Integration
  • Use Newman CLI to run Postman collections and generate reports in formats like HTML, JSON, or JUnit XML.
  • Integrate Newman runs into CI/CD pipelines (Jenkins, GitHub Actions, GitLab CI) to automate schema validation on each build.
  • Publish reports as build artifacts or send notifications on failures to keep the team informed.
  • Use tags or folder structures in collections to organize schema validation tests for selective execution.
Best Practices
  1. Separate Schema Files: Keep JSON Schemas outside collections for easy updates and reuse.
  2. Use Environment Variables: Manage URLs and credentials securely and flexibly.
  3. Embed Schema Validation in Tests: Use Postman test scripts with pm.expect and tv4 or ajv libraries for clear validation.
  4. Automate with Newman: Run tests automatically in CI/CD to catch schema issues early.
  5. Clear Reporting: Generate readable reports to quickly identify schema validation failures.
Self Check

Where in this folder structure would you add a new JSON Schema file for validating the response of a new API endpoint?

Key Result
Organize Postman schema validation by separating collections, environments, schemas, and automate tests with Newman for reliable API contract checks.