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.
- package/README.md +61 -62
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,85 +1,84 @@
|
|
|
1
|
-
|
|
1
|
+
# Readme for Request-Guardian
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
To
|
|
7
|
+
To install `request-guardian`, use `npm` or `yarn`.
|
|
8
8
|
|
|
9
|
+
```bash
|
|
10
|
+
npm install request-guardian
|
|
9
11
|
```
|
|
10
|
-
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add request-guardian
|
|
11
15
|
```
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
## Usage
|
|
14
18
|
|
|
15
|
-
|
|
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
|
-
```
|
|
21
|
+
```javascript
|
|
18
22
|
const express = require('express');
|
|
19
|
-
const validate = require('
|
|
23
|
+
const validate = require('request-guardian');
|
|
20
24
|
|
|
21
25
|
const app = express();
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
});
|
|
26
|
-
```
|
|
27
|
+
// use request-guardian middleware
|
|
28
|
+
app.use(validate);
|
|
27
29
|
|
|
28
|
-
|
|
30
|
+
// define your routes
|
|
31
|
+
app.get('/', (req, res) => {
|
|
32
|
+
// handle request
|
|
33
|
+
});
|
|
29
34
|
|
|
30
|
-
|
|
35
|
+
app.post('/users', (req, res) => {
|
|
36
|
+
// handle validated request
|
|
37
|
+
});
|
|
38
|
+
```
|
|
31
39
|
|
|
32
|
-
|
|
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
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
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.
|