Socket
Book a DemoInstallSign in
Socket

restify-swagger-validation-middleware

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

restify-swagger-validation-middleware

Restify middleware for validating REST requests with swagger specifications

Source
npmnpm
Version
0.0.1
Version published
Weekly downloads
10
900%
Maintainers
1
Weekly downloads
 
Created
Source

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.

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

Usage:

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

Example:

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

// 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 = null; // No options yet 
    server = restify.createServer();
    // validation middleware requires query and body parser to be used,
    // both have to disable mapping their properties into req.params
    server.use(restify.queryParser({mapParams: false}));
    server.use(restify.bodyParser({mapParams: false}));
    server.use(middleware(options, swaggerAPI));

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

Keywords

node

FAQs

Package last updated on 13 Jul 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts