ingeni-validation 1.0.9 → 1.1.1
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 +68 -20
- package/package.json +1 -1
package/ingeni-validation.js
CHANGED
|
@@ -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;
|
|
@@ -91,55 +92,102 @@ async function checkBooleanParams(...params) {
|
|
|
91
92
|
}
|
|
92
93
|
|
|
93
94
|
|
|
94
|
-
|
|
95
|
+
// Mobile No. Validation
|
|
95
96
|
async function isValidMobile(mobile) {
|
|
96
97
|
|
|
98
|
+
if (mobile === null || mobile === undefined || typeof mobile === "boolean") {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
|
|
97
102
|
const mobileStr = String(mobile).trim();
|
|
98
|
-
return /^[0-9]{10}$/.test(mobileStr);
|
|
99
|
-
}
|
|
100
103
|
|
|
104
|
+
if (mobileStr === "") {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return /^[6-9]\d{9}$/.test(mobileStr);
|
|
109
|
+
}
|
|
101
110
|
|
|
111
|
+
// Email Validation
|
|
102
112
|
async function isValidEmail(email) {
|
|
103
113
|
|
|
114
|
+
if (email === null || email === undefined || typeof email === "boolean") {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
104
118
|
const emailStr = String(email).trim();
|
|
119
|
+
|
|
120
|
+
if (emailStr === "") {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
105
124
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
106
125
|
|
|
107
126
|
return emailRegex.test(emailStr);
|
|
108
127
|
}
|
|
109
128
|
|
|
110
129
|
|
|
130
|
+
//OTP Validation
|
|
111
131
|
|
|
132
|
+
async function isValidOtp(otp, otplength) {
|
|
112
133
|
|
|
134
|
+
if (otp === null || otp === undefined || typeof otp === "boolean") {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (!Number.isInteger(otplength) || otplength <= 0) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
113
141
|
|
|
114
|
-
async function isValidOtp(otp, otplength) {
|
|
115
|
-
if (otp === undefined || otp === null) return false;
|
|
116
|
-
|
|
117
|
-
if (!Number.isInteger(otplength) || otplength <= 0) return false;
|
|
118
|
-
//console.log("OTPSTR:", otpStr, otpStr.length);
|
|
119
142
|
const otpStr = String(otp).trim();
|
|
120
|
-
|
|
143
|
+
|
|
144
|
+
if (otpStr === "") {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
|
|
121
148
|
return new RegExp(`^[0-9]{${otplength}}$`).test(otpStr);
|
|
122
149
|
}
|
|
150
|
+
|
|
151
|
+
//Valid Number with length
|
|
123
152
|
|
|
124
153
|
async function isValidNumber(num, numlength) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
154
|
+
|
|
155
|
+
if (num === null || num === undefined || typeof num === "boolean") {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (!Number.isInteger(numlength) || numlength <= 0) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
|
|
129
163
|
const numStr = String(num).trim();
|
|
130
|
-
|
|
164
|
+
|
|
165
|
+
if (numStr === "") {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return new RegExp(`^\\d{${numlength}}$`).test(numStr);
|
|
131
170
|
}
|
|
132
171
|
|
|
172
|
+
|
|
173
|
+
//Pincode validation
|
|
174
|
+
|
|
133
175
|
async function isValidPin(pinCode) {
|
|
134
|
-
if (pinCode === undefined || pinCode === null) return false;
|
|
135
176
|
|
|
136
|
-
|
|
177
|
+
if (pinCode === null || pinCode === undefined || typeof pinCode === "boolean") {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
|
|
137
181
|
const pinCodeStr = String(pinCode).trim();
|
|
138
182
|
|
|
139
|
-
|
|
140
|
-
|
|
183
|
+
if (pinCodeStr === "") {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
141
186
|
|
|
142
187
|
return /^[0-9]{6}$/.test(pinCodeStr);
|
|
143
188
|
}
|
|
144
189
|
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
145
193
|
export {validateMandate,validateOptional, isValidMobile, isValidEmail, isValidOtp, isValidPin,isValidNumber,isValidBoolean,checkBooleanParams};
|