jcc-express-mvc 1.2.1 → 1.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jcc-express-mvc",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "express mvc structure",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -36,6 +36,24 @@ class Util {
36
36
  this.middlewares = middlewares;
37
37
  }
38
38
 
39
+ /**
40
+ * Checks if the callback is a fuction or an array .
41
+ * @param {function|array} callback - Route callback.
42
+ */
43
+ validateCallback(callback) {
44
+ if (typeof callback === "function") {
45
+ return callback;
46
+ }
47
+ if (Array.isArray(callback) && callback.length === 2) {
48
+ const [controller, method] = callback;
49
+ return controller[method];
50
+ }
51
+
52
+ throw new Error(
53
+ `Callback expect to be a function or an array. but got ${callback} instead`
54
+ );
55
+ }
56
+
39
57
  /**
40
58
  * Resolves the path based on the route prefix and base path.
41
59
  * @param {string} path - The path to resolve.
@@ -117,12 +135,12 @@ class Util {
117
135
  if (this.isMiddleware()) {
118
136
  this.app[httpMethod](this.resolvePath(path), [
119
137
  this.middlewares,
120
- asyncHandler(callback),
138
+ asyncHandler(this.validateCallback(callback)),
121
139
  ]);
122
140
  }
123
141
  return this.app[httpMethod](
124
142
  this.resolvePath(path),
125
- asyncHandler(callback)
143
+ asyncHandler(this.validateCallback(callback))
126
144
  );
127
145
  } catch (error) {
128
146
  throw new Error(error?.message);
@@ -14,6 +14,34 @@ class ValidationMethod {
14
14
  : false;
15
15
  }
16
16
 
17
+ /**
18
+ * Validates if a field is required.
19
+ * @param {string} fieldName - The name of the field.
20
+ * @param {object} value - The value of the field.
21
+ * @returns {string|boolean} - Error message if validation fails, otherwise false.
22
+ */
23
+ object(fieldName, value) {
24
+ return typeof value != "object"
25
+ ? `${capitalize(
26
+ fieldName
27
+ )} expected an object but received a ${typeof value}`
28
+ : false;
29
+ }
30
+
31
+ /**
32
+ * Validates if a field is required.
33
+ * @param {string} fieldName - The name of the field.
34
+ * @param {Array} value - The value of the field.
35
+ * @returns {string|boolean} - Error message if validation fails, otherwise false.
36
+ */
37
+ array(fieldName, value) {
38
+ return !Array.isArray(value)
39
+ ? `${capitalize(
40
+ fieldName
41
+ )} expected an array but received a ${typeof value}`
42
+ : false;
43
+ }
44
+
17
45
  /**
18
46
  * Validates if a field meets a minimum length requirement.
19
47
  * @param {string} fieldName - The name of the field.
@@ -271,6 +299,9 @@ class ValidationMethod {
271
299
  return false;
272
300
  }
273
301
 
302
+ next() {
303
+ return false;
304
+ }
274
305
  /**
275
306
  * Validates if a field is a valid MongoDB ObjectId.
276
307
  * @param {string} fieldName - The name of the field being validated.
@@ -84,8 +84,16 @@ exports.rules = async (validation, fieldName, fieldValue, secondValue) => {
84
84
  case "skip":
85
85
  return ValidationMethod.skip();
86
86
 
87
+ case "next":
88
+ return ValidationMethod.next();
89
+
90
+ case "array":
91
+ return ValidationMethod.array(fieldName, fieldValue);
92
+
93
+ case "object":
94
+ return ValidationMethod.object(fieldName, fieldValue);
95
+
87
96
  default:
88
- // return `${validation}() invalid method`;
89
97
  let err = new Error(
90
98
  "validation method",
91
99
  `${validation}() invalid method`
@@ -94,6 +102,7 @@ exports.rules = async (validation, fieldName, fieldValue, secondValue) => {
94
102
  throw err;
95
103
  }
96
104
  } catch (error) {
105
+ console.log(error?.message);
97
106
  let err = new Error("validation error", error);
98
107
  err.validationMethod = error.message;
99
108
  throw err;