ingeni-validation 1.1.0 → 1.1.2

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.
@@ -1,5 +1,5 @@
1
1
 
2
-
2
+ //Mandatory Field Validation
3
3
  async function validateMandate(req, res, requiredFields) {
4
4
  try {
5
5
  // const missingFields = requiredFields.filter(field => !req.body[field]);
@@ -42,6 +42,7 @@ async function validateMandate(req, res, requiredFields) {
42
42
  }
43
43
  };
44
44
 
45
+ //Optional Field validation
45
46
  async function validateOptional(req, res, optionalFields) {
46
47
  try {
47
48
  // const missingFields = requiredFields.filter(field => !req.body[field]);
@@ -71,7 +72,7 @@ const optionMissingFields = optionalFields.filter(
71
72
  }
72
73
  };
73
74
 
74
-
75
+ //valid boolean
75
76
  async function isValidBoolean(value){
76
77
  if(typeof value==="boolean"){
77
78
  return true;
@@ -83,77 +84,131 @@ const optionMissingFields = optionalFields.filter(
83
84
  //Check Boolean Parameters
84
85
  async function checkBooleanParams(...params) {
85
86
  for (let param of params) {
86
- if (param === "true" || param === "false") {
87
+
88
+ // Reject null or undefined
89
+ if (param === null || param === undefined) {
90
+ return false;
91
+ }
92
+
93
+ // Reject boolean type
94
+ if (typeof param === "boolean") {
95
+ return false;
96
+ }
97
+
98
+ // Reject empty or space-only strings
99
+ if (typeof param === "string" && param.trim().length === 0) {
87
100
  return false;
88
101
  }
102
+
103
+ // Reject boolean strings
104
+ if (typeof param === "string") {
105
+ const val = param.trim().toLowerCase();
106
+ if (val === "true" || val === "false") {
107
+ return false;
108
+ }
109
+ }
89
110
  }
111
+
90
112
  return true;
91
113
  }
92
114
 
93
115
 
94
- //Mobile No. Validation
116
+ // Mobile No. Validation
95
117
  async function isValidMobile(mobile) {
96
-
97
- if (typeof mobile === "boolean" || mobile===null || mobile===undefined) {
118
+
119
+ if (mobile === null || mobile === undefined || typeof mobile === "boolean") {
98
120
  return false;
99
121
  }
100
-
122
+
101
123
  const mobileStr = String(mobile).trim();
102
-
103
- return /^[0-9]{10}$/.test(mobileStr);
104
- }
105
124
 
125
+ if (mobileStr === "") {
126
+ return false;
127
+ }
128
+
129
+ return /^[6-9]\d{9}$/.test(mobileStr);
130
+ }
106
131
 
132
+ // Email Validation
107
133
  async function isValidEmail(email) {
108
- if (typeof email === "boolean" || email===null || email===undefined) {
109
- return false;
110
- }
134
+
135
+ if (email === null || email === undefined || typeof email === "boolean") {
136
+ return false;
137
+ }
111
138
 
112
139
  const emailStr = String(email).trim();
140
+
141
+ if (emailStr === "") {
142
+ return false;
143
+ }
144
+
113
145
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
114
146
 
115
147
  return emailRegex.test(emailStr);
116
148
  }
117
149
 
118
150
 
151
+ //OTP Validation
119
152
 
153
+ async function isValidOtp(otp, otplength) {
120
154
 
155
+ if (otp === null || otp === undefined || typeof otp === "boolean") {
156
+ return false;
157
+ }
158
+
159
+ if (!Number.isInteger(otplength) || otplength <= 0) {
160
+ return false;
161
+ }
121
162
 
122
- async function isValidOtp(otp, otplength) {
123
- if (typeof otp === "boolean" || otp===null || otp===undefined) {
124
- return false;
125
- }
126
-
127
- if (!Number.isInteger(otplength) || otplength <= 0) return false;
128
- //console.log("OTPSTR:", otpStr, otpStr.length);
129
163
  const otpStr = String(otp).trim();
130
-
164
+
165
+ if (otpStr === "") {
166
+ return false;
167
+ }
168
+
131
169
  return new RegExp(`^[0-9]{${otplength}}$`).test(otpStr);
132
170
  }
171
+
172
+ //Valid Number with length
133
173
 
134
174
  async function isValidNumber(num, numlength) {
135
175
 
136
- if (typeof num === "boolean" || num===null || num===undefined) {
137
- return false;
138
- }
139
-
140
- if (!Number.isInteger(numlength) || numlength <= 0) return false;
141
-
176
+ if (num === null || num === undefined || typeof num === "boolean") {
177
+ return false;
178
+ }
179
+
180
+ if (!Number.isInteger(numlength) || numlength <= 0) {
181
+ return false;
182
+ }
183
+
142
184
  const numStr = String(num).trim();
143
- return new RegExp(`^[0-9]{${numlength}}$`).test(numStr);
185
+
186
+ if (numStr === "") {
187
+ return false;
188
+ }
189
+
190
+ return new RegExp(`^\\d{${numlength}}$`).test(numStr);
144
191
  }
145
192
 
193
+
194
+ //Pincode validation
195
+
146
196
  async function isValidPin(pinCode) {
147
- if (typeof pinCode === "boolean" || pinCode===null || pinCode===undefined) {
148
- return false;
149
- }
150
- // convert safely to string
197
+
198
+ if (pinCode === null || pinCode === undefined || typeof pinCode === "boolean") {
199
+ return false;
200
+ }
201
+
151
202
  const pinCodeStr = String(pinCode).trim();
152
203
 
153
- // exactly 6 digits
154
- if (pinCodeStr.length !== 6) return false;
204
+ if (pinCodeStr === "") {
205
+ return false;
206
+ }
155
207
 
156
208
  return /^[0-9]{6}$/.test(pinCodeStr);
157
209
  }
158
210
 
211
+
212
+
213
+
159
214
  export {validateMandate,validateOptional, isValidMobile, isValidEmail, isValidOtp, isValidPin,isValidNumber,isValidBoolean,checkBooleanParams};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ingeni-validation",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Ingeni Validation",
5
5
  "license": "ISC",
6
6
  "author": "",