npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

postman-openapi-schema-validator

v1.1.1

Published

A Node.js utility for validating and integrating OpenAPI schema validations into Postman collections. Supports OpenAPI versions 2.0, 3.0.x. Works as a CLI tool and a library.

Downloads

315

Readme

npm
Integration Tests

OpenAPI to Postman Schema Validator

This project provides a Node.js utility for automating the validation of Postman request responses against OpenAPI (Swagger) schemas. It works both as a command-line tool and a library, making it flexible for various use cases. The utility reads OpenAPI specifications (in JSON or YAML format) and Postman collections (in JSON format), then inserts schema validation scripts into the Postman collection based on OpenAPI definitions. Ensure that your OpenAPI specifications contain schemas, as empty schemas won't be added.


Features

  • Supports OpenAPI versions: 2.0, 3.0.0, 3.0.1, and higher.
  • Automates schema validation: Adds schema validation scripts to Postman requests.
  • Debugging: Logs validation results in the console.
  • Improved Postman collections: Generates new Postman collections with added schema validation for 20X status codes.
  • Uses AJV: Validates API responses against OpenAPI schemas.
  • Preserves existing tests: Does not overwrite existing Postman scripts; new scripts are appended.
  • Status code checks: Automatically adds status code validation based on OpenAPI specifications.
  • Flexible usage: Can be used via CLI or imported as a library.

Requirements

  • Node.js: Version 14.x or later.
  • NPM: Version 6.x or later.

Benefits for QA

  1. Accurate schema validation: Ensures API contracts are always up to date.
  2. Saves time: Eliminates the need for manual schema validation creation in Postman.
  3. Seamless CI/CD integration: Can be used before running tests to focus on business logic and data state validations.
  4. Supports multiple formats: Works with OpenAPI (Swagger), JSON, and YAML.

Installation

Global Installation (for CLI usage)

Install the utility globally with npm:

npm install -g postman-openapi-schema-validator

This makes the posv command available globally in the terminal.

Local Installation (for library usage)

Install the utility locally in your project:

npm install postman-openapi-schema-validator

Import it into your code:

const { validateCollection } = require('postman-openapi-schema-validator');

Usage

CLI

To validate a Postman collection against an OpenAPI specification, use the following command:

posv --collection path/to/postman_collection.json --spec path/to/openapi_spec.yaml

To include additional status code validation:

posv --collection path/to/postman_collection.json --spec path/to/openapi_spec.yaml --status-code-check

Library

Use the utility in your code for greater flexibility:

const { validateCollection } = require('postman-openapi-schema-validator');

const collectionPath = './path/to/postman_collection.json';
const specPath = './path/to/openapi_spec.yaml';
const options = { statusCodeCheck: true };

validateCollection(collectionPath, specPath, options)
    .then((outputPath) => {
        console.log(`Validation completed. Output saved to: ${outputPath}`);
    })
    .catch((error) => {
        console.error(`Validation failed: ${error.message}`);
    });

Console Report

Example Output

The utility logs detailed results to the console. Here's an example output showing validation summaries:

OpenAPI version detected: 3.0.1
✓ Schema validation test added for POST /pet
✗ Schema not found for GET /unknown
✓ Schema validation test added for DELETE /user/{id}

--- Validation Summary ---
┌─────────────────────┬────────┐
│ Metric              │ Value  │
├─────────────────────┼────────┤
│ Total Requests      │ 10     │
│ Validated Requests  │ 8      │
│ Unvalidated Requests│ 2      │
│ Schema Coverage (%) │ 80.00% │
└─────────────────────┴────────┘

Example Postman Script with AJV Validation

ajv


Arguments

CLI Arguments:

  • --collection <path>: Path to the Postman collection JSON file.
  • --spec <path>: Path to the OpenAPI specification file (JSON or YAML).
  • --status-code-check: Adds status code validation to Postman tests.

Library Options:

  • collectionPath: Path to the Postman collection JSON file.
  • specPath: Path to the OpenAPI specification file (JSON or YAML).
  • options.statusCodeCheck: Enables status code validation.

Logging

  • Green Checkmark (✓): Schema found and successfully added to the Postman collection.
  • Red Cross (✗): Schema not found or an error occurred. Details will be logged in the console.

Links


What's New in Version 1.1.1

  • Added a percentage-based schema coverage report.
  • Enhanced CLI error handling and user feedback.
  • Improved library usage example with detailed options.
  • Bug fixes and performance optimizations.