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

openapi3-middleware

v1.1.0

Published

validate requests against your openapi3 definition

Downloads

456

Readme

openapi3-middleware

validate requests against your openapi3 definition

npm

npm install openapi3-middleware

Usage

validator checks incoming request as identified by operationId, therefore operationId fields in api definition are required for explicitly matching the right implementation. It's another layer of certainty for your implementation.

const openapi3middleware = require('openapi3-middleware')

const validatorInstance = openapi3middleware.validator(apiDefinition)

// Use directly
validatorInstance.validateRequest(req, operationId) 
  // throws a validation error if anything goes wrong

// Express middleware with default responses
app.get(validatorInstance.expressMiddlewareAutorespond(operationId), handlerFunction)
app.get(validatorInstance.expressMiddlewareAutorespondJson(operationId), handlerFunction)
  // returns a response with a status code matching what happened and a descriptive message to help fix the error

// Express middleware
app.get(validatorInstance.expressMiddleware(operationId), handlerFunction)
  // propagates an error, so custom response can be built in an express error handler

// Validating responses BETA
const req = {
      body: {<parsed JSON>},
      statusCode: 200,
      method: 'get',
      headers: {
        'content-type': 'application/json'
      }
    }
validatorInstance.validateResponse(req, operationId, { path: "the path from the request - to match operationId" }) 

Expected format of req object

If you want to use the validator outside express, you can provide a plain object where req is expected. It needs to provide the fields shown below.

req := {
  path: 'string',
  route: { path: 'string' }, // all use of : is removed, the :paramName will be compared with paramName from definition
  method: 'string', // method name, case insensitive match with the API spec
  body: Body, // Whatever you expect as body - probably an object resulting from parsing JSON
  headers: { //headers are optional, but if request has a body, the content-type header must exist and match the key in content field exactly.
  }
}

Example:

req = {
  path: '/pet/1',
  route: { path: '/pet/:petId' },
  method: 'post',
  body: { ... },
  headers: {
    'content-type': 'application/json'
  }
}

Contributing

Contributing other framework specific middlewares is more than welcome.

When contributing make sure you run npm run lint and don't get any errors nor warnings. Feel free to consciously add security warning exceptions, we can discuss in the pull request.