@koziatek/http 1.1.0 → 1.2.0

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 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
  }
@@ -1,4 +1,5 @@
1
1
  const { HttpStatusCodes } = require('../HttpStatusCodes.js');
2
+ const { isFunction } = require('@koziatek/utils');
2
3
 
3
4
 
4
5
  function ErrorRenderer(err, req, res, next) {
@@ -0,0 +1,25 @@
1
+ const { ZodType } = require('zod');
2
+
3
+ function ValidateSchema(schema) {
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.body);
10
+
11
+ if (!result.success) {
12
+ return res.status(422).json({
13
+ error: 'Invalid Data Schema',
14
+ details: result.error.flatten(),
15
+ });
16
+ }
17
+
18
+ req.body = result.data;
19
+ next();
20
+ }
21
+ }
22
+
23
+ module.exports = {
24
+ ValidateSchema
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koziatek/http",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
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"