1.0.19 • Published 12 months ago

request-guardian v1.0.19

Weekly downloads
-
License
ISC
Repository
github
Last release
12 months ago

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.

1.0.19

12 months ago

1.0.18

12 months ago

1.0.17

12 months ago

1.0.16

12 months ago

1.0.15

12 months ago

1.0.14

12 months ago

1.0.13

12 months ago

1.0.12

12 months ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago