1.1.0 • Published 4 years ago

swagger-validation-express v1.1.0

Weekly downloads
6
License
MIT
Repository
github
Last release
4 years ago

swagger-validation-express

Build Status

use your swagger file as a validation for express application

Tech

swagger-validation-express uses the open source definition of swagger to work properly:

Coming soon

  • headers validation
  • formdata validation
  • logger
  • white listing of url
  • flexible swagger file path

Installation

swagger-validation-express:

-requires Node.js v12+ to run as expected. -support typeScript -body should be parsed (use express.json() or body-parser)

1- Installing the module

$ npm i swagger-validation-express

or

$ yarn add swagger-validation-express

2- Including the swagger file in this version the module is expecting the swagger file to be as json at root level

├── package.json
├── package-lock.json
├── README.md
├── server.js
├── settings.json
├── src
├── swagger.json   //the same level with server.js and with exact name swagger.json
└── tsconfig.json

3-Use the middleware basicly theres two ways to use the module, one is flexible and the other is outside the box

import * as express from 'express';
import * as swaggerValidation from 'swagger-validation-express';
const app = express();
const PORT = 2900;
app.use(express.json());
app.use(swaggerValidation.middleware); //using the module as the midlleware
app.post('/test', (req, res) => {
    res.json({ status: 'success' });
});
app.listen(PORT);

OR

import * as express from 'express';
import * as swaggerValidation from 'swagger-validation-express';
const app = express();
const PORT = 2900;
app.use(express.json());
app.use((req, res, next) => {
    // add your own logic...
    try {
        swaggerValidation.validate(req);
        next();
    } catch (error) {
        res.status(401).json({ status: 'Rejected', message: error.message });
    }
});
app.post('/test', (req, res) => {
    res.json({ status: 'success' });
});
app.listen(PORT);

Example

Lets use our swagger file : https://github.com/uranium93/swagger-validation-express/blob/development/swagger.json and now lets try to send this request to our server

curl --location --request POST 'http://localhost:2900/service1/hjs/hold?query2=test&query1=111-dd-XRPW' \
--header 'Content-Type: application/json' \
--data-raw '{
    "key2": "string DATA",
    "record": {
        "subKey1": 12345,
        "subKey2": {
            "final": true,
            "script":"<script>some important scripts that should be allowed and well formated /script>"
        }
    }
}'

the request will be refused because script value didn't match the swagger pattern validation

{
    "status": "Rejected",
    "message": "<script>some important scripts that should be allowed and well formated <script> is not valid : ^<script>.*</script>$"
}

Development

Want to contribute? Great!

swagger-valdiation-express uses typescript for secure developing. Make a change in your file and instantaneously see your updates!

Open your favorite Terminal and run these commands.

First Tab:

$ git clone git@github.com:uranium93/swagger-validation-express.git

Second Tab:

$ cd swagger-valdiation-express

Third:

$ npm install

Fourth:

$ npm run dev

to run the development server