@sn1006/common 1.0.12 → 1.0.15
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/build/errors/validation-error.d.ts +7 -4
- package/build/errors/validation-error.js +1 -2
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/middlewares/joi-validate-request.d.ts +3 -0
- package/build/middlewares/joi-validate-request.js +18 -0
- package/build/middlewares/validate-request.js +4 -3
- package/package.json +2 -1
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { NormalizedError } from './normalized-error';
|
|
2
|
-
|
|
2
|
+
interface NonSerializedValidationError {
|
|
3
|
+
msg: string;
|
|
4
|
+
path: string;
|
|
5
|
+
}
|
|
3
6
|
declare class CustomValidationError extends NormalizedError {
|
|
4
|
-
errors:
|
|
7
|
+
errors: NonSerializedValidationError[];
|
|
5
8
|
status: number;
|
|
6
|
-
constructor(errors:
|
|
9
|
+
constructor(errors: NonSerializedValidationError[]);
|
|
7
10
|
serializeErrors(): {
|
|
8
|
-
message:
|
|
11
|
+
message: string;
|
|
9
12
|
field: string;
|
|
10
13
|
}[];
|
|
11
14
|
}
|
|
@@ -10,8 +10,7 @@ class CustomValidationError extends normalized_error_1.NormalizedError {
|
|
|
10
10
|
}
|
|
11
11
|
serializeErrors() {
|
|
12
12
|
return this.errors.map(err => {
|
|
13
|
-
|
|
14
|
-
return { message: ferr.msg, field: ferr.path };
|
|
13
|
+
return { message: err.msg, field: err.path };
|
|
15
14
|
});
|
|
16
15
|
}
|
|
17
16
|
}
|
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -24,3 +24,4 @@ __exportStar(require("./middlewares/current-user"), exports);
|
|
|
24
24
|
__exportStar(require("./middlewares/error-handler"), exports);
|
|
25
25
|
__exportStar(require("./middlewares/require-auth"), exports);
|
|
26
26
|
__exportStar(require("./middlewares/validate-request"), exports);
|
|
27
|
+
__exportStar(require("./middlewares/joi-validate-request"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const validation_error_1 = __importDefault(require("../errors/validation-error"));
|
|
7
|
+
function joiValidateRequest(schema, options) {
|
|
8
|
+
return (req, res, next) => {
|
|
9
|
+
const { error } = schema.validate(req, options);
|
|
10
|
+
if (error) {
|
|
11
|
+
// normalized the error to be similar to express-validator error
|
|
12
|
+
const errors = error.details.map((err) => (Object.assign(Object.assign({}, err), { msg: err.message, path: err.path.join(".") })));
|
|
13
|
+
return next(new validation_error_1.default(errors));
|
|
14
|
+
}
|
|
15
|
+
return next();
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
exports.default = joiValidateRequest;
|
|
@@ -7,9 +7,10 @@ exports.validateRequest = void 0;
|
|
|
7
7
|
const express_validator_1 = require("express-validator");
|
|
8
8
|
const validation_error_1 = __importDefault(require("../errors/validation-error"));
|
|
9
9
|
const validateRequest = (req, res, next) => {
|
|
10
|
-
const
|
|
11
|
-
if (!
|
|
12
|
-
|
|
10
|
+
const error = (0, express_validator_1.validationResult)(req);
|
|
11
|
+
if (!error.isEmpty()) {
|
|
12
|
+
const errors = error.array();
|
|
13
|
+
return next(new validation_error_1.default(errors));
|
|
13
14
|
}
|
|
14
15
|
next();
|
|
15
16
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sn1006/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"cookie-session": "^2.0.0",
|
|
27
27
|
"express": "^4.18.2",
|
|
28
28
|
"express-validator": "^7.0.1",
|
|
29
|
+
"joi": "^17.12.1",
|
|
29
30
|
"jsonwebtoken": "^9.0.1"
|
|
30
31
|
}
|
|
31
32
|
}
|