@koziatek/http 1.1.1 → 1.2.1
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/index.js +3 -1
- package/middleware/ValidateSchema.js +26 -0
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -7,6 +7,7 @@ const { ResData } = require("./middleware/ResData");
|
|
|
7
7
|
const { SendRes } = require("./middleware/SendRes");
|
|
8
8
|
const { Status } = require("./middleware/Status");
|
|
9
9
|
const { RequireAuth } = require("./middleware/Auth");
|
|
10
|
+
const { ValidateSchema } = require("./middleware/ValidateSchema");
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
module.exports = {
|
|
@@ -19,7 +20,8 @@ module.exports = {
|
|
|
19
20
|
ResData,
|
|
20
21
|
SendRes,
|
|
21
22
|
Status,
|
|
22
|
-
RequireAuth
|
|
23
|
+
RequireAuth,
|
|
24
|
+
ValidateSchema
|
|
23
25
|
},
|
|
24
26
|
|
|
25
27
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const { ZodType } = require('zod');
|
|
2
|
+
|
|
3
|
+
function ValidateSchema(schema, {prop='body'}) {
|
|
4
|
+
if (!(schema instanceof ZodType)) {
|
|
5
|
+
throw new Error('ValidateSchema expects a Zod schema');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
return (req, res, next) => {
|
|
9
|
+
const result = schema.safeParse(req[prop]);
|
|
10
|
+
|
|
11
|
+
if (!result.success) {
|
|
12
|
+
return res.status(422).json({
|
|
13
|
+
error: 'Invalid Data Schema',
|
|
14
|
+
location: prop,
|
|
15
|
+
details: result.error.flatten(),
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
req[prop] = result.data;
|
|
20
|
+
next();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = {
|
|
25
|
+
ValidateSchema
|
|
26
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koziatek/http",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@koziatek/utils": "^1.0.0",
|
|
17
17
|
"jsonwebtoken": "^9.0.3",
|
|
18
|
-
"jwks-rsa": "^3.2.2"
|
|
18
|
+
"jwks-rsa": "^3.2.2",
|
|
19
|
+
"zod": "^4.3.6"
|
|
19
20
|
},
|
|
20
21
|
"devDependencies": {
|
|
21
22
|
"jest": "^29.7.0"
|