ingeni-validation 1.1.2 → 1.1.4

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.
@@ -42,25 +42,34 @@ async function validateMandate(req, res, requiredFields) {
42
42
  }
43
43
  };
44
44
 
45
- //Optional Field validation
45
+
46
+
47
+ // Optional Field validation
46
48
  async function validateOptional(req, res, optionalFields) {
47
49
  try {
48
- // const missingFields = requiredFields.filter(field => !req.body[field]);
49
-
50
-
51
- const optionMissingFields = optionalFields.filter(
52
- opField => req.body[opField] === undefined
53
- );
54
50
 
55
- if (optionMissingFields.length > 0) {
56
- return {
57
- success: false,
58
- message: `Missing optional fields: ${optionMissingFields.join(", ")}`
59
- };
51
+ for (const field of optionalFields) {
52
+ const value = req.body[field];
53
+
54
+ // check undefined
55
+ if (value === undefined) {
56
+ return {
57
+ success: false,
58
+ message: `Missing optional field: ${field}`
59
+ };
60
+ }
61
+
62
+ // reject real boolean values only
63
+ if (typeof value === "boolean") {
64
+ return {
65
+ success: false,
66
+ message: `Boolean value not allowed for field: ${field}`
67
+ };
68
+ }
69
+
70
+ // "true" and "false" strings are allowed
60
71
  }
61
72
 
62
-
63
- // return true if everything is fine
64
73
  return true;
65
74
 
66
75
  } catch (error) {
@@ -70,7 +79,9 @@ const optionMissingFields = optionalFields.filter(
70
79
  message: "Internal Server Error"
71
80
  };
72
81
  }
73
- };
82
+ }
83
+
84
+
74
85
 
75
86
  //valid boolean
76
87
  async function isValidBoolean(value){
@@ -209,6 +220,33 @@ async function isValidPin(pinCode) {
209
220
  }
210
221
 
211
222
 
223
+ //Valid Date Time (isValidDate(value, "yyyy-mm-dd") , isValidDate(value, "yyyy-mm-dd h:i:s")
224
+ function isValidDate(value, format) {
225
+
226
+ let regex;
227
+
228
+ switch (format.toLowerCase()) {
229
+
230
+ case "yyyy-mm-dd":
231
+ regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
232
+ break;
233
+
234
+ case "yyyy-mm-dd h:i:s":
235
+ regex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/;
236
+ break;
237
+
238
+ default:
239
+ return false;
240
+ }
241
+
242
+ if (!regex.test(value)) {
243
+ return false;
244
+ }
245
+
246
+ const d = new Date(value.replace(" ", "T"));
247
+ return !isNaN(d.getTime());
248
+ }
249
+
212
250
 
213
251
 
214
- export {validateMandate,validateOptional, isValidMobile, isValidEmail, isValidOtp, isValidPin,isValidNumber,isValidBoolean,checkBooleanParams};
252
+ export {validateMandate,validateOptional, isValidMobile, isValidEmail, isValidOtp, isValidPin,isValidNumber,isValidBoolean,checkBooleanParams,isValidDate};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ingeni-validation",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "Ingeni Validation",
5
5
  "license": "ISC",
6
6
  "author": "",