New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@ssense/restify-request-validator

Package Overview
Dependencies
Maintainers
2
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ssense/restify-request-validator

Restify requests validator

  • 1.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
50
increased by354.55%
Maintainers
2
Weekly downloads
 
Created
Source

Restify request validator

Simple restify middleware which adds validation for all incoming requests.

Use in project

Installation

To install the validator for use in your project, go to your project's main directory, then run the following command:

npm install --production --save @ssense/restify-request-validator

Usage

To add the middleware to your current restify project, follow the snippet below:

Initialization
For javascript project
// Require module
var restifyValidation = require('@ssense/restify-request-validator');
...
// Create restify server
var server = restify.createServer();
...
// Add middleware
var validator = new restifyValidation.RequestValidator();
server.use(validator.validate.bind(validator));

By default, on each validation error, the RequestValidator will throw an Error object with a 500 HTTP code.

You can throw a specific restify error and http code, by passing it to the constructor:

var validator = new restifyValidation.RequestValidator(restify.BadRequestError);

With this configuration, on each validation error, the RequestValidator will throw a BadRequestError with a 400 HTTP code.

For typescript project
// Import module
import {RequestValidator} from '@ssense/restify-request-validator';
...
// Create restify server
const server = restify.createServer();
...
// Add middleware
const validator = new RequestValidator();
server.use(validator.validate.bind(validator));

Like for javascript initialization, you can pass a restify error handler in the constructor:

const validator = new RequestValidator(restify.BadRequestError);
Validation
Example usage

Just add a validation param to the route to validate:

function respond(req, res, next) {
    res.send('hello ' + req.params.name);
    next();
}

// Without validation
server.get('/hello/:name', respond);

// With validation
server.get({
    url: '/hello/:name',
    validation: {
        url: {
            name: {type: 'string', required: true, min: 3},
        },
    },
}, respond);

With this validation, the name param must be a string with minimum length of 3.

Validation object

The validation object accepts 3 optional properties:

  • url to validate parameters in url path
  • query to validate url query parameters
  • body to validate request body parameters (useful for POST or PUT requests)

To validate inputs, just add properties to url, query, and/or body params with the following syntax :

validation: {
    <url/query/body>: {
        <property_name>: {
            type: 'valid type', // required field, supported types are 'string', 'number', 'boolean', 'numeric', 'array', 'object'
            required: true|false, // optional, default false, determines is the parameter is required,
            min: 1, // optional, default 1, if 'type' property is 'string' or 'array', determines the minimum length, if 'type' parameter is 'number', determines the minimum value
            max: 5, // optional, default null, if 'type' property is 'string' or 'array', determines the maximum length, if 'type' parameter is 'number', determines the maximum value
            length: 3, // optional, default null, only works if 'type' property is 'string' or 'array', determines the required length,
            arrayType: 'valid type', // optional, default null, only works if 'type' property is 'array', check if the array content has valid types, supported types: 'string', 'number', 'boolean', 'numeric'
            values: ['value1', 'value2'], // optional, default null, validates that parameter value belongs to the provided list, if 'type' is 'array', validates every array element
            regex: /^Valid regex$/, // optional, default null, validates parameter value against provided regex
        }
    }
}

Development

Installation

To install the validator and get a proper development environment, clone/fork the current repository, then run the following command:

npm install

Running the tests

  • To check coding style (linting), run npm run lint
  • To run the tests for the project, run npm t
  • To run the tests with code coverage, run npm run cover, the result will be available in the folder <your_project_dir>/tests/coverage/index.html

Deployment

Before each commit to this project, don't forget to build javascript files by running the following command:

npm run compile

Authors

  • Rémy Jeancolas - Initial work - RemyJeancolas

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Keywords

FAQs

Package last updated on 02 Sep 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc