quantique-field-validator 1.0.1 → 1.0.2-Beta

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.
@@ -0,0 +1,20 @@
1
+ // Default error messages
2
+ const defaultErrorMessages = (rules, type) => {
3
+ return {
4
+ noSpace: "Value should not contain spaces",
5
+ minAge: `User must be ${rules.minAge} years or older`,
6
+ minMinus: "Date is too far in the past",
7
+ maxPlus: "Date is too far in the future",
8
+ required: "This field is required",
9
+ minLength: `Minimum length should be ${rules.minLength}`,
10
+ maxLength: `Maximum length should be ${rules.maxLength}`,
11
+ invalid: `Invalid ${type}`,
12
+ noValidation: `No validation exist for ${type}`,
13
+ custom: "Validation failed",
14
+ Alphanumeric: "value must be alphanumecric",
15
+ customValidation:
16
+ rules.errorMessages?.customValidation || "Custom validation failed",
17
+ };
18
+ };
19
+
20
+ module.exports = defaultErrorMessages;
@@ -0,0 +1,33 @@
1
+ const validateAadhaar = (
2
+ value,
3
+ rules,
4
+ defaultErrorMessages,
5
+ type
6
+
7
+ ) => {
8
+ const defaultRules = {
9
+ regex: /^[2-9]{1}[0-9]{11}$/,
10
+ };
11
+ const effectiveRules = {
12
+ ...defaultRules,
13
+ ...rules,
14
+ };
15
+
16
+
17
+
18
+ if (
19
+ effectiveRules?.regex &&
20
+ !new RegExp(effectiveRules?.regex).test(value)
21
+ ) {
22
+ return {
23
+ isValid: false,
24
+ error:
25
+ effectiveRules?.errorMessages?.invalid ||
26
+ defaultErrorMessages(effectiveRules, type).invalid,
27
+ };
28
+ }
29
+
30
+ return { isValid: true, error: "" };
31
+ };
32
+
33
+ module.exports = validateAadhaar;
@@ -0,0 +1,38 @@
1
+ // Address validation
2
+ const validateAddress = (value, rules, defaultErrorMessages, type) => {
3
+ // Check length constraints
4
+ if (rules?.minLength && value.length < rules?.minLength) {
5
+ return {
6
+ isValid: false,
7
+ error:
8
+ rules?.errorMessages?.minLength ||
9
+ defaultErrorMessages(rules, type).minLength,
10
+ };
11
+ }
12
+
13
+ if (rules?.maxLength && value.length > rules?.maxLength) {
14
+ return {
15
+ isValid: false,
16
+ error:
17
+ rules?.errorMessages?.maxLength ||
18
+ defaultErrorMessages(rules, type).maxLength,
19
+ };
20
+ }
21
+
22
+ // Check against custom regex if provided
23
+ const defaultRegex = /^[a-zA-Z0-9\s,./\-#&:]+$/;
24
+ const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
25
+
26
+ if (!regexToUse.test(value)) {
27
+ return {
28
+ isValid: false,
29
+ error:
30
+ rules?.errorMessages?.invalid ||
31
+ defaultErrorMessages(rules, type).invalid,
32
+ };
33
+ }
34
+
35
+ return { isValid: true, error: "" };
36
+ };
37
+
38
+ module.exports = validateAddress;
@@ -0,0 +1,47 @@
1
+ // Alphanumeric validation
2
+ const validateAlphanumeric = (value, rules, defaultErrorMessages, type) => {
3
+ // Check length constraints if value is a string
4
+ if (typeof value !== "string") {
5
+ return {
6
+ isValid: false,
7
+ error:
8
+ rules?.errorMessages?.invalid ||
9
+ defaultErrorMessages(rules, type).Alphanumeric,
10
+ };
11
+ }
12
+ if (rules?.minLength && value.length < rules?.minLength) {
13
+ return {
14
+ isValid: false,
15
+ error:
16
+ rules?.errorMessages?.minLength ||
17
+ defaultErrorMessages(rules, type).minLength,
18
+ };
19
+ }
20
+
21
+ if (rules?.maxLength && value.length > rules?.maxLength) {
22
+ return {
23
+ isValid: false,
24
+ error:
25
+ rules?.errorMessages?.maxLength ||
26
+ defaultErrorMessages(rules, type).maxLength,
27
+ };
28
+ }
29
+
30
+ // Check against custom regex if provided
31
+ const defaultRegex = /^[a-z0-9]+$/;
32
+ const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
33
+
34
+ if (!regexToUse.test(value)) {
35
+ return {
36
+ isValid: false,
37
+ error:
38
+ rules?.errorMessages?.invalid ||
39
+ defaultErrorMessages(rules, type).Alphanumeric ||
40
+ defaultErrorMessages(rules, type).invalid,
41
+ };
42
+ }
43
+
44
+ return { isValid: true, error: "" };
45
+ };
46
+
47
+ module.exports = validateAlphanumeric;
@@ -0,0 +1,18 @@
1
+ // Custom validation
2
+ const validateCustom = (value, rules, defaultErrorMessages, type) => {
3
+ // Check against custom regex if provided
4
+ const regexToUse = new RegExp(rules.regex);
5
+
6
+ if (!regexToUse.test(value)) {
7
+ return {
8
+ isValid: false,
9
+ error:
10
+ rules?.errorMessages?.invalid ||
11
+ defaultErrorMessages(rules, type).invalid,
12
+ };
13
+ }
14
+
15
+ return { isValid: true, error: "" };
16
+ };
17
+
18
+ module.exports = validateCustom;
@@ -0,0 +1,118 @@
1
+ // Date validation
2
+ const validateDate = (
3
+ value,
4
+ rules = { format: "DD/MM/YYYY" },
5
+ defaultErrorMessages,
6
+ type
7
+ ) => {
8
+ const parseDateByFormat = (value, format) => {
9
+ if (!format) return new Date(value); // fallback to default
10
+
11
+ const separator = format.includes("/") ? "/" : "-";
12
+ const parts = value.split(separator);
13
+ const formatParts = format.split(separator);
14
+
15
+ if (parts.length !== 3 || formatParts.length !== 3) return new Date(NaN);
16
+
17
+ const dateParts = {};
18
+ formatParts.forEach((part, index) => {
19
+ dateParts[part] = parseInt(parts[index], 10);
20
+ });
21
+
22
+ const day = dateParts["DD"];
23
+ const month = (dateParts["MM"] || 1) - 1; // Month is 0-indexed
24
+ const year = dateParts["YYYY"];
25
+
26
+ return new Date(year, month, day);
27
+ };
28
+
29
+ const parsedDate = parseDateByFormat(value, rules?.format);
30
+ const today = new Date();
31
+ today.setHours(0, 0, 0, 0); // Normalize for accurate comparisons
32
+
33
+ if (isNaN(parsedDate.getTime())) {
34
+ return {
35
+ isValid: false,
36
+ error:
37
+ rules?.errorMessages?.invalid ||
38
+ defaultErrorMessages(rules, type)?.invalid ||
39
+ "Invalid date format",
40
+ };
41
+ }
42
+
43
+ // Age check (e.g., user must be 18+)
44
+ if (rules?.minAge) {
45
+ const ageDate = new Date();
46
+ ageDate.setFullYear(ageDate.getFullYear() - rules.minAge);
47
+ if (parsedDate > ageDate) {
48
+ return {
49
+ isValid: false,
50
+ error:
51
+ rules?.errorMessages?.minAge ||
52
+ defaultErrorMessages(rules, type)?.minAge ||
53
+ `You must be at least ${rules.minAge} years old`,
54
+ };
55
+ }
56
+ }
57
+
58
+ // Relative date checks
59
+ if (rules?.minMinus) {
60
+ const minPast = new Date(today);
61
+ minPast.setDate(minPast.getDate() - rules.minMinus);
62
+ if (parsedDate < minPast) {
63
+ return {
64
+ isValid: false,
65
+ error:
66
+ rules?.errorMessages?.minMinus ||
67
+ defaultErrorMessages(rules, type)?.minMinus ||
68
+ `Date should not be earlier than ${rules.minMinus} days ago`,
69
+ };
70
+ }
71
+ }
72
+
73
+ if (rules?.maxPlus) {
74
+ const maxFuture = new Date(today);
75
+ maxFuture.setDate(maxFuture.getDate() + rules.maxPlus);
76
+ if (parsedDate > maxFuture) {
77
+ return {
78
+ isValid: false,
79
+ error:
80
+ rules?.errorMessages?.maxPlus ||
81
+ defaultErrorMessages(rules, type)?.maxPlus ||
82
+ `Date should not be more than ${rules.maxPlus} days in the future`,
83
+ };
84
+ }
85
+ }
86
+
87
+ if (rules?.minPlus) {
88
+ const minFuture = new Date(today);
89
+ minFuture.setDate(minFuture.getDate() + rules.minPlus);
90
+ if (parsedDate < minFuture) {
91
+ return {
92
+ isValid: false,
93
+ error:
94
+ rules?.errorMessages?.minPlus ||
95
+ defaultErrorMessages(rules, type)?.minPlus ||
96
+ `Date should be at least ${rules.minPlus} days in the future`,
97
+ };
98
+ }
99
+ }
100
+
101
+ if (rules?.maxMinus) {
102
+ const maxPast = new Date(today);
103
+ maxPast.setDate(maxPast.getDate() - rules.maxMinus);
104
+ if (parsedDate > maxPast) {
105
+ return {
106
+ isValid: false,
107
+ error:
108
+ rules?.errorMessages?.maxMinus ||
109
+ defaultErrorMessages(rules, type)?.maxMinus ||
110
+ `Date should not be later than ${rules.maxMinus} days ago`,
111
+ };
112
+ }
113
+ }
114
+
115
+ return { isValid: true, error: "" };
116
+ };
117
+
118
+ module.exports = validateDate;
@@ -0,0 +1,38 @@
1
+ // Email validation
2
+ const validateEmail = (value, rules, defaultErrorMessages, type) => {
3
+ // Check length constraints
4
+ if (rules?.minLength && value.length < rules?.minLength) {
5
+ return {
6
+ isValid: false,
7
+ error:
8
+ rules?.errorMessages?.minLength ||
9
+ defaultErrorMessages(rules, type).minLength,
10
+ };
11
+ }
12
+
13
+ if (rules?.maxLength && value.length > rules?.maxLength) {
14
+ return {
15
+ isValid: false,
16
+ error:
17
+ rules?.errorMessages?.maxLength ||
18
+ defaultErrorMessages(rules, type).maxLength,
19
+ };
20
+ }
21
+
22
+ // Check against custom regex if provided
23
+ const defaultRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
24
+ const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
25
+
26
+ if (!regexToUse.test(value)) {
27
+ return {
28
+ isValid: false,
29
+ error:
30
+ rules?.errorMessages?.invalid ||
31
+ defaultErrorMessages(rules, type).invalid,
32
+ };
33
+ }
34
+
35
+ return { isValid: true, error: "" };
36
+ };
37
+
38
+ module.exports = validateEmail;
@@ -0,0 +1,116 @@
1
+ // Mobile validation
2
+ const validateMobile = (value, rules, defaultErrorMessages, type) => {
3
+ // Default regex for Indian mobile number: starts with 6-9 and followed by 9 digits (total 10)
4
+ const defaultRegex = /^[6-9]\d{9}$/;
5
+ const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
6
+
7
+ if (!regexToUse.test(value)) {
8
+ return {
9
+ isValid: false,
10
+ error:
11
+ rules?.errorMessages?.invalid ||
12
+ defaultErrorMessages(rules, type).invalid,
13
+ };
14
+ }
15
+
16
+ return { isValid: true, error: "" };
17
+ };
18
+
19
+ // const validateMobile = (value, rules, defaultErrorMessages, type) => {
20
+ // // Check length constraints
21
+ // if (rules?.minLength && value.length < rules?.minLength) {
22
+ // return {
23
+ // isValid: false,
24
+ // error: rules?.errorMessages?.minLength || defaultErrorMessages(rules, type).minLength,
25
+ // };
26
+ // }
27
+
28
+ // if (rules?.maxLength && value.length > rules?.maxLength) {
29
+ // return {
30
+ // isValid: false,
31
+ // error: rules?.errorMessages?.maxLength || defaultErrorMessages(rules, type).maxLength,
32
+ // };
33
+ // }
34
+
35
+ // // Check against custom regex if provided
36
+ // if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
37
+ // return {
38
+ // isValid: false,
39
+ // error: rules?.errorMessages?.invalid || defaultErrorMessages(rules, type).invalid,
40
+ // };
41
+ // }
42
+
43
+ // // Default mobile regex (accepts numbers, +, -, and spaces)
44
+ // const mobileRegex = /^[\d\s+\-()]*$/;
45
+ // if (!mobileRegex.test(value)) {
46
+ // return {
47
+ // isValid: false,
48
+ // error:
49
+ // rules?.errorMessages?.invalid || "Please enter a valid mobile number",
50
+ // };
51
+ // }
52
+
53
+ // return { isValid: true, error: "" };
54
+ // };
55
+
56
+ // const validateMobile = (value, rules = {}, defaultErrorMessages, type) => {
57
+ // // Check length constraints
58
+ // if (rules.minLength && value.length < rules.minLength) {
59
+ // return {
60
+ // isValid: false,
61
+ // error:
62
+ // rules.errorMessages?.minLength ||
63
+ // defaultErrorMessages(rules, type).minLength,
64
+ // };
65
+ // }
66
+
67
+ // if (rules.maxLength && value.length > rules.maxLength) {
68
+ // return {
69
+ // isValid: false,
70
+ // error:
71
+ // rules.errorMessages?.maxLength ||
72
+ // defaultErrorMessages(rules, type).maxLength,
73
+ // };
74
+ // }
75
+
76
+ // // Custom regex check
77
+ // if (rules.regex && !new RegExp(rules.regex).test(value)) {
78
+ // return {
79
+ // isValid: false,
80
+ // error:
81
+ // rules.errorMessages?.invalid ||
82
+ // defaultErrorMessages(rules, type).invalid,
83
+ // };
84
+ // }
85
+
86
+ // // Validate using libphonenumber-js
87
+ // const phoneNumber = parsePhoneNumberFromString(
88
+ // value,
89
+ // rules?.countryCode || "IN"
90
+ // );
91
+
92
+ // // console.log('phoneNumber', phoneNumber)
93
+ // console.log(
94
+ // "phoneNumber.isValid()",
95
+ // phoneNumber.isValid(),
96
+ // phoneNumber.getType()
97
+ // );
98
+
99
+ // if (
100
+ // !phoneNumber?.isValid() &&
101
+ // (phoneNumber?.getType() !== "MOBILE" ||
102
+ // phoneNumber?.getType() !== "FIXED_LINE_OR_MOBILE")
103
+ // ) {
104
+ // return {
105
+ // isValid: false,
106
+ // error:
107
+ // rules.errorMessages?.invalid ||
108
+ // defaultErrorMessages(rules, type).invalid ||
109
+ // "Please enter a valid mobile number",
110
+ // };
111
+ // }
112
+
113
+ // return { isValid: true, error: "" };
114
+ // };
115
+
116
+ module.exports = validateMobile;
@@ -0,0 +1,59 @@
1
+ // Name validation
2
+ const validateName = (
3
+ value,
4
+ rules = { type: "user" },
5
+ defaultErrorMessages,
6
+ type
7
+ ) => {
8
+ // Check length constraints
9
+ if (rules?.minLength && value?.length < rules?.minLength) {
10
+ return {
11
+ isValid: false,
12
+ error:
13
+ rules?.errorMessages?.minLength ||
14
+ defaultErrorMessages(rules, type).minLength,
15
+ };
16
+ }
17
+
18
+ if (rules?.maxLength && value?.length > rules?.maxLength) {
19
+ return {
20
+ isValid: false,
21
+ error:
22
+ rules?.errorMessages?.maxLength ||
23
+ defaultErrorMessages(rules, type).maxLength,
24
+ };
25
+ }
26
+
27
+ // Default name validation (only letters, spaces, and basic punctuation)
28
+ // Check against custom regex if provided
29
+ if (rules?.type === "user") {
30
+ const defaultRegex = /^[a-zA-ZÀ-ÿ\s'-.]*$/;
31
+ const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
32
+
33
+ if (!regexToUse.test(value)) {
34
+ return {
35
+ isValid: false,
36
+ error:
37
+ rules?.errorMessages?.invalid || "Name contains invalid characters",
38
+ };
39
+ }
40
+ }
41
+
42
+ if (rules?.type === "corporate") {
43
+ const defaultRegex = /^[A-Za-z0-9&.,'()\- ]*$/;
44
+ const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
45
+
46
+ if (!regexToUse.test(value)) {
47
+ return {
48
+ isValid: false,
49
+ error:
50
+ rules?.errorMessages?.invalid ||
51
+ "Corporate Name contains invalid characters",
52
+ };
53
+ }
54
+ }
55
+
56
+ return { isValid: true, error: "" };
57
+ };
58
+
59
+ module.exports = validateName;
@@ -0,0 +1,75 @@
1
+ // Number validation
2
+ const validateNumber = (value, rules, defaultErrorMessages, type) => {
3
+ // Reject if value contains spaces
4
+ if (/\s/.test(value)) {
5
+ return {
6
+ isValid: false,
7
+ error:
8
+ rules?.errorMessages?.invalid ||
9
+ defaultErrorMessages(rules, type).noSpace ||
10
+ defaultErrorMessages(rules, type).invalid,
11
+ };
12
+ }
13
+
14
+ // Default numeric regex (supports optional decimal and negative)
15
+ const defaultNumberRegex = /^-?\d+(\.\d+)?$/;
16
+ const regexToUse = rules?.regex
17
+ ? new RegExp(rules.regex)
18
+ : defaultNumberRegex;
19
+
20
+ if (!regexToUse.test(value)) {
21
+ return {
22
+ isValid: false,
23
+ error:
24
+ rules?.errorMessages?.invalid ||
25
+ defaultErrorMessages(rules, type).invalid ||
26
+ "Please enter a valid number",
27
+ };
28
+ }
29
+
30
+ const numberValue = Number(value);
31
+
32
+ // Length checks (on the string)
33
+ if (rules?.minLength && value.length < rules.minLength) {
34
+ return {
35
+ isValid: false,
36
+ error:
37
+ rules?.errorMessages?.minLength ||
38
+ defaultErrorMessages(rules, type).minLength,
39
+ };
40
+ }
41
+
42
+ if (rules?.maxLength && value.length > rules.maxLength) {
43
+ return {
44
+ isValid: false,
45
+ error:
46
+ rules?.errorMessages?.maxLength ||
47
+ defaultErrorMessages(rules, type).maxLength,
48
+ };
49
+ }
50
+
51
+ // Value range checks
52
+ if (rules?.minValue !== undefined && numberValue < rules.minValue) {
53
+ return {
54
+ isValid: false,
55
+ error:
56
+ rules?.errorMessages?.minValue ||
57
+ defaultErrorMessages(rules, type).minValue ||
58
+ `Value must be at least ${rules.minValue}`,
59
+ };
60
+ }
61
+
62
+ if (rules?.maxValue !== undefined && numberValue > rules.maxValue) {
63
+ return {
64
+ isValid: false,
65
+ error:
66
+ rules?.errorMessages?.maxValue ||
67
+ defaultErrorMessages(rules, type).maxValue ||
68
+ `Value must not exceed ${rules.maxValue}`,
69
+ };
70
+ }
71
+
72
+ return { isValid: true, error: "" };
73
+ };
74
+
75
+ module.exports = validateNumber;
@@ -0,0 +1,34 @@
1
+ const validatePanCard = (
2
+ value,
3
+ rules,
4
+ defaultErrorMessages,
5
+ type
6
+ ) => {
7
+ const defaultRules = {
8
+ regex: /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/,
9
+ };
10
+
11
+
12
+ const effectiveRules = {
13
+ ...defaultRules,
14
+ ...rules,
15
+ };
16
+
17
+
18
+
19
+ if (
20
+ effectiveRules?.regex &&
21
+ !new RegExp(effectiveRules?.regex).test(value)
22
+ ) {
23
+ return {
24
+ isValid: false,
25
+ error:
26
+ effectiveRules?.errorMessages?.invalid ||
27
+ defaultErrorMessages(effectiveRules, type).invalid,
28
+ };
29
+ }
30
+
31
+ return { isValid: true, error: "" };
32
+ };
33
+
34
+ module.exports = validatePanCard;
@@ -0,0 +1,38 @@
1
+ // Add password validation
2
+ const validatePassword = (value, rules, defaultErrorMessages, type) => {
3
+ // Check length constraints
4
+ if (rules?.minLength && value.length < rules?.minLength) {
5
+ return {
6
+ isValid: false,
7
+ error:
8
+ rules?.errorMessages?.minLength ||
9
+ defaultErrorMessages(rules, type).minLength,
10
+ };
11
+ }
12
+
13
+ if (rules?.maxLength && value.length > rules?.maxLength) {
14
+ return {
15
+ isValid: false,
16
+ error:
17
+ rules?.errorMessages?.maxLength ||
18
+ defaultErrorMessages(rules, type).maxLength,
19
+ };
20
+ }
21
+
22
+ // Check against custom regex if provided
23
+ const defaultRegex = /^(?=.*[A-Za-z])(?=.*\d).+$/;
24
+ const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
25
+
26
+ if (!regexToUse.test(value)) {
27
+ return {
28
+ isValid: false,
29
+ error:
30
+ rules?.errorMessages?.invalid ||
31
+ "Password must contain at least one letter and one number",
32
+ };
33
+ }
34
+
35
+ return { isValid: true, error: "" };
36
+ };
37
+
38
+ module.exports = validatePassword;
@@ -0,0 +1,41 @@
1
+ // String validation
2
+ const validateString = (value, rules, defaultErrorMessages, type) => {
3
+ if (typeof value !== "string") {
4
+ return {
5
+ isValid: false,
6
+ error:
7
+ rules?.errorMessages?.invalid ||
8
+ defaultErrorMessages(rules, type).invalid,
9
+ };
10
+ }
11
+ if (rules?.minLength && value.length < rules?.minLength) {
12
+ return {
13
+ isValid: false,
14
+ error:
15
+ rules?.errorMessages?.minLength ||
16
+ defaultErrorMessages(rules, type).minLength,
17
+ };
18
+ }
19
+
20
+ if (rules?.maxLength && value.length > rules?.maxLength) {
21
+ return {
22
+ isValid: false,
23
+ error:
24
+ rules?.errorMessages?.maxLength ||
25
+ defaultErrorMessages(rules, type).maxLength,
26
+ };
27
+ }
28
+
29
+ if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
30
+ return {
31
+ isValid: false,
32
+ error:
33
+ rules?.errorMessages?.invalid ||
34
+ defaultErrorMessages(rules, type).invalid,
35
+ };
36
+ }
37
+
38
+ return { isValid: true, error: "" };
39
+ };
40
+
41
+ module.exports = validateString;
package/index.js CHANGED
@@ -1,5 +1,3 @@
1
- const { parsePhoneNumberFromString } = require("libphonenumber-js/max");
2
-
3
1
  /**
4
2
  * Enhanced dynamic validator with custom function support
5
3
  * @param {string} value - The value to validate
@@ -8,27 +6,34 @@ const { parsePhoneNumberFromString } = require("libphonenumber-js/max");
8
6
  * @param {function} [customValidator] - Optional custom validation function
9
7
  * @returns {object} - Returns { isValid: boolean, error: string }
10
8
  */
11
- const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
12
- // Default error messages
13
- const defaultErrorMessages = {
14
- required: "This field is required",
15
- minLength: `Minimum length should be ${rules.minLength}`,
16
- maxLength: `Maximum length should be ${rules.maxLength}`,
17
- invalid: `Invalid ${type}`,
18
- noValidation: `No validation exist for ${type}`,
19
- custom: "Validation failed",
20
- customValidation:
21
- rules.errorMessages?.customValidation || "Custom validation failed",
22
- };
23
9
 
10
+ const defaultErrorMessages = require("./Messages/defaultErrorMessages");
11
+ const validateString = require("./Validators/validateString");
12
+ const validateAlphanumeric = require("./Validators/validateAlphanumeric");
13
+ const validateNumber = require("./Validators/validateNumber");
14
+ const validateDate = require("./Validators/validateDate");
15
+ const validateName = require("./Validators/validateName");
16
+ const validateEmail = require("./Validators/validateEmail");
17
+ const validateMobile = require("./Validators/validateMobile");
18
+ const validateAddress = require("./Validators/validateAddress");
19
+ const validatePassword = require("./Validators/validatePassword");
20
+ const validateCustom = require("./Validators/validateCustom");
21
+ const validateAadhaar = require("./Validators/validateAadhaar");
22
+ const validatePanCard = require("./Validators/validatePanCard");
23
+
24
+ const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
24
25
  // Trim the value if it's a string
25
- const trimmedValue = typeof value === "string" ? value.trim() : value;
26
+ // const trimmedValue = typeof value === "string" ? value.trim() : value;
27
+
28
+ const trimmedValue = value;
26
29
 
27
30
  // Check if field is required and empty
28
31
  if (rules.required && (!trimmedValue || trimmedValue === "")) {
29
32
  return {
30
33
  isValid: false,
31
- error: rules.errorMessages?.required || defaultErrorMessages.required,
34
+ error:
35
+ rules.errorMessages?.required ||
36
+ defaultErrorMessages(rules, type).required,
32
37
  };
33
38
  }
34
39
 
@@ -47,12 +52,14 @@ const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
47
52
  ) {
48
53
  return {
49
54
  isValid: false,
50
- error: customValidation.error || defaultErrorMessages.customValidation,
55
+ error:
56
+ customValidation.error ||
57
+ defaultErrorMessages(rules, type).customValidation,
51
58
  };
52
59
  } else if (customValidation === false) {
53
60
  return {
54
61
  isValid: false,
55
- error: defaultErrorMessages.customValidation,
62
+ error: defaultErrorMessages(rules, type).customValidation,
56
63
  };
57
64
  }
58
65
  // If custom validator returns true or { isValid: true }, continue with other validations
@@ -68,292 +75,55 @@ const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
68
75
  ) {
69
76
  return {
70
77
  isValid: false,
71
- error: rulesCustomValidation.error || defaultErrorMessages.custom,
78
+ error:
79
+ rulesCustomValidation.error ||
80
+ defaultErrorMessages(rules, type).custom,
72
81
  };
73
82
  } else if (rulesCustomValidation === false) {
74
83
  return {
75
84
  isValid: false,
76
- error: defaultErrorMessages.custom,
85
+ error: defaultErrorMessages(rules, type).custom,
77
86
  };
78
87
  }
79
88
  }
80
89
 
81
90
  // Then validate based on type
82
91
  switch (type) {
92
+ case "string":
93
+ return validateString(trimmedValue, rules, defaultErrorMessages, type);
94
+ case "alphanumeric":
95
+ return validateAlphanumeric(
96
+ trimmedValue,
97
+ rules,
98
+ defaultErrorMessages,
99
+ type
100
+ );
101
+ case "number":
102
+ return validateNumber(trimmedValue, rules, defaultErrorMessages, type);
103
+ case "date":
104
+ return validateDate(trimmedValue, rules, defaultErrorMessages, type);
83
105
  case "name":
84
- return validateName(trimmedValue, rules, defaultErrorMessages);
106
+ return validateName(trimmedValue, rules, defaultErrorMessages, type);
107
+ case "aadhaar":
108
+ return validateAadhaar(trimmedValue, rules, defaultErrorMessages, type);
109
+ case "pan":
110
+ return validatePanCard(trimmedValue, rules, defaultErrorMessages, type);
111
+
85
112
  case "email":
86
- return validateEmail(trimmedValue, rules, defaultErrorMessages);
113
+ return validateEmail(trimmedValue, rules, defaultErrorMessages, type);
87
114
  case "mobile":
88
- return validateMobile(trimmedValue, rules, defaultErrorMessages);
115
+ return validateMobile(trimmedValue, rules, defaultErrorMessages, type);
89
116
  case "address":
90
- return validateAddress(trimmedValue, rules, defaultErrorMessages);
117
+ return validateAddress(trimmedValue, rules, defaultErrorMessages, type);
91
118
  case "password":
92
- return validatePassword(trimmedValue, rules, defaultErrorMessages);
119
+ return validatePassword(trimmedValue, rules, defaultErrorMessages, type);
93
120
  case "custom":
94
- return validateCustom(trimmedValue, rules, defaultErrorMessages);
121
+ return validateCustom(trimmedValue, rules, defaultErrorMessages, type);
95
122
  default:
96
- return { isValid: false, error: defaultErrorMessages?.noValidation };
97
- }
98
- };
99
-
100
- // ... (keep all the existing validateName, validateEmail, etc. functions from previous solution)
101
-
102
- // Name validation
103
- const validateName = (value, rules, defaultErrorMessages) => {
104
- // Check length constraints
105
- if (rules?.minLength && value?.length < rules?.minLength) {
106
- return {
107
- isValid: false,
108
- error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
109
- };
110
- }
111
-
112
- if (rules?.maxLength && value?.length > rules?.maxLength) {
113
- return {
114
- isValid: false,
115
- error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
116
- };
117
- }
118
-
119
- // Check against custom regex if provided
120
- if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
121
- return {
122
- isValid: false,
123
- error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
124
- };
125
- }
126
-
127
- // Default name validation (only letters, spaces, and basic punctuation)
128
- const nameRegex = /^[a-zA-ZÀ-ÿ\s'-.]*$/;
129
- if (!nameRegex.test(value)) {
130
- return {
131
- isValid: false,
132
- error:
133
- rules?.errorMessages?.invalid || "Name contains invalid characters",
134
- };
135
- }
136
-
137
- return { isValid: true, error: "" };
138
- };
139
-
140
- // Email validation
141
- const validateEmail = (value, rules, defaultErrorMessages) => {
142
- // Check length constraints
143
- if (rules?.minLength && value.length < rules?.minLength) {
144
- return {
145
- isValid: false,
146
- error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
147
- };
148
- }
149
-
150
- if (rules?.maxLength && value.length > rules?.maxLength) {
151
- return {
152
- isValid: false,
153
- error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
154
- };
155
- }
156
-
157
- // Check against custom regex if provided
158
- if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
159
- return {
160
- isValid: false,
161
- error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
162
- };
163
- }
164
-
165
- // Default email regex
166
- const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
167
- if (!emailRegex.test(value)) {
168
- return {
169
- isValid: false,
170
- error:
171
- rules?.errorMessages?.invalid || "Please enter a valid email address",
172
- };
173
- }
174
-
175
- return { isValid: true, error: "" };
176
- };
177
-
178
- // Mobile validation
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 = {}) => {
218
- // Check length constraints
219
- if (rules.minLength && value.length < rules.minLength) {
220
- return {
221
- isValid: false,
222
- error: rules.errorMessages?.minLength || defaultErrorMessages.minLength,
223
- };
224
- }
225
-
226
- if (rules.maxLength && value.length > rules.maxLength) {
227
- return {
228
- isValid: false,
229
- error: rules.errorMessages?.maxLength || defaultErrorMessages.maxLength,
230
- };
231
- }
232
-
233
- // Custom regex check
234
- if (rules.regex && !new RegExp(rules.regex).test(value)) {
235
- return {
236
- isValid: false,
237
- error: rules.errorMessages?.invalid || defaultErrorMessages.invalid,
238
- };
239
- }
240
-
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
- ) {
259
- return {
260
- isValid: false,
261
- error:
262
- rules.errorMessages?.invalid ||
263
- defaultErrorMessages.invalid ||
264
- "Please enter a valid mobile number",
265
- };
266
- }
267
-
268
- return { isValid: true, error: "" };
269
- };
270
-
271
- // Address validation
272
- const validateAddress = (value, rules, defaultErrorMessages) => {
273
- // Check length constraints
274
- if (rules?.minLength && value.length < rules?.minLength) {
275
- return {
276
- isValid: false,
277
- error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
278
- };
279
- }
280
-
281
- if (rules?.maxLength && value.length > rules?.maxLength) {
282
- return {
283
- isValid: false,
284
- error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
285
- };
286
- }
287
-
288
- // Check against custom regex if provided
289
- if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
290
- return {
291
- isValid: false,
292
- error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
293
- };
294
- }
295
-
296
- return { isValid: true, error: "" };
297
- };
298
-
299
- // Add password validation
300
- const validatePassword = (value, rules, defaultErrorMessages) => {
301
- // Check length constraints
302
- if (rules?.minLength && value.length < rules?.minLength) {
303
- return {
304
- isValid: false,
305
- error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
306
- };
307
- }
308
-
309
- if (rules?.maxLength && value.length > rules?.maxLength) {
310
- return {
311
- isValid: false,
312
- error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
313
- };
314
- }
315
-
316
- // Check against custom regex if provided
317
- if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
318
- return {
319
- isValid: false,
320
- error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
321
- };
322
- }
323
-
324
- // Default password validation (at least one letter, one number)
325
- const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d).+$/;
326
- if (!passwordRegex.test(value)) {
327
- return {
328
- isValid: false,
329
- error:
330
- rules?.errorMessages?.invalid ||
331
- "Password must contain at least one letter and one number",
332
- };
333
- }
334
-
335
- return { isValid: true, error: "" };
336
- };
337
-
338
- // Custom validation
339
- const validateCustom = (value, rules, defaultErrorMessages) => {
340
- // Check length constraints if value is a string
341
- if (typeof value === "string") {
342
- if (rules?.minLength && value.length < rules?.minLength) {
343
123
  return {
344
124
  isValid: false,
345
- error:
346
- rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
347
- };
348
- }
349
-
350
- if (rules?.maxLength && value.length > rules?.maxLength) {
351
- return {
352
- isValid: false,
353
- error:
354
- rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
125
+ error: defaultErrorMessages(rules, type)?.noValidation,
355
126
  };
356
- }
357
127
  }
358
128
  };
359
129
 
package/package.json CHANGED
@@ -1,25 +1,22 @@
1
- {
2
- "name": "quantique-field-validator",
3
- "version": "1.0.1",
4
- "description": "Validator to verify all form fields.",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/saketsinhaquantique/quantique-field-validator.git"
12
- },
13
- "keywords": [
14
- "QFV"
15
- ],
16
- "author": "Saket Brij Sinha <saket.sinha@quantique.ai>",
17
- "license": "MIT",
18
- "bugs": {
19
- "url": "https://github.com/saketsinhaquantique/quantique-field-validator/issues"
20
- },
21
- "homepage": "https://github.com/saketsinhaquantique/quantique-field-validator#readme",
22
- "dependencies": {
23
- "libphonenumber-js": "^1.12.6"
24
- }
25
- }
1
+ {
2
+ "name": "quantique-field-validator",
3
+ "version": "1.0.2Beta",
4
+ "description": "Validator to verify all form fields.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/saketsinhaquantique/quantique-field-validator.git"
12
+ },
13
+ "keywords": [
14
+ "QFV"
15
+ ],
16
+ "author": "Saket Brij Sinha <saket.sinha@quantique.ai>",
17
+ "license": "MIT",
18
+ "bugs": {
19
+ "url": "https://github.com/saketsinhaquantique/quantique-field-validator/issues"
20
+ },
21
+ "homepage": "https://github.com/saketsinhaquantique/quantique-field-validator#readme"
22
+ }