request-guardian 1.0.4 → 1.0.6
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 +83 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -0,0 +1,83 @@
|
|
|
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 400 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
|
+
```javascript
|
|
22
|
+
const express = require('express');
|
|
23
|
+
const validate = require('request-guardian');
|
|
24
|
+
|
|
25
|
+
const app = express();
|
|
26
|
+
|
|
27
|
+
// use request-guardian middleware
|
|
28
|
+
app.use(validate);
|
|
29
|
+
|
|
30
|
+
// define your routes
|
|
31
|
+
app.get('/', (req, res) => {
|
|
32
|
+
// handle request
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
app.post('/users', (req, res) => {
|
|
36
|
+
// handle validated request
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
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.
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
// utils/validations/index.js
|
|
44
|
+
|
|
45
|
+
const { body } = require('express-validator');
|
|
46
|
+
|
|
47
|
+
module.exports = {
|
|
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
|
+
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
|
+
// use request-guardian middleware for a specific route
|
|
74
|
+
app.post('/users', (req, res) => {
|
|
75
|
+
// handle validated request
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
app.listen(3000, () => {
|
|
79
|
+
console.log('Server started on port 3000');
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
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.
|