request-guardian 1.0.4 → 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.
Files changed (2) hide show
  1. package/README.md +84 -0
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -0,0 +1,84 @@
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
+ // 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
+ });
82
+ ```
83
+
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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "request-guardian",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {