Socket
Socket
Sign inDemoInstall

request-guardian

Package Overview
Dependencies
3
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    request-guardian

`request-guardian` is a middleware function that validates incoming requests against a set of validation rules using `express-validator`. It can be used to ensure that data sent to a server is in the expected format and meets certain criteria. If the vali


Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Readme for Request-Guardian

request-guardian is a middleware function that validates incoming requests against a set of validation rules using express-validator. It can be used to ensure that data sent to a server is in the expected format and meets certain criteria. If the validation fails, it returns a 400 error response with an array of validation errors.

Installation

To install request-guardian, use npm or yarn.

npm install request-guardian
yarn add request-guardian

Usage

request-guardian is a middleware function that can be used with Express applications. To use it, simply require the module and use it as middleware for your routes.

NOTE: Make sure that you call the validate() method after a middleware to parse incoming requests with JSON payloads.

const express = require('express');
const validate = require('request-guardian');

const app = express();

// middleware to parse incoming requests with JSON payloads.
app.use(express.json());

// Use Request Guardian middleware
validate(app);

// define your routes
app.post('/users', (req, res) => {
    // handle validated request
});

Validation rules are defined in separate files located in the utils/validations/index.js directory. request-guardian will look for a file with the same name as the current route and load any validation rules defined within that file.

// utils/validations/index.js

const { body } = require('express-validator');

module.exports = {
    '/api/authentication/signup': [
        body('email').isEmail(),
        body('password').isLength({ min: 8 }),
    ]
} ;

If no validation rules are found for the current route, request-guardian will simply pass the request to the next middleware function in the stack.

Keywords

FAQs

Last updated on 18 May 2023

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc