ingeni-validation 1.1.1 → 1.1.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/ingeni-validation.js +48 -16
- package/package.json +1 -1
package/ingeni-validation.js
CHANGED
|
@@ -42,25 +42,34 @@ async function validateMandate(req, res, requiredFields) {
|
|
|
42
42
|
}
|
|
43
43
|
};
|
|
44
44
|
|
|
45
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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){
|
|
@@ -84,10 +95,31 @@ const optionMissingFields = optionalFields.filter(
|
|
|
84
95
|
//Check Boolean Parameters
|
|
85
96
|
async function checkBooleanParams(...params) {
|
|
86
97
|
for (let param of params) {
|
|
87
|
-
|
|
98
|
+
|
|
99
|
+
// Reject null or undefined
|
|
100
|
+
if (param === null || param === undefined) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Reject boolean type
|
|
105
|
+
if (typeof param === "boolean") {
|
|
88
106
|
return false;
|
|
89
107
|
}
|
|
108
|
+
|
|
109
|
+
// Reject empty or space-only strings
|
|
110
|
+
if (typeof param === "string" && param.trim().length === 0) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Reject boolean strings
|
|
115
|
+
if (typeof param === "string") {
|
|
116
|
+
const val = param.trim().toLowerCase();
|
|
117
|
+
if (val === "true" || val === "false") {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
90
121
|
}
|
|
122
|
+
|
|
91
123
|
return true;
|
|
92
124
|
}
|
|
93
125
|
|