gg-express 1.0.143 → 1.0.144

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.
@@ -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(req.query.data);
57
+ options.requireParams.data.parse(autoCastNumbers(req.query.data));
58
58
  }
59
59
  else if (method === "post" ||
60
60
  method === "put" ||
61
61
  method === "delete") {
62
- options.requireParams.data.parse(req.body.data.data);
62
+ options.requireParams.data.parse(autoCastNumbers(req.body.data.data));
63
63
  }
64
64
  next();
65
65
  },
@@ -99,3 +99,16 @@ 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 autoCastNumbers(obj) {
103
+ if (Array.isArray(obj)) {
104
+ return obj.map(autoCastNumbers);
105
+ }
106
+ if (obj !== null && typeof obj === "object") {
107
+ return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, autoCastNumbers(v)]));
108
+ }
109
+ // auto convert string number
110
+ if (typeof obj === "string" && obj.trim() !== "" && !isNaN(Number(obj))) {
111
+ return Number(obj);
112
+ }
113
+ return obj;
114
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gg-express",
3
- "version": "1.0.143",
3
+ "version": "1.0.144",
4
4
  "description": "",
5
5
  "main": "dist/main.js",
6
6
  "scripts": {
@@ -112,13 +112,13 @@ export default class GGExpressV3<
112
112
  method === "get" &&
113
113
  isEmptyZodObject(options.requireParams.data) === false
114
114
  ) {
115
- options.requireParams.data.parse(req.query.data)
115
+ options.requireParams.data.parse(autoCastNumbers(req.query.data))
116
116
  } else if (
117
117
  method === "post" ||
118
118
  method === "put" ||
119
119
  method === "delete"
120
120
  ) {
121
- options.requireParams.data.parse(req.body.data.data)
121
+ options.requireParams.data.parse(autoCastNumbers(req.body.data.data))
122
122
  }
123
123
  next()
124
124
  },
@@ -254,3 +254,22 @@ function isEmptyZodObject(
254
254
  ): schema is z.ZodObject<Record<string, never>> {
255
255
  return schema instanceof z.ZodObject && Object.keys(schema.shape).length === 0
256
256
  }
257
+
258
+ function autoCastNumbers(obj: unknown): unknown {
259
+ if (Array.isArray(obj)) {
260
+ return obj.map(autoCastNumbers)
261
+ }
262
+
263
+ if (obj !== null && typeof obj === "object") {
264
+ return Object.fromEntries(
265
+ Object.entries(obj).map(([k, v]) => [k, autoCastNumbers(v)]),
266
+ )
267
+ }
268
+
269
+ // auto convert string number
270
+ if (typeof obj === "string" && obj.trim() !== "" && !isNaN(Number(obj))) {
271
+ return Number(obj)
272
+ }
273
+
274
+ return obj
275
+ }