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

restify-swagger-validation-middleware

v0.0.5

Published

Restify middleware for validating REST requests with swagger specifications

Downloads

4

Readme

restify-swagger-validation-middleware

Restify middleware for validating REST requests with swagger specifications.

Validates all query, route and body parameters according to the swagger definition.

This library is not production-ready yet, consider this pre-alpha.

How it works:

This middleware requires the built-in restify plugins queryParse and bodyParser loaded before it, both with mapParams set to false.

It also requires a valid de-referenced swagger 2.0 definition, you can use the module swagger-parser for this.

For every incoming request it will look up the swagger definition for the parameters for the active route and operation (swagger allows parameters on the whole route and on specific operations on the route, the middleware will merge both). It will then create a JSON schema which validates all query, body and route parameters at once. It then compiles this schema (using the great ajv library) to a javascript function which validates the data from the request. The generated function for this route and operatinm is cached through lodash.memoize().

ajv will also handle settings defaults and type coercion.

The middleware will create a swagger object on the restify request: req.swagger.

The swagger object will contain the following properties:

  • api: contains the parsed swaggerAPI
  • query: contains all query parameters, type-coerced and with defaults
  • path: contains all route path parameters, type-coerced and with defaults
  • body: contains all body parameters, type-coerced and with defaults
  • params: contains all query, path and body parameters merged together

The middleware will NOT change existing values in req.query, req.params and req.body.

If validation succeeds, the next middleware will just be called with next()

If validation fails, per default a restify ValidationError (BadRequest, status 400) will be generated. The error will contain the error collection from ajv.

The generated Error can be customized by passing errorTransformer and errorResponder in the middleware options.

A typical validation error response body will look like this:

{
  "code": "ValidationError",
  "errors": [
    {
      "data": "abc",
      "dataPath": ".query.test",
      "keyword": "enum",
      "message": "should be equal to one of the allowed values",
      "params": {},
      "schema": [
        "enum1",
        "enum2"
      ],
      "schemaPath": "#/properties/query/properties/test/enum"
    }
  ],
  "message": "Validation error"
}

Example usage of middleware:

const restify = require('restify');
const SwaggerParser = require('swagger-parser');
const restifySwaggerValidationMiddleware = require('restify-swagger-validation-middleware');

// In a real world project you would read your api from the file system
let api = {
    swagger: '2.0',
    info: {title: 'test', version: '1.0'},
    paths: {
      '/test': {
        get: {
          parameters: [{name: 'test', type: 'integer', in: 'query', required: true}],
          responses: {'200': {description: 'no content'}}
        }
      }
    }
};

SwaggerParser.validate(api)
  .then((swaggerAPI) => {
    let options = {};
    server = restify.createServer();
    // validation middleware requires query and body parser to be used,
    // both have to disable mapping their properties into req.params
    // so req.params only contains the route path parameters.
    server.use(restify.queryParser({mapParams: false}));
    server.use(restify.bodyParser({mapParams: false}));
    server.use(restifySwaggerValidationMiddleware(swaggerAPI, options));

    server.listen(PORT, '127.0.0.1', () => {
       // your code
    })
  })

Notes:

  • The middleware could easily be ported to express I guess as it has no direct dependency on restify and middlewares look quite the same in restify and express. What hat to change is the validation error handling as that needs restify-errors.
  • Also, it should be easy to use the same mechanism (mainly delegating everything to avj) with RAML instead of swagger, it should be even easier because RAML is much closer to the JSON schema standard and body and query parameters are not handled differently like in swagger.