request-guardian 2.0.1 → 2.0.2
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 +59 -17
- package/package.json +2 -2
- package/utils/validations/index.js +10 -5
package/README.md
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Request Guardian
|
|
2
2
|
|
|
3
|
-
`request-guardian`
|
|
3
|
+
`request-guardian` registers [express-validator](https://express-validator.github.io/) rules on an Express app. Rules come from your project's `utils/validations/index.js`. Failed validation returns **422**. Unexpected errors while running validators return **400**.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
To install `request-guardian`, use `npm` or `yarn`.
|
|
8
|
-
|
|
9
7
|
```bash
|
|
10
8
|
npm install request-guardian
|
|
11
9
|
```
|
|
@@ -14,11 +12,11 @@ npm install request-guardian
|
|
|
14
12
|
yarn add request-guardian
|
|
15
13
|
```
|
|
16
14
|
|
|
17
|
-
|
|
15
|
+
This package depends on `express-validator`. Use it with Express.
|
|
18
16
|
|
|
19
|
-
|
|
17
|
+
## Usage
|
|
20
18
|
|
|
21
|
-
|
|
19
|
+
Call `validate(app)` **after** body parsers such as `express.json()`, and **before** you define route handlers. `validate` attaches middleware when it runs, so calling it after `app.post(...)` can skip validation.
|
|
22
20
|
|
|
23
21
|
```javascript
|
|
24
22
|
const express = require('express');
|
|
@@ -26,25 +24,29 @@ const validate = require('request-guardian');
|
|
|
26
24
|
|
|
27
25
|
const app = express();
|
|
28
26
|
|
|
29
|
-
// middleware to parse incoming requests with JSON payloads.
|
|
30
27
|
app.use(express.json());
|
|
31
|
-
|
|
32
|
-
// Use Request Guardian middleware
|
|
33
28
|
validate(app);
|
|
34
29
|
|
|
35
|
-
// define your routes
|
|
36
30
|
app.post('/users', (req, res) => {
|
|
37
|
-
//
|
|
31
|
+
// request already passed validation for this path
|
|
38
32
|
});
|
|
39
33
|
```
|
|
40
34
|
|
|
41
|
-
|
|
35
|
+
Put validation rules in `utils/validations/index.js` at the **application root** (the process working directory), not inside `node_modules`. Keys must match the Express paths you register.
|
|
36
|
+
|
|
37
|
+
Each path can be:
|
|
38
|
+
|
|
39
|
+
- An **array** of validation chains: applied to every method (`app.use`).
|
|
40
|
+
- An **object** keyed by HTTP method: different rules for `GET`, `POST`, and other methods on the same path.
|
|
41
|
+
|
|
42
|
+
Supported method keys (case-insensitive): `all`, `get`, `post`, `put`, `delete`, `patch`, `options`, `head`. Unknown keys are ignored.
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
### Method-specific rules
|
|
45
|
+
|
|
46
|
+
Use this when the same path needs different rules per method:
|
|
44
47
|
|
|
45
48
|
```javascript
|
|
46
49
|
// utils/validations/index.js
|
|
47
|
-
|
|
48
50
|
const { body, query } = require('express-validator');
|
|
49
51
|
|
|
50
52
|
module.exports = {
|
|
@@ -60,9 +62,23 @@ module.exports = {
|
|
|
60
62
|
};
|
|
61
63
|
```
|
|
62
64
|
|
|
63
|
-
|
|
65
|
+
`all` applies to every method on that path, the same as a bare array:
|
|
64
66
|
|
|
65
67
|
```javascript
|
|
68
|
+
module.exports = {
|
|
69
|
+
'/api/reports': {
|
|
70
|
+
all: [
|
|
71
|
+
query('from').optional().isISO8601(),
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Same rules for every method
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
const { body } = require('express-validator');
|
|
81
|
+
|
|
66
82
|
module.exports = {
|
|
67
83
|
'/api/users': [
|
|
68
84
|
body('name').notEmpty(),
|
|
@@ -70,4 +86,30 @@ module.exports = {
|
|
|
70
86
|
};
|
|
71
87
|
```
|
|
72
88
|
|
|
73
|
-
If no validation
|
|
89
|
+
If a path is not listed, no validation middleware is attached for it. If `utils/validations/index.js` is missing, `validate()` logs `Validations rules not found at utils/validations/index.js` and registers nothing.
|
|
90
|
+
|
|
91
|
+
## Responses
|
|
92
|
+
|
|
93
|
+
Validation failure:
|
|
94
|
+
|
|
95
|
+
```json
|
|
96
|
+
{
|
|
97
|
+
"data": [],
|
|
98
|
+
"status": "VALIDATION_ERROR",
|
|
99
|
+
"message": "Invalid Data, Validation Failed."
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`data` is `express-validator`'s `errors.array()`.
|
|
104
|
+
|
|
105
|
+
Unexpected error while running validators:
|
|
106
|
+
|
|
107
|
+
```json
|
|
108
|
+
{
|
|
109
|
+
"data": "error message",
|
|
110
|
+
"status": "BAD_REQUEST",
|
|
111
|
+
"message": "error message"
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
HTTP status codes: **422** for validation errors, **400** for other failures in the validator.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "request-guardian",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"description": "Express middleware that runs express-validator rules from utils/validations/index.js, including per-method rules.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
const { body } = require('express-validator');
|
|
1
|
+
const { body, query } = require('express-validator');
|
|
2
2
|
|
|
3
3
|
module.exports = {
|
|
4
|
-
'/api/authentication/signup':
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
'/api/authentication/signup': {
|
|
5
|
+
GET: [
|
|
6
|
+
query('page').optional().isInt({ min: 1 }),
|
|
7
|
+
],
|
|
8
|
+
POST: [
|
|
9
|
+
body('email').isEmail(),
|
|
10
|
+
body('password').isLength({ min: 8 }),
|
|
11
|
+
],
|
|
12
|
+
},
|
|
8
13
|
};
|