request-guardian 1.0.10 → 1.0.12
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 +2 -33
- package/index.js +27 -23
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -24,14 +24,10 @@ const validate = require('request-guardian');
|
|
|
24
24
|
|
|
25
25
|
const app = express();
|
|
26
26
|
|
|
27
|
-
//
|
|
28
|
-
app
|
|
27
|
+
// Use Request Guardian middleware
|
|
28
|
+
validate(app);
|
|
29
29
|
|
|
30
30
|
// define your routes
|
|
31
|
-
app.get('/', (req, res) => {
|
|
32
|
-
// handle request
|
|
33
|
-
});
|
|
34
|
-
|
|
35
31
|
app.post('/users', (req, res) => {
|
|
36
32
|
// handle validated request
|
|
37
33
|
});
|
|
@@ -52,33 +48,6 @@ module.exports = {
|
|
|
52
48
|
} ;
|
|
53
49
|
```
|
|
54
50
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
```javascript
|
|
58
|
-
// app.js
|
|
59
|
-
|
|
60
|
-
const express = require('express');
|
|
61
|
-
const validate = require('request-guardian');
|
|
62
|
-
|
|
63
|
-
const app = express();
|
|
64
|
-
|
|
65
|
-
// use request-guardian middleware
|
|
66
|
-
app.use(validate);
|
|
67
|
-
|
|
68
|
-
// define your routes
|
|
69
|
-
app.get('/', (req, res) => {
|
|
70
|
-
// handle request
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
app.post('/users', (req, res) => {
|
|
74
|
-
// handle validated request
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
app.listen(3000, () => {
|
|
78
|
-
console.log('Server started on port 3000');
|
|
79
|
-
});
|
|
80
|
-
```
|
|
81
|
-
|
|
82
51
|
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.
|
|
83
52
|
|
|
84
53
|
## Keywords
|
package/index.js
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
|
-
const { validationResult
|
|
1
|
+
const { validationResult } = require('express-validator');
|
|
2
|
+
const path = require('path');
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
function runValidator(validations) {
|
|
5
|
+
return async function _runValidator(req, res, next) {
|
|
6
|
+
try {
|
|
7
|
+
if (!validations) return next();
|
|
8
|
+
for (let validation of validations) {
|
|
9
|
+
const result = await validation.run(req);
|
|
10
|
+
if (result.errors.length) break;
|
|
11
|
+
}
|
|
12
|
+
const errors = validationResult(req);
|
|
13
|
+
if (errors.isEmpty()) {
|
|
14
|
+
return next();
|
|
15
|
+
}
|
|
16
|
+
res.status(400).json({ errors: errors.array() });
|
|
17
|
+
} catch (error) {
|
|
18
|
+
let message = error.message;
|
|
19
|
+
if (error.code === 'MODULE_NOT_FOUND') {
|
|
20
|
+
message = `Validations rules not found for ${req.path}`;
|
|
21
|
+
}
|
|
22
|
+
res.status(400).json({ errors: message });
|
|
10
23
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
res.status(400).json({ errors: errors.array() });
|
|
18
|
-
} catch (error) {
|
|
19
|
-
let message = error.message;
|
|
20
|
-
if (error.code === 'MODULE_NOT_FOUND') {
|
|
21
|
-
message = `Validations rules not found for ${req.path}`;
|
|
22
|
-
}
|
|
23
|
-
res.status(400).json({ errors: message });
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function validate(app) {
|
|
27
|
+
for (const key in require(path.resolve('utils/validations/index')) ) {
|
|
28
|
+
app.use(key, runValidator(require(path.resolve('utils/validations/index'))[key]));
|
|
24
29
|
}
|
|
25
|
-
}
|
|
26
|
-
|
|
30
|
+
}
|
|
27
31
|
module.exports = validate;
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "request-guardian",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
8
|
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/saiyadzaid/request-guardian.git"
|
|
12
|
+
},
|
|
9
13
|
"keywords": [],
|
|
10
14
|
"author": "",
|
|
11
15
|
"license": "ISC",
|