request-guardian 1.0.18 → 2.0.1
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.
- package/README.md +72 -55
- package/index.js +89 -44
- package/package.json +2 -2
- package/utils/validations/index.js +8 -0
package/README.md
CHANGED
|
@@ -1,56 +1,73 @@
|
|
|
1
|
-
# Readme for Request-Guardian
|
|
2
|
-
|
|
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
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
To install `request-guardian`, use `npm` or `yarn`.
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install request-guardian
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
yarn add request-guardian
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Usage
|
|
18
|
-
|
|
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.
|
|
20
|
-
|
|
21
|
-
NOTE: Make sure that you call the validate() method after a middleware to parse incoming requests with JSON payloads.
|
|
22
|
-
|
|
23
|
-
```javascript
|
|
24
|
-
const express = require('express');
|
|
25
|
-
const validate = require('request-guardian');
|
|
26
|
-
|
|
27
|
-
const app = express();
|
|
28
|
-
|
|
29
|
-
// middleware to parse incoming requests with JSON payloads.
|
|
30
|
-
app.use(express.json());
|
|
31
|
-
|
|
32
|
-
// Use Request Guardian middleware
|
|
33
|
-
validate(app);
|
|
34
|
-
|
|
35
|
-
// define your routes
|
|
36
|
-
app.post('/users', (req, res) => {
|
|
37
|
-
// handle validated request
|
|
38
|
-
});
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
Validation rules are defined in
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
1
|
+
# Readme for Request-Guardian
|
|
2
|
+
|
|
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 422 error response with an array of validation errors.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
To install `request-guardian`, use `npm` or `yarn`.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install request-guardian
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add request-guardian
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
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.
|
|
20
|
+
|
|
21
|
+
NOTE: Make sure that you call the validate() method after a middleware to parse incoming requests with JSON payloads.
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
const express = require('express');
|
|
25
|
+
const validate = require('request-guardian');
|
|
26
|
+
|
|
27
|
+
const app = express();
|
|
28
|
+
|
|
29
|
+
// middleware to parse incoming requests with JSON payloads.
|
|
30
|
+
app.use(express.json());
|
|
31
|
+
|
|
32
|
+
// Use Request Guardian middleware
|
|
33
|
+
validate(app);
|
|
34
|
+
|
|
35
|
+
// define your routes
|
|
36
|
+
app.post('/users', (req, res) => {
|
|
37
|
+
// handle validated request
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Validation rules are defined in `utils/validations/index.js`. Each route can either use a single array of validation chains for all methods, or a method-specific object when the same path should validate differently by HTTP method.
|
|
42
|
+
|
|
43
|
+
This is useful when the same route name is reused for different methods such as `GET` and `POST` but each method needs different validation logic.
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
// utils/validations/index.js
|
|
47
|
+
|
|
48
|
+
const { body, query } = require('express-validator');
|
|
49
|
+
|
|
50
|
+
module.exports = {
|
|
51
|
+
'/api/authentication/signup': {
|
|
52
|
+
GET: [
|
|
53
|
+
query('page').optional().isInt({ min: 1 }),
|
|
54
|
+
],
|
|
55
|
+
POST: [
|
|
56
|
+
body('email').isEmail(),
|
|
57
|
+
body('password').isLength({ min: 8 }),
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
You can also use a single validation array for all methods on a route:
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
module.exports = {
|
|
67
|
+
'/api/users': [
|
|
68
|
+
body('name').notEmpty(),
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
```
|
|
72
|
+
|
|
56
73
|
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/index.js
CHANGED
|
@@ -1,45 +1,90 @@
|
|
|
1
|
-
const { validationResult } = require('express-validator');
|
|
2
|
-
const path = require('path');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
const { validationResult } = require('express-validator');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
const VALID_HTTP_METHODS = new Set([
|
|
5
|
+
'all',
|
|
6
|
+
'get',
|
|
7
|
+
'post',
|
|
8
|
+
'put',
|
|
9
|
+
'delete',
|
|
10
|
+
'patch',
|
|
11
|
+
'options',
|
|
12
|
+
'head',
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
function runValidator(validations) {
|
|
16
|
+
return async function _runValidator(req, res, next) {
|
|
17
|
+
try {
|
|
18
|
+
if (!validations) return next();
|
|
19
|
+
for (let validation of validations) {
|
|
20
|
+
const result = await validation.run(req);
|
|
21
|
+
if (result.errors.length) break;
|
|
22
|
+
}
|
|
23
|
+
const errors = validationResult(req);
|
|
24
|
+
if (errors.isEmpty()) {
|
|
25
|
+
return next();
|
|
26
|
+
}
|
|
27
|
+
res.status(422).json({
|
|
28
|
+
data: errors.array(),
|
|
29
|
+
status: 'VALIDATION_ERROR',
|
|
30
|
+
message: 'Invalid Data, Validation Failed.',
|
|
31
|
+
});
|
|
32
|
+
} catch (error) {
|
|
33
|
+
let message = error.message;
|
|
34
|
+
if (error.code === 'MODULE_NOT_FOUND') {
|
|
35
|
+
message = `Validations rules not found for ${req.path}`;
|
|
36
|
+
}
|
|
37
|
+
res.status(400).json({
|
|
38
|
+
data: message,
|
|
39
|
+
status: 'BAD_REQUEST',
|
|
40
|
+
message: message,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function registerRouteValidation(app, routeName, validationConfig) {
|
|
47
|
+
if (!validationConfig) return;
|
|
48
|
+
|
|
49
|
+
if (Array.isArray(validationConfig)) {
|
|
50
|
+
app.use(routeName, runValidator(validationConfig));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (typeof validationConfig !== 'object') {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const [method, rules] of Object.entries(validationConfig)) {
|
|
59
|
+
const normalizedMethod = method.toLowerCase();
|
|
60
|
+
|
|
61
|
+
if (!VALID_HTTP_METHODS.has(normalizedMethod)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (Array.isArray(rules)) {
|
|
66
|
+
if (normalizedMethod === 'all') {
|
|
67
|
+
app.use(routeName, runValidator(rules));
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
app[normalizedMethod](routeName, runValidator(rules));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function validate(app) {
|
|
77
|
+
try {
|
|
78
|
+
const validationRules = require(path.resolve('utils/validations/index'));
|
|
79
|
+
|
|
80
|
+
for (const [key, value] of Object.entries(validationRules || {})) {
|
|
81
|
+
registerRouteValidation(app, key, value);
|
|
82
|
+
}
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error.code === 'MODULE_NOT_FOUND') {
|
|
85
|
+
console.log(`Validations rules not found at utils/validations/index.js`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
45
90
|
module.exports = validate;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "request-guardian",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/saiyadzaid/request-guardian.git"
|
|
11
|
+
"url": "git+https://github.com/saiyadzaid/request-guardian.git"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"express",
|