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

swagger-inputs-validator

v1.0.8

Published

Lightweight Express middleware that allows the validation of incoming parameters.

Downloads

25

Readme

swagger-inputs-validator

npm version Travis Build Status

###About

Lightweight Express middleware that controls your incoming requests. It will reject all requests that do not respect the requirements written in your swagger.

So far, the middleware is able to control the parameters present in :

  • req.query
  • req.params
  • req.body

You have two ways of using this middleware :

  • Control all the requests by using an application middleware
  • Control a specific route by using a route middleware

Installation

$ npm install swagger-inputs-validator --save

###Examples

#####Controls your entire application :

var express = require('express');
var SwaggerValidator = require('swagger-inputs-validator');
var swaggerFile = require("./swagger.json");
var app = express();

var swaggerMiddleware = new SwaggerValidator(swaggerFile);

app.use(swaggerMiddleware.all());

app.get('/products', function(req,res){
  res.json({success: 'If you can enter here it seems that swagger validator let you get in'});
});

app.listen(80);

#####Controls a specific route :

var express = require('express');
var SwaggerValidator = require('swagger-inputs-validator');
var swaggerFile = require("./swagger.json");
var app = express();

var swaggerMiddleware = new SwaggerValidator(swaggerFile);

app.get('/products', swaggerMiddleware.get('/products'), function(req,res){
  res.json({success: 'If you can enter here it seems that swagger validator let you get in'});
});

app.listen(80);

Why would I use only a route middleware if I can control my entire app using swaggerMiddleware.all() ?

You might want to implement this middleware progressively into your code. This is why, you would start by refractoring your code route by route instead of controlling your whole app. Moreover, for tests purpose, you might want to disable some controls and let others active.

So far, you can use the route middleware with the following HTTP verbs :

  • GET
  • POST
  • PUT
  • DELETE
  • HEAD
  • PATCH

Options

When you are calling the constructor, you can specify options :

  • strict : Default to false. When it's true, SwaggerInputsValidator will reject all the incoming parameters that are not specified in the swagger file
  • onError : a function that will handle your custom error behaviour
  • allowNull : Default to true, allows your users to send you (in the body) variables that are equal to null.

var customErrorHandler = function(errors, req, res){
  res.status(400);//You could choose a custom error code
  res.json({message : "This message is coming from a custom error handler. Please find all your mistakes in the errors variable", errors : errors});
};

var swaggerMiddleware = new SwaggerValidator(swaggerFile, {strict : true, onError : customErrorHandler, allowNull : false});

###Current release capabilities

  • [x] Parse a swagger json file (2.0 specification)
  • [x] checking parameters in req.query
  • [x] checking parameters in req.body
  • [x] checking parameters in req.params
  • [ ] checking parameters in req.headers
  • [x] checking optinal variables
  • [x] checking variable types string | integer | double | boolean
  • [x] checking object structure present within the body
  • [ ] checking parameters in req.file
  • [x] checking arrays
  • [ ] checking string patterns (with RegExp)

We are currently working to enhance this middleware, any contribution is welcome :)

###Tests

Unit tests have been written using Mocha. To launch them, please run the following command :

$ npm test