@levrbet/shared 0.1.65 → 0.1.66
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.
|
@@ -1,25 +1,24 @@
|
|
|
1
1
|
import type { Request, Response } from "express";
|
|
2
2
|
import type { ZodType } from "zod";
|
|
3
3
|
/**
|
|
4
|
-
* Validates a part of an Express request using a Zod schema.
|
|
4
|
+
* Validates a part of an Express request using a Zod schema and returns the validated data.
|
|
5
5
|
*
|
|
6
6
|
* @param schema - The Zod schema to validate against
|
|
7
7
|
* @param req - The Express request object
|
|
8
8
|
* @param part - The part of the request to validate ("body", "query", "params", etc.)
|
|
9
9
|
* @param res - The Express response object
|
|
10
|
-
* @returns
|
|
10
|
+
* @returns The validated data if successful, undefined if validation fails (and sends 400 response)
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* ```typescript
|
|
14
14
|
* const bodySchema = z.object({ name: z.string() });
|
|
15
15
|
*
|
|
16
16
|
* app.post("/api", (req, res) => {
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
17
|
+
* const body = validateRequest(bodySchema, req, "body", res);
|
|
18
|
+
* if (!body) return;
|
|
19
|
+
* // body is now typed as { name: string }
|
|
20
|
+
* console.log(body.name);
|
|
20
21
|
* });
|
|
21
22
|
* ```
|
|
22
23
|
*/
|
|
23
|
-
export declare function validateRequest<T>(schema: ZodType<T>, req: Request, part: keyof Request, res: Response):
|
|
24
|
-
[K in typeof part]: T;
|
|
25
|
-
};
|
|
24
|
+
export declare function validateRequest<T>(schema: ZodType<T>, req: Request, part: keyof Request, res: Response): T | undefined;
|
|
@@ -2,22 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.validateRequest = validateRequest;
|
|
4
4
|
/**
|
|
5
|
-
* Validates a part of an Express request using a Zod schema.
|
|
5
|
+
* Validates a part of an Express request using a Zod schema and returns the validated data.
|
|
6
6
|
*
|
|
7
7
|
* @param schema - The Zod schema to validate against
|
|
8
8
|
* @param req - The Express request object
|
|
9
9
|
* @param part - The part of the request to validate ("body", "query", "params", etc.)
|
|
10
10
|
* @param res - The Express response object
|
|
11
|
-
* @returns
|
|
11
|
+
* @returns The validated data if successful, undefined if validation fails (and sends 400 response)
|
|
12
12
|
*
|
|
13
13
|
* @example
|
|
14
14
|
* ```typescript
|
|
15
15
|
* const bodySchema = z.object({ name: z.string() });
|
|
16
16
|
*
|
|
17
17
|
* app.post("/api", (req, res) => {
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* const body = validateRequest(bodySchema, req, "body", res);
|
|
19
|
+
* if (!body) return;
|
|
20
|
+
* // body is now typed as { name: string }
|
|
21
|
+
* console.log(body.name);
|
|
21
22
|
* });
|
|
22
23
|
* ```
|
|
23
24
|
*/
|
|
@@ -25,14 +26,11 @@ function validateRequest(schema, req, part, res) {
|
|
|
25
26
|
const result = schema.safeParse(req[part]);
|
|
26
27
|
if (!result.success) {
|
|
27
28
|
res.status(400).json({
|
|
28
|
-
error:
|
|
29
|
+
error: `Validation failed for request ${part.toString()}`,
|
|
29
30
|
details: result.error.issues,
|
|
30
31
|
});
|
|
31
|
-
return
|
|
32
|
+
return undefined;
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
-
// @ts-expect-error - We're intentionally overwriting the request part with validated data
|
|
35
|
-
req[part] = result.data;
|
|
36
|
-
return true;
|
|
34
|
+
return result.data;
|
|
37
35
|
}
|
|
38
36
|
//# sourceMappingURL=express.utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"express.utils.js","sourceRoot":"","sources":["../../../src/server/utils/express.utils.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"express.utils.js","sourceRoot":"","sources":["../../../src/server/utils/express.utils.ts"],"names":[],"mappings":";;AAwBA,0CAYC;AAjCD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAgB,eAAe,CAAI,MAAkB,EAAE,GAAY,EAAE,IAAmB,EAAE,GAAa;IACnG,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAA;IAE1C,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QAClB,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACjB,KAAK,EAAE,iCAAiC,IAAI,CAAC,QAAQ,EAAE,EAAE;YACzD,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM;SAC/B,CAAC,CAAA;QACF,OAAO,SAAS,CAAA;IACpB,CAAC;IAED,OAAO,MAAM,CAAC,IAAI,CAAA;AACtB,CAAC"}
|