quantique-field-validator 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/index.js +73 -13
  2. package/package.json +5 -2
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ const { parsePhoneNumberFromString } = require("libphonenumber-js/max");
2
+
1
3
  /**
2
4
  * Enhanced dynamic validator with custom function support
3
5
  * @param {string} value - The value to validate
@@ -13,6 +15,7 @@ const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
13
15
  minLength: `Minimum length should be ${rules.minLength}`,
14
16
  maxLength: `Maximum length should be ${rules.maxLength}`,
15
17
  invalid: `Invalid ${type}`,
18
+ noValidation: `No validation exist for ${type}`,
16
19
  custom: "Validation failed",
17
20
  customValidation:
18
21
  rules.errorMessages?.customValidation || "Custom validation failed",
@@ -87,8 +90,10 @@ const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
87
90
  return validateAddress(trimmedValue, rules, defaultErrorMessages);
88
91
  case "password":
89
92
  return validatePassword(trimmedValue, rules, defaultErrorMessages);
90
- default:
93
+ case "custom":
91
94
  return validateCustom(trimmedValue, rules, defaultErrorMessages);
95
+ default:
96
+ return { isValid: false, error: defaultErrorMessages?.noValidation };
92
97
  }
93
98
  };
94
99
 
@@ -171,37 +176,92 @@ const validateEmail = (value, rules, defaultErrorMessages) => {
171
176
  };
172
177
 
173
178
  // Mobile validation
174
- const validateMobile = (value, rules, defaultErrorMessages) => {
179
+
180
+ // const validateMobile = (value, rules, defaultErrorMessages) => {
181
+ // // Check length constraints
182
+ // if (rules?.minLength && value.length < rules?.minLength) {
183
+ // return {
184
+ // isValid: false,
185
+ // error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
186
+ // };
187
+ // }
188
+
189
+ // if (rules?.maxLength && value.length > rules?.maxLength) {
190
+ // return {
191
+ // isValid: false,
192
+ // error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
193
+ // };
194
+ // }
195
+
196
+ // // Check against custom regex if provided
197
+ // if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
198
+ // return {
199
+ // isValid: false,
200
+ // error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
201
+ // };
202
+ // }
203
+
204
+ // // Default mobile regex (accepts numbers, +, -, and spaces)
205
+ // const mobileRegex = /^[\d\s+\-()]*$/;
206
+ // if (!mobileRegex.test(value)) {
207
+ // return {
208
+ // isValid: false,
209
+ // error:
210
+ // rules?.errorMessages?.invalid || "Please enter a valid mobile number",
211
+ // };
212
+ // }
213
+
214
+ // return { isValid: true, error: "" };
215
+ // };
216
+
217
+ const validateMobile = (value, rules = {}, defaultErrorMessages = {}) => {
175
218
  // Check length constraints
176
- if (rules?.minLength && value.length < rules?.minLength) {
219
+ if (rules.minLength && value.length < rules.minLength) {
177
220
  return {
178
221
  isValid: false,
179
- error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
222
+ error: rules.errorMessages?.minLength || defaultErrorMessages.minLength,
180
223
  };
181
224
  }
182
225
 
183
- if (rules?.maxLength && value.length > rules?.maxLength) {
226
+ if (rules.maxLength && value.length > rules.maxLength) {
184
227
  return {
185
228
  isValid: false,
186
- error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
229
+ error: rules.errorMessages?.maxLength || defaultErrorMessages.maxLength,
187
230
  };
188
231
  }
189
232
 
190
- // Check against custom regex if provided
191
- if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
233
+ // Custom regex check
234
+ if (rules.regex && !new RegExp(rules.regex).test(value)) {
192
235
  return {
193
236
  isValid: false,
194
- error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
237
+ error: rules.errorMessages?.invalid || defaultErrorMessages.invalid,
195
238
  };
196
239
  }
197
240
 
198
- // Default mobile regex (accepts numbers, +, -, and spaces)
199
- const mobileRegex = /^[\d\s+\-()]*$/;
200
- if (!mobileRegex.test(value)) {
241
+ // Validate using libphonenumber-js
242
+ const phoneNumber = parsePhoneNumberFromString(
243
+ value,
244
+ rules?.countryCode || "IN"
245
+ );
246
+
247
+ // console.log('phoneNumber', phoneNumber)
248
+ console.log(
249
+ "phoneNumber.isValid()",
250
+ phoneNumber.isValid(),
251
+ phoneNumber.getType()
252
+ );
253
+
254
+ if (
255
+ !phoneNumber?.isValid() &&
256
+ (phoneNumber?.getType() !== "MOBILE" ||
257
+ phoneNumber?.getType() !== "FIXED_LINE_OR_MOBILE")
258
+ ) {
201
259
  return {
202
260
  isValid: false,
203
261
  error:
204
- rules?.errorMessages?.invalid || "Please enter a valid mobile number",
262
+ rules.errorMessages?.invalid ||
263
+ defaultErrorMessages.invalid ||
264
+ "Please enter a valid mobile number",
205
265
  };
206
266
  }
207
267
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quantique-field-validator",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Validator to verify all form fields.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -18,5 +18,8 @@
18
18
  "bugs": {
19
19
  "url": "https://github.com/saketsinhaquantique/quantique-field-validator/issues"
20
20
  },
21
- "homepage": "https://github.com/saketsinhaquantique/quantique-field-validator#readme"
21
+ "homepage": "https://github.com/saketsinhaquantique/quantique-field-validator#readme",
22
+ "dependencies": {
23
+ "libphonenumber-js": "^1.12.6"
24
+ }
22
25
  }