response-standardizer 1.1.7 → 1.1.9

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/dist/index.d.ts CHANGED
@@ -24,3 +24,4 @@ export declare const warn: (message: string, meta?: any) => void;
24
24
  export declare const info: (message: string, meta?: any) => void;
25
25
  export declare const log: (level: "INFO" | "WARN" | "ERROR", message: string, meta?: any) => void;
26
26
  export declare const handleRestException: (req: Request, res: Response, err: Error) => void;
27
+ export declare const handleValidationException: (error: any) => void;
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ import { generateRequestId, getTimestamp } from "./utils.js";
2
2
  import axios from "axios";
3
3
  import jwt from "jsonwebtoken";
4
4
  import path from "path";
5
+ import { ZodError } from "zod";
5
6
  import { ServiceException } from "./service.exception.js";
6
7
  let KEYCLOAK_PUBLIC_KEY = null;
7
8
  export const initKeycloak = async (config) => {
@@ -189,3 +190,16 @@ export const handleRestException = (req, res, err) => {
189
190
  RestResponse.exceptionError(req, res, err.message);
190
191
  }
191
192
  };
193
+ export const handleValidationException = (error) => {
194
+ if (error instanceof ZodError) {
195
+ const validationErrors = {};
196
+ error.issues.forEach((issue) => {
197
+ const field = issue.path[0];
198
+ if (!validationErrors[field]) {
199
+ validationErrors[field] = [];
200
+ }
201
+ validationErrors[field].push(issue.message);
202
+ });
203
+ throw new ServiceException("Validation error", 400, validationErrors);
204
+ }
205
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "response-standardizer",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "Express middleware to standardize API responses",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,7 +24,8 @@
24
24
  "dependencies": {
25
25
  "axios": "^1.13.2",
26
26
  "jsonwebtoken": "^9.0.2",
27
- "uuid": "^9.0.1"
27
+ "uuid": "^9.0.1",
28
+ "zod": "^4.1.12"
28
29
  },
29
30
  "devDependencies": {
30
31
  "@types/express": "^5.0.5",
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ import { generateRequestId, getTimestamp } from "./utils.js";
4
4
  import axios from "axios";
5
5
  import jwt, { TokenExpiredError } from "jsonwebtoken";
6
6
  import path from "path";
7
+ import { ZodError } from "zod";
7
8
  import { ServiceException } from "./service.exception.js";
8
9
  declare global {
9
10
  namespace Express {
@@ -244,4 +245,18 @@ export const handleRestException = (req: Request, res: Response, err: Error) =>
244
245
  }else if(err instanceof Error){
245
246
  RestResponse.exceptionError(req, res, err.message)
246
247
  }
248
+ }
249
+
250
+ export const handleValidationException = (error: any) => {
251
+ if (error instanceof ZodError) {
252
+ const validationErrors: any = {};
253
+ error.issues.forEach((issue) => {
254
+ const field = issue.path[0] as string;
255
+ if (!validationErrors[field]) {
256
+ validationErrors[field] = [];
257
+ }
258
+ validationErrors[field].push(issue.message);
259
+ });
260
+ throw new ServiceException("Validation error", 400, validationErrors);
261
+ }
247
262
  }