request-guardian 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +61 -62
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,85 +1,84 @@
1
- ## Express Request Body Validation Middleware
1
+ # Readme for Request-Guardian
2
2
 
3
- This middleware provides request body validation using [express-validator](https://express-validator.github.io/docs/) library. It is intended to be used as a middleware function in an Express.js application.
3
+ `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.
4
4
 
5
- ### Installation
5
+ ## Installation
6
6
 
7
- To use this middleware in your project, install the required dependencies by running the following command:
7
+ To install `request-guardian`, use `npm` or `yarn`.
8
8
 
9
+ ```bash
10
+ npm install request-guardian
9
11
  ```
10
- npm install express-validator
12
+
13
+ ```bash
14
+ yarn add request-guardian
11
15
  ```
12
16
 
13
- ### Usage
17
+ ## Usage
14
18
 
15
- To use this middleware, simply require it in your route handler and add it as a middleware to your route. For example:
19
+ `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.
16
20
 
17
- ```js
21
+ ```javascript
18
22
  const express = require('express');
19
- const validate = require('./path/to/validate');
23
+ const validate = require('request-guardian');
20
24
 
21
25
  const app = express();
22
26
 
23
- app.post('/api/authentication/signup', validate, (req, res) => {
24
- // handle signup request
25
- });
26
- ```
27
+ // use request-guardian middleware
28
+ app.use(validate);
27
29
 
28
- In the above example, the `validate` middleware will validate the request body against the rules specified in `/utils/validations/index.js`.
30
+ // define your routes
31
+ app.get('/', (req, res) => {
32
+ // handle request
33
+ });
29
34
 
30
- ### Validation Rules
35
+ app.post('/users', (req, res) => {
36
+ // handle validated request
37
+ });
38
+ ```
31
39
 
32
- All the validation rules should be located at `/utils/validations/index.js`. The file should export an object with keys as route paths and values as arrays of validation rules.
40
+ 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.
33
41
 
34
- For example, the validation rules for the `/api/authentication/signup` route could be defined as follows:
42
+ ```javascript
43
+ // utils/validations/index.js
35
44
 
36
- ```js
37
45
  const { body } = require('express-validator');
38
46
 
39
47
  module.exports = {
40
- '/api/authentication/signup': [
41
- body('email').notEmpty().withMessage('Email is required')
42
- .isEmail().withMessage('Email is not valid'),
43
- body('password').notEmpty().withMessage('Password is required')
44
- .isLength({ min: 5 }).withMessage('Password length should be minimum 5')
45
- ]
46
- };
48
+ '/api/authentication/signup': [
49
+ body('email').isEmail(),
50
+ body('password').isLength({ min: 8 }),
51
+ ]
52
+ } ;
53
+ ```
54
+
55
+
56
+
57
+ ```javascript
58
+ // app.js
59
+
60
+ const express = require('express');
61
+ const validate = require('request-guardian');
62
+
63
+ // use request-guardian middleware
64
+ const app = express();
65
+
66
+
67
+ app.use(validate);
68
+
69
+ // define your routes
70
+ app.get('/', (req, res) => {
71
+ // handle request
72
+ });
73
+
74
+ // use request-guardian middleware for a specific route
75
+ app.post('/users', (req, res) => {
76
+ // handle validated request
77
+ });
78
+
79
+ app.listen(3000, () => {
80
+ console.log('Server started on port 3000');
81
+ });
47
82
  ```
48
83
 
49
- ### Error Handling
50
-
51
- If any of the validation rules fail, the middleware will send a `400 Bad Request` response with an array of error messages. The error messages will be in the format specified by `express-validator`.
52
-
53
- If the validation rules for a route are not found, the middleware will send a `400 Bad Request` response with an appropriate error message.
54
-
55
- ### Code
56
-
57
- ```js
58
- const { validationResult, ValidationChain } = require('express-validator');
59
-
60
- async function validate(req, res, next) {
61
- try {
62
- let validations = require('../utils/validations')[req.path];
63
- if (!validations) return next();
64
- for (let validation of validations) {
65
- const result = await validation.run(req);
66
- if (result.errors.length) break;
67
- }
68
-
69
- const errors = validationResult(req);
70
- delete req.validations;
71
- if (errors.isEmpty()) {
72
- return next();
73
- }
74
- res.status(400).json({ errors: errors.array() });
75
- } catch (error) {
76
- let message = error.message;
77
- if (error.code === 'MODULE_NOT_FOUND') {
78
- message = `Validations rules not found for ${req.path}`;
79
- }
80
- res.status(400).json({ errors: message });
81
- }
82
- };
83
-
84
- module.exports = validate;
85
- ```
84
+ 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "request-guardian",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {