quantique-field-validator 1.0.6 → 1.0.9
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/.github/workflows/publish.yml +31 -0
- package/Messages/defaultErrorMessages.js +27 -26
- package/Validators/ValidateChassisNumber.js +37 -37
- package/Validators/validateAadhaar.js +18 -22
- package/Validators/validateAddress.js +38 -38
- package/Validators/validateAlphanumeric.js +47 -47
- package/Validators/validateBankIFSC.js +41 -41
- package/Validators/validateCustom.js +18 -18
- package/Validators/validateDate.js +166 -166
- package/Validators/validateEmail.js +39 -39
- package/Validators/validateEngineNumber.js +37 -37
- package/Validators/validateFirstMiddleLastName.js +36 -36
- package/Validators/validateFloatNumber.js +108 -108
- package/Validators/validateGST.js +19 -19
- package/Validators/validateIPAddress.js +41 -41
- package/Validators/validateMobile.js +116 -116
- package/Validators/validateName.js +67 -67
- package/Validators/validateNumber.js +75 -75
- package/Validators/validatePanCard.js +23 -23
- package/Validators/validatePassword.js +38 -38
- package/Validators/validateRTO.js +41 -41
- package/Validators/validateString.js +41 -41
- package/index.js +194 -180
- package/package.json +22 -22
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
// Float Number Validation with Precision Check
|
|
2
|
-
|
|
3
|
-
// const rules = {
|
|
4
|
-
// decimalPrecision: 2, // allow only 2 digits after decimal
|
|
5
|
-
// maxWholeDigits: 6, // allow up to 6 digits before decimal
|
|
6
|
-
// minValue: 0.01,
|
|
7
|
-
// maxValue: 999999.99,
|
|
8
|
-
// };
|
|
9
|
-
|
|
10
|
-
const validateFloatNumber = (value, rules, defaultErrorMessages, type) => {
|
|
11
|
-
// Reject if value contains spaces
|
|
12
|
-
if (/\s/.test(value)) {
|
|
13
|
-
return {
|
|
14
|
-
isValid: false,
|
|
15
|
-
error:
|
|
16
|
-
rules?.errorMessages?.invalid ||
|
|
17
|
-
defaultErrorMessages(rules, type).noSpace ||
|
|
18
|
-
defaultErrorMessages(rules, type).invalid,
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// Default float regex (supports optional decimal)
|
|
23
|
-
const defaultFloatRegex = /^\d+(\.\d+)?$/;
|
|
24
|
-
const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultFloatRegex;
|
|
25
|
-
|
|
26
|
-
if (!regexToUse.test(value)) {
|
|
27
|
-
return {
|
|
28
|
-
isValid: false,
|
|
29
|
-
error:
|
|
30
|
-
rules?.errorMessages?.invalid ||
|
|
31
|
-
defaultErrorMessages(rules, type).invalid ||
|
|
32
|
-
"Please enter a valid amount (e.g., 123.45)",
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const numberValue = Number(value);
|
|
37
|
-
|
|
38
|
-
// Decimal precision validation
|
|
39
|
-
if (rules?.decimalPrecision !== undefined) {
|
|
40
|
-
const decimalPart = value.split(".")[1];
|
|
41
|
-
const actualPrecision = decimalPart ? decimalPart.length : 0;
|
|
42
|
-
if (actualPrecision > rules.decimalPrecision) {
|
|
43
|
-
return {
|
|
44
|
-
isValid: false,
|
|
45
|
-
error:
|
|
46
|
-
rules?.errorMessages?.decimalPrecision ||
|
|
47
|
-
`Only up to ${rules.decimalPrecision} digits allowed after decimal.`,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Whole number digit validation
|
|
53
|
-
if (rules?.maxWholeDigits !== undefined) {
|
|
54
|
-
const wholePart = value.split(".")[0];
|
|
55
|
-
if (wholePart.length > rules.maxWholeDigits) {
|
|
56
|
-
return {
|
|
57
|
-
isValid: false,
|
|
58
|
-
error:
|
|
59
|
-
rules?.errorMessages?.maxWholeDigits ||
|
|
60
|
-
`Only up to ${rules.maxWholeDigits} digits allowed before decimal.`,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Length checks (on the full string)
|
|
66
|
-
if (rules?.minLength && value.length < rules.minLength) {
|
|
67
|
-
return {
|
|
68
|
-
isValid: false,
|
|
69
|
-
error:
|
|
70
|
-
rules?.errorMessages?.minLength ||
|
|
71
|
-
defaultErrorMessages(rules, type).minLength,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (rules?.maxLength && value.length > rules.maxLength) {
|
|
76
|
-
return {
|
|
77
|
-
isValid: false,
|
|
78
|
-
error:
|
|
79
|
-
rules?.errorMessages?.maxLength ||
|
|
80
|
-
defaultErrorMessages(rules, type).maxLength,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Value range checks
|
|
85
|
-
if (rules?.minValue !== undefined && numberValue < rules.minValue) {
|
|
86
|
-
return {
|
|
87
|
-
isValid: false,
|
|
88
|
-
error:
|
|
89
|
-
rules?.errorMessages?.minValue ||
|
|
90
|
-
defaultErrorMessages(rules, type).minValue ||
|
|
91
|
-
`Amount must be at least ${rules.minValue}`,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (rules?.maxValue !== undefined && numberValue > rules.maxValue) {
|
|
96
|
-
return {
|
|
97
|
-
isValid: false,
|
|
98
|
-
error:
|
|
99
|
-
rules?.errorMessages?.maxValue ||
|
|
100
|
-
defaultErrorMessages(rules, type).maxValue ||
|
|
101
|
-
`Amount must not exceed ${rules.maxValue}`,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return { isValid: true, error: "" };
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
module.exports = validateFloatNumber;
|
|
1
|
+
// Float Number Validation with Precision Check
|
|
2
|
+
|
|
3
|
+
// const rules = {
|
|
4
|
+
// decimalPrecision: 2, // allow only 2 digits after decimal
|
|
5
|
+
// maxWholeDigits: 6, // allow up to 6 digits before decimal
|
|
6
|
+
// minValue: 0.01,
|
|
7
|
+
// maxValue: 999999.99,
|
|
8
|
+
// };
|
|
9
|
+
|
|
10
|
+
const validateFloatNumber = (value, rules, defaultErrorMessages, type) => {
|
|
11
|
+
// Reject if value contains spaces
|
|
12
|
+
if (/\s/.test(value)) {
|
|
13
|
+
return {
|
|
14
|
+
isValid: false,
|
|
15
|
+
error:
|
|
16
|
+
rules?.errorMessages?.invalid ||
|
|
17
|
+
defaultErrorMessages(rules, type).noSpace ||
|
|
18
|
+
defaultErrorMessages(rules, type).invalid,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Default float regex (supports optional decimal)
|
|
23
|
+
const defaultFloatRegex = /^\d+(\.\d+)?$/;
|
|
24
|
+
const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultFloatRegex;
|
|
25
|
+
|
|
26
|
+
if (!regexToUse.test(value)) {
|
|
27
|
+
return {
|
|
28
|
+
isValid: false,
|
|
29
|
+
error:
|
|
30
|
+
rules?.errorMessages?.invalid ||
|
|
31
|
+
defaultErrorMessages(rules, type).invalid ||
|
|
32
|
+
"Please enter a valid amount (e.g., 123.45)",
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const numberValue = Number(value);
|
|
37
|
+
|
|
38
|
+
// Decimal precision validation
|
|
39
|
+
if (rules?.decimalPrecision !== undefined) {
|
|
40
|
+
const decimalPart = value.split(".")[1];
|
|
41
|
+
const actualPrecision = decimalPart ? decimalPart.length : 0;
|
|
42
|
+
if (actualPrecision > rules.decimalPrecision) {
|
|
43
|
+
return {
|
|
44
|
+
isValid: false,
|
|
45
|
+
error:
|
|
46
|
+
rules?.errorMessages?.decimalPrecision ||
|
|
47
|
+
`Only up to ${rules.decimalPrecision} digits allowed after decimal.`,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Whole number digit validation
|
|
53
|
+
if (rules?.maxWholeDigits !== undefined) {
|
|
54
|
+
const wholePart = value.split(".")[0];
|
|
55
|
+
if (wholePart.length > rules.maxWholeDigits) {
|
|
56
|
+
return {
|
|
57
|
+
isValid: false,
|
|
58
|
+
error:
|
|
59
|
+
rules?.errorMessages?.maxWholeDigits ||
|
|
60
|
+
`Only up to ${rules.maxWholeDigits} digits allowed before decimal.`,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Length checks (on the full string)
|
|
66
|
+
if (rules?.minLength && value.length < rules.minLength) {
|
|
67
|
+
return {
|
|
68
|
+
isValid: false,
|
|
69
|
+
error:
|
|
70
|
+
rules?.errorMessages?.minLength ||
|
|
71
|
+
defaultErrorMessages(rules, type).minLength,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (rules?.maxLength && value.length > rules.maxLength) {
|
|
76
|
+
return {
|
|
77
|
+
isValid: false,
|
|
78
|
+
error:
|
|
79
|
+
rules?.errorMessages?.maxLength ||
|
|
80
|
+
defaultErrorMessages(rules, type).maxLength,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Value range checks
|
|
85
|
+
if (rules?.minValue !== undefined && numberValue < rules.minValue) {
|
|
86
|
+
return {
|
|
87
|
+
isValid: false,
|
|
88
|
+
error:
|
|
89
|
+
rules?.errorMessages?.minValue ||
|
|
90
|
+
defaultErrorMessages(rules, type).minValue ||
|
|
91
|
+
`Amount must be at least ${rules.minValue}`,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (rules?.maxValue !== undefined && numberValue > rules.maxValue) {
|
|
96
|
+
return {
|
|
97
|
+
isValid: false,
|
|
98
|
+
error:
|
|
99
|
+
rules?.errorMessages?.maxValue ||
|
|
100
|
+
defaultErrorMessages(rules, type).maxValue ||
|
|
101
|
+
`Amount must not exceed ${rules.maxValue}`,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return { isValid: true, error: "" };
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
module.exports = validateFloatNumber;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
// GSTIN validation
|
|
2
|
-
const validateGST = (value, rules, defaultErrorMessages, type) => {
|
|
3
|
-
const defaultRegex = /^\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}Z{1}[A-Z\d]{1}$/;
|
|
4
|
-
const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
|
|
5
|
-
|
|
6
|
-
if (!regexToUse.test(value)) {
|
|
7
|
-
return {
|
|
8
|
-
isValid: false,
|
|
9
|
-
error:
|
|
10
|
-
rules?.errorMessages?.regex ||
|
|
11
|
-
|
|
12
|
-
defaultErrorMessages(rules, type).invalid,
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return { isValid: true, error:
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
module.exports = validateGST;
|
|
1
|
+
// GSTIN validation
|
|
2
|
+
const validateGST = (value, rules, defaultErrorMessages, type) => {
|
|
3
|
+
const defaultRegex = /^\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}Z{1}[A-Z\d]{1}$/;
|
|
4
|
+
const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
|
|
5
|
+
|
|
6
|
+
if (!regexToUse.test(value)) {
|
|
7
|
+
return {
|
|
8
|
+
isValid: false,
|
|
9
|
+
error:
|
|
10
|
+
rules?.errorMessages?.regex ||
|
|
11
|
+
rules?.errorMessages?.invalid ||
|
|
12
|
+
defaultErrorMessages(rules, type).invalid,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return { isValid: true, error: '' };
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
module.exports = validateGST;
|
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
// IP Address validation
|
|
2
|
-
const validateIPAddress = (value, rules, defaultErrorMessages, type) => {
|
|
3
|
-
if (rules?.minLength && value?.length < rules?.minLength) {
|
|
4
|
-
return {
|
|
5
|
-
isValid: false,
|
|
6
|
-
error:
|
|
7
|
-
rules?.errorMessages?.minLength ||
|
|
8
|
-
defaultErrorMessages(rules, type).minLength,
|
|
9
|
-
};
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (rules?.maxLength && value?.length > rules?.maxLength) {
|
|
13
|
-
return {
|
|
14
|
-
isValid: false,
|
|
15
|
-
error:
|
|
16
|
-
rules?.errorMessages?.maxLength ||
|
|
17
|
-
defaultErrorMessages(rules, type).maxLength,
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const defaultRegex =
|
|
22
|
-
rules?.version === 'v4'
|
|
23
|
-
? /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
|
|
24
|
-
: rules?.version === 'v6'
|
|
25
|
-
? /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/
|
|
26
|
-
: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
27
|
-
const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
|
|
28
|
-
if (!regexToUse.test(value)) {
|
|
29
|
-
return {
|
|
30
|
-
isValid: false,
|
|
31
|
-
error: rules?.errorMessages?.invalid
|
|
32
|
-
? rules?.errorMessages?.invalid
|
|
33
|
-
: defaultErrorMessages(rules, type).ipAddress ||
|
|
34
|
-
defaultErrorMessages(rules, type).invalid,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return { isValid: true, error: '' };
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
module.exports = validateIPAddress;
|
|
1
|
+
// IP Address validation
|
|
2
|
+
const validateIPAddress = (value, rules, defaultErrorMessages, type) => {
|
|
3
|
+
if (rules?.minLength && value?.length < rules?.minLength) {
|
|
4
|
+
return {
|
|
5
|
+
isValid: false,
|
|
6
|
+
error:
|
|
7
|
+
rules?.errorMessages?.minLength ||
|
|
8
|
+
defaultErrorMessages(rules, type).minLength,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (rules?.maxLength && value?.length > rules?.maxLength) {
|
|
13
|
+
return {
|
|
14
|
+
isValid: false,
|
|
15
|
+
error:
|
|
16
|
+
rules?.errorMessages?.maxLength ||
|
|
17
|
+
defaultErrorMessages(rules, type).maxLength,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const defaultRegex =
|
|
22
|
+
rules?.version === 'v4'
|
|
23
|
+
? /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
|
|
24
|
+
: rules?.version === 'v6'
|
|
25
|
+
? /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/
|
|
26
|
+
: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
27
|
+
const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
|
|
28
|
+
if (!regexToUse.test(value)) {
|
|
29
|
+
return {
|
|
30
|
+
isValid: false,
|
|
31
|
+
error: rules?.errorMessages?.invalid
|
|
32
|
+
? rules?.errorMessages?.invalid
|
|
33
|
+
: defaultErrorMessages(rules, type).ipAddress ||
|
|
34
|
+
defaultErrorMessages(rules, type).invalid,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return { isValid: true, error: '' };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
module.exports = validateIPAddress;
|
|
@@ -1,116 +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;
|
|
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;
|