express-zod-safe 1.3.2 → 1.3.3

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.
Files changed (2) hide show
  1. package/dist/index.js +25 -8
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,4 +1,8 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const express_1 = __importDefault(require("express"));
2
6
  const zod_1 = require("zod");
3
7
  const types = ['query', 'params', 'body'];
4
8
  const emptyObjectSchema = zod_1.z.object({}).strict();
@@ -10,6 +14,23 @@ const emptyObjectSchema = zod_1.z.object({}).strict();
10
14
  function isZodSchema(schema) {
11
15
  return !!schema && typeof schema.safeParse === 'function';
12
16
  }
17
+ // Override express@^5 request.query getter to provider setter
18
+ const descriptor = Object.getOwnPropertyDescriptor(express_1.default.request, 'query');
19
+ if (descriptor) {
20
+ Object.defineProperty(express_1.default.request, 'query', {
21
+ get() {
22
+ var _a;
23
+ if (this._query)
24
+ return this._query;
25
+ return (_a = descriptor === null || descriptor === void 0 ? void 0 : descriptor.get) === null || _a === void 0 ? void 0 : _a.call(this);
26
+ },
27
+ set(query) {
28
+ this._query = query;
29
+ },
30
+ configurable: true,
31
+ enumerable: true
32
+ });
33
+ }
13
34
  /**
14
35
  * Generates a middleware function for Express.js that validates request params, query, and body.
15
36
  * This function uses Zod schemas to perform validation against the provided schema definitions.
@@ -57,19 +78,15 @@ function validate(schemas) {
57
78
  body: isZodSchema(schemas.body) ? schemas.body : zod_1.z.object((_c = schemas.body) !== null && _c !== void 0 ? _c : {}).strict()
58
79
  };
59
80
  return (req, res, next) => {
60
- var _a, _b;
81
+ var _a;
61
82
  const errors = [];
62
83
  // Validate all types (params, query, body)
63
84
  for (const type of types) {
64
85
  const parsed = validation[type].safeParse((_a = req[type]) !== null && _a !== void 0 ? _a : {});
65
- if (parsed.success) {
66
- const writable = (_b = Object.getOwnPropertyDescriptor(req, type)) === null || _b === void 0 ? void 0 : _b.writable;
67
- if (writable)
68
- req[type] = parsed.data;
69
- }
70
- else {
86
+ if (parsed.success)
87
+ req[type] = parsed.data;
88
+ else
71
89
  errors.push({ type, errors: parsed.error });
72
- }
73
90
  }
74
91
  // Return all errors if there are any
75
92
  if (errors.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-zod-safe",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "description": "TypeScript-friendly middleware designed for Express applications, leveraging the robustness of Zod schemas to validate incoming request bodies, parameters, and queries.",
5
5
  "main": "dist/index.js",
6
6
  "repository": {