express-zod-safe 1.3.2 → 1.4.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/dist/index.js +38 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
const express_1 = __importDefault(require("express"));
|
|
2
15
|
const zod_1 = require("zod");
|
|
3
16
|
const types = ['query', 'params', 'body'];
|
|
4
17
|
const emptyObjectSchema = zod_1.z.object({}).strict();
|
|
@@ -8,7 +21,24 @@ const emptyObjectSchema = zod_1.z.object({}).strict();
|
|
|
8
21
|
* @returns Whether the provided schema is a ZodSchema.
|
|
9
22
|
*/
|
|
10
23
|
function isZodSchema(schema) {
|
|
11
|
-
return !!schema && typeof schema.
|
|
24
|
+
return !!schema && typeof schema.safeParseAsync === 'function';
|
|
25
|
+
}
|
|
26
|
+
// Override express@^5 request.query getter to provider setter
|
|
27
|
+
const descriptor = Object.getOwnPropertyDescriptor(express_1.default.request, 'query');
|
|
28
|
+
if (descriptor) {
|
|
29
|
+
Object.defineProperty(express_1.default.request, 'query', {
|
|
30
|
+
get() {
|
|
31
|
+
var _a;
|
|
32
|
+
if (this._query)
|
|
33
|
+
return this._query;
|
|
34
|
+
return (_a = descriptor === null || descriptor === void 0 ? void 0 : descriptor.get) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
35
|
+
},
|
|
36
|
+
set(query) {
|
|
37
|
+
this._query = query;
|
|
38
|
+
},
|
|
39
|
+
configurable: true,
|
|
40
|
+
enumerable: true
|
|
41
|
+
});
|
|
12
42
|
}
|
|
13
43
|
/**
|
|
14
44
|
* Generates a middleware function for Express.js that validates request params, query, and body.
|
|
@@ -56,20 +86,16 @@ function validate(schemas) {
|
|
|
56
86
|
query: isZodSchema(schemas.query) ? schemas.query : zod_1.z.object((_b = schemas.query) !== null && _b !== void 0 ? _b : {}).strict(),
|
|
57
87
|
body: isZodSchema(schemas.body) ? schemas.body : zod_1.z.object((_c = schemas.body) !== null && _c !== void 0 ? _c : {}).strict()
|
|
58
88
|
};
|
|
59
|
-
return (req, res, next) => {
|
|
60
|
-
var _a
|
|
89
|
+
return (req, res, next) => __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
var _a;
|
|
61
91
|
const errors = [];
|
|
62
92
|
// Validate all types (params, query, body)
|
|
63
93
|
for (const type of types) {
|
|
64
|
-
const parsed = validation[type].
|
|
65
|
-
if (parsed.success)
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
req[type] = parsed.data;
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
94
|
+
const parsed = yield validation[type].safeParseAsync((_a = req[type]) !== null && _a !== void 0 ? _a : {});
|
|
95
|
+
if (parsed.success)
|
|
96
|
+
req[type] = parsed.data;
|
|
97
|
+
else
|
|
71
98
|
errors.push({ type, errors: parsed.error });
|
|
72
|
-
}
|
|
73
99
|
}
|
|
74
100
|
// Return all errors if there are any
|
|
75
101
|
if (errors.length > 0) {
|
|
@@ -80,6 +106,6 @@ function validate(schemas) {
|
|
|
80
106
|
return;
|
|
81
107
|
}
|
|
82
108
|
return next();
|
|
83
|
-
};
|
|
109
|
+
});
|
|
84
110
|
}
|
|
85
111
|
module.exports = validate;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-zod-safe",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
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": {
|