request-guardian 1.0.2 → 1.0.4
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 +0 -85
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
## Express Request Body Validation Middleware
|
|
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.
|
|
4
|
-
|
|
5
|
-
### Installation
|
|
6
|
-
|
|
7
|
-
To use this middleware in your project, install the required dependencies by running the following command:
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
npm install express-validator
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
### Usage
|
|
14
|
-
|
|
15
|
-
To use this middleware, simply require it in your route handler and add it as a middleware to your route. For example:
|
|
16
|
-
|
|
17
|
-
```js
|
|
18
|
-
const express = require('express');
|
|
19
|
-
const validate = require('./path/to/validate');
|
|
20
|
-
|
|
21
|
-
const app = express();
|
|
22
|
-
|
|
23
|
-
app.post('/api/authentication/signup', validate, (req, res) => {
|
|
24
|
-
// handle signup request
|
|
25
|
-
});
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
In the above example, the `validate` middleware will validate the request body against the rules specified in `/utils/validations/index.js`.
|
|
29
|
-
|
|
30
|
-
### Validation Rules
|
|
31
|
-
|
|
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.
|
|
33
|
-
|
|
34
|
-
For example, the validation rules for the `/api/authentication/signup` route could be defined as follows:
|
|
35
|
-
|
|
36
|
-
```js
|
|
37
|
-
const { body } = require('express-validator');
|
|
38
|
-
|
|
39
|
-
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
|
-
};
|
|
47
|
-
```
|
|
48
|
-
|
|
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
|
-
```
|