gg-express 1.0.144 → 1.0.146
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/v3/GGExpressV3.js +37 -11
- package/package.json +1 -1
- package/src/v3/GGExpressV3.ts +50 -13
package/dist/v3/GGExpressV3.js
CHANGED
|
@@ -54,12 +54,12 @@ class GGExpressV3 {
|
|
|
54
54
|
// if (req.body) console.log("req.body", req.body)
|
|
55
55
|
if (method === "get" &&
|
|
56
56
|
isEmptyZodObject(options.requireParams.data) === false) {
|
|
57
|
-
options.requireParams.data.parse(
|
|
57
|
+
options.requireParams.data.parse(autoCastBySchema(req.query.data, options.requireParams.data));
|
|
58
58
|
}
|
|
59
59
|
else if (method === "post" ||
|
|
60
60
|
method === "put" ||
|
|
61
61
|
method === "delete") {
|
|
62
|
-
options.requireParams.data.parse(
|
|
62
|
+
options.requireParams.data.parse(autoCastBySchema(req.body.data.data, options.requireParams.data));
|
|
63
63
|
}
|
|
64
64
|
next();
|
|
65
65
|
},
|
|
@@ -99,16 +99,42 @@ exports.default = GGExpressV3;
|
|
|
99
99
|
function isEmptyZodObject(schema) {
|
|
100
100
|
return schema instanceof zod_1.default.ZodObject && Object.keys(schema.shape).length === 0;
|
|
101
101
|
}
|
|
102
|
-
function
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
function autoCastBySchema(value, schema) {
|
|
103
|
+
const typeName = schema._def.typeName;
|
|
104
|
+
// 🔥 STRING
|
|
105
|
+
if (typeName === "ZodString") {
|
|
106
|
+
if (typeof value === "number") {
|
|
107
|
+
return String(value);
|
|
108
|
+
}
|
|
109
|
+
return value;
|
|
110
|
+
}
|
|
111
|
+
// 🔥 NUMBER
|
|
112
|
+
if (typeName === "ZodNumber") {
|
|
113
|
+
if (typeof value === "string" &&
|
|
114
|
+
value.trim() !== "" &&
|
|
115
|
+
!isNaN(Number(value))) {
|
|
116
|
+
return Number(value);
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
105
119
|
}
|
|
106
|
-
|
|
107
|
-
|
|
120
|
+
// 🔥 OBJECT (recursive)
|
|
121
|
+
if (typeName === "ZodObject") {
|
|
122
|
+
const shape = schema.shape;
|
|
123
|
+
if (typeof value !== "object" || value === null) {
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
const result = {};
|
|
127
|
+
for (const key in shape) {
|
|
128
|
+
result[key] = autoCastBySchema(value[key], shape[key]);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
108
131
|
}
|
|
109
|
-
//
|
|
110
|
-
if (
|
|
111
|
-
|
|
132
|
+
// 🔥 ARRAY
|
|
133
|
+
if (typeName === "ZodArray") {
|
|
134
|
+
const itemSchema = schema._def.type;
|
|
135
|
+
if (!Array.isArray(value))
|
|
136
|
+
return value;
|
|
137
|
+
return value.map((v) => autoCastBySchema(v, itemSchema));
|
|
112
138
|
}
|
|
113
|
-
return
|
|
139
|
+
return value;
|
|
114
140
|
}
|
package/package.json
CHANGED
package/src/v3/GGExpressV3.ts
CHANGED
|
@@ -112,13 +112,17 @@ export default class GGExpressV3<
|
|
|
112
112
|
method === "get" &&
|
|
113
113
|
isEmptyZodObject(options.requireParams.data) === false
|
|
114
114
|
) {
|
|
115
|
-
options.requireParams.data.parse(
|
|
115
|
+
options.requireParams.data.parse(
|
|
116
|
+
autoCastBySchema(req.query.data, options.requireParams.data),
|
|
117
|
+
)
|
|
116
118
|
} else if (
|
|
117
119
|
method === "post" ||
|
|
118
120
|
method === "put" ||
|
|
119
121
|
method === "delete"
|
|
120
122
|
) {
|
|
121
|
-
options.requireParams.data.parse(
|
|
123
|
+
options.requireParams.data.parse(
|
|
124
|
+
autoCastBySchema(req.body.data.data, options.requireParams.data),
|
|
125
|
+
)
|
|
122
126
|
}
|
|
123
127
|
next()
|
|
124
128
|
},
|
|
@@ -255,21 +259,54 @@ function isEmptyZodObject(
|
|
|
255
259
|
return schema instanceof z.ZodObject && Object.keys(schema.shape).length === 0
|
|
256
260
|
}
|
|
257
261
|
|
|
258
|
-
function
|
|
259
|
-
|
|
260
|
-
|
|
262
|
+
function autoCastBySchema(value: any, schema: z.ZodTypeAny): any {
|
|
263
|
+
const typeName = (schema as any)._def.typeName
|
|
264
|
+
|
|
265
|
+
// 🔥 STRING
|
|
266
|
+
if (typeName === "ZodString") {
|
|
267
|
+
if (typeof value === "number") {
|
|
268
|
+
return String(value)
|
|
269
|
+
}
|
|
270
|
+
return value
|
|
261
271
|
}
|
|
262
272
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
273
|
+
// 🔥 NUMBER
|
|
274
|
+
if (typeName === "ZodNumber") {
|
|
275
|
+
if (
|
|
276
|
+
typeof value === "string" &&
|
|
277
|
+
value.trim() !== "" &&
|
|
278
|
+
!isNaN(Number(value))
|
|
279
|
+
) {
|
|
280
|
+
return Number(value)
|
|
281
|
+
}
|
|
282
|
+
return value
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// 🔥 OBJECT (recursive)
|
|
286
|
+
if (typeName === "ZodObject") {
|
|
287
|
+
const shape = (schema as any).shape
|
|
288
|
+
|
|
289
|
+
if (typeof value !== "object" || value === null) {
|
|
290
|
+
return value
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const result: any = {}
|
|
294
|
+
|
|
295
|
+
for (const key in shape) {
|
|
296
|
+
result[key] = autoCastBySchema(value[key], shape[key])
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return result
|
|
267
300
|
}
|
|
268
301
|
|
|
269
|
-
//
|
|
270
|
-
if (
|
|
271
|
-
|
|
302
|
+
// 🔥 ARRAY
|
|
303
|
+
if (typeName === "ZodArray") {
|
|
304
|
+
const itemSchema = (schema as any)._def.type
|
|
305
|
+
|
|
306
|
+
if (!Array.isArray(value)) return value
|
|
307
|
+
|
|
308
|
+
return value.map((v) => autoCastBySchema(v, itemSchema))
|
|
272
309
|
}
|
|
273
310
|
|
|
274
|
-
return
|
|
311
|
+
return value
|
|
275
312
|
}
|