quantique-field-validator 1.0.9 → 2.0.0
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/dist/index.d.ts +52 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +176 -0
- package/dist/messages/defaultErrorMessages.d.ts +8 -0
- package/dist/messages/defaultErrorMessages.d.ts.map +1 -0
- package/dist/messages/defaultErrorMessages.js +38 -0
- package/dist/types/index.d.ts +97 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/utils/safeRegex.d.ts +22 -0
- package/dist/utils/safeRegex.d.ts.map +1 -0
- package/dist/utils/safeRegex.js +32 -0
- package/dist/validators/validateAadhaar.d.ts +8 -0
- package/dist/validators/validateAadhaar.d.ts.map +1 -0
- package/dist/validators/validateAadhaar.js +20 -0
- package/dist/validators/validateAddress.d.ts +8 -0
- package/dist/validators/validateAddress.d.ts.map +1 -0
- package/dist/validators/validateAddress.js +38 -0
- package/dist/validators/validateAlphanumeric.d.ts +8 -0
- package/dist/validators/validateAlphanumeric.d.ts.map +1 -0
- package/dist/validators/validateAlphanumeric.js +38 -0
- package/dist/validators/validateBankIFSC.d.ts +9 -0
- package/dist/validators/validateBankIFSC.d.ts.map +1 -0
- package/dist/validators/validateBankIFSC.js +33 -0
- package/dist/validators/validateChassisNumber.d.ts +9 -0
- package/dist/validators/validateChassisNumber.d.ts.map +1 -0
- package/dist/validators/validateChassisNumber.js +33 -0
- package/dist/validators/validateCustom.d.ts +9 -0
- package/dist/validators/validateCustom.d.ts.map +1 -0
- package/dist/validators/validateCustom.js +26 -0
- package/dist/validators/validateDate.d.ts +11 -0
- package/dist/validators/validateDate.d.ts.map +1 -0
- package/dist/validators/validateDate.js +119 -0
- package/dist/validators/validateEmail.d.ts +9 -0
- package/dist/validators/validateEmail.d.ts.map +1 -0
- package/dist/validators/validateEmail.js +33 -0
- package/dist/validators/validateEngineNumber.d.ts +8 -0
- package/dist/validators/validateEngineNumber.d.ts.map +1 -0
- package/dist/validators/validateEngineNumber.js +32 -0
- package/dist/validators/validateFirstMiddleLastName.d.ts +9 -0
- package/dist/validators/validateFirstMiddleLastName.d.ts.map +1 -0
- package/dist/validators/validateFirstMiddleLastName.js +33 -0
- package/dist/validators/validateFloatNumber.d.ts +13 -0
- package/dist/validators/validateFloatNumber.d.ts.map +1 -0
- package/dist/validators/validateFloatNumber.js +75 -0
- package/dist/validators/validateGST.d.ts +9 -0
- package/dist/validators/validateGST.d.ts.map +1 -0
- package/dist/validators/validateGST.js +21 -0
- package/dist/validators/validateIPAddress.d.ts +8 -0
- package/dist/validators/validateIPAddress.d.ts.map +1 -0
- package/dist/validators/validateIPAddress.js +51 -0
- package/dist/validators/validateMobile.d.ts +9 -0
- package/dist/validators/validateMobile.d.ts.map +1 -0
- package/dist/validators/validateMobile.js +21 -0
- package/dist/validators/validateName.d.ts +11 -0
- package/dist/validators/validateName.d.ts.map +1 -0
- package/dist/validators/validateName.js +53 -0
- package/dist/validators/validateNumber.d.ts +8 -0
- package/dist/validators/validateNumber.d.ts.map +1 -0
- package/dist/validators/validateNumber.js +51 -0
- package/dist/validators/validatePanCard.d.ts +8 -0
- package/dist/validators/validatePanCard.d.ts.map +1 -0
- package/dist/validators/validatePanCard.js +20 -0
- package/dist/validators/validatePassword.d.ts +4 -0
- package/dist/validators/validatePassword.d.ts.map +1 -0
- package/dist/validators/validatePassword.js +47 -0
- package/dist/validators/validatePincode.d.ts +8 -0
- package/dist/validators/validatePincode.d.ts.map +1 -0
- package/dist/validators/validatePincode.js +20 -0
- package/dist/validators/validateRTO.d.ts +12 -0
- package/dist/validators/validateRTO.d.ts.map +1 -0
- package/dist/validators/validateRTO.js +45 -0
- package/dist/validators/validateString.d.ts +8 -0
- package/dist/validators/validateString.d.ts.map +1 -0
- package/dist/validators/validateString.js +39 -0
- package/dist/validators/validateURL.d.ts +9 -0
- package/dist/validators/validateURL.d.ts.map +1 -0
- package/dist/validators/validateURL.js +51 -0
- package/package.json +50 -9
- package/.github/workflows/publish.yml +0 -31
- package/Messages/defaultErrorMessages.js +0 -27
- package/README.md +0 -0
- package/Validators/ValidateChassisNumber.js +0 -37
- package/Validators/validateAadhaar.js +0 -18
- package/Validators/validateAddress.js +0 -38
- package/Validators/validateAlphanumeric.js +0 -47
- package/Validators/validateBankIFSC.js +0 -41
- package/Validators/validateCustom.js +0 -18
- package/Validators/validateDate.js +0 -166
- package/Validators/validateEmail.js +0 -39
- package/Validators/validateEngineNumber.js +0 -37
- package/Validators/validateFirstMiddleLastName.js +0 -36
- package/Validators/validateFloatNumber.js +0 -108
- package/Validators/validateGST.js +0 -19
- package/Validators/validateIPAddress.js +0 -41
- package/Validators/validateMobile.js +0 -116
- package/Validators/validateName.js +0 -67
- package/Validators/validateNumber.js +0 -75
- package/Validators/validatePanCard.js +0 -23
- package/Validators/validatePassword.js +0 -38
- package/Validators/validateRTO.js +0 -41
- package/Validators/validateString.js +0 -41
- package/index.js +0 -194
|
@@ -1,116 +0,0 @@
|
|
|
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,67 +0,0 @@
|
|
|
1
|
-
// Name validation
|
|
2
|
-
const validateName = (
|
|
3
|
-
value,
|
|
4
|
-
rules = { type: 'user' },
|
|
5
|
-
defaultErrorMessages,
|
|
6
|
-
type
|
|
7
|
-
) => {
|
|
8
|
-
// First check if value is empty or just whitespace
|
|
9
|
-
if (!value || !value.trim()) {
|
|
10
|
-
return {
|
|
11
|
-
isValid: false,
|
|
12
|
-
error: rules?.errorMessages?.empty || 'Name cannot be empty',
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// Check length constraints
|
|
17
|
-
if (rules?.minLength && value?.length < rules?.minLength) {
|
|
18
|
-
return {
|
|
19
|
-
isValid: false,
|
|
20
|
-
error:
|
|
21
|
-
rules?.errorMessages?.minLength ||
|
|
22
|
-
defaultErrorMessages(rules, type).minLength,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (rules?.maxLength && value?.length > rules?.maxLength) {
|
|
27
|
-
return {
|
|
28
|
-
isValid: false,
|
|
29
|
-
error:
|
|
30
|
-
rules?.errorMessages?.maxLength ||
|
|
31
|
-
defaultErrorMessages(rules, type).maxLength,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Default name validation (only letters, spaces, and basic punctuation)
|
|
36
|
-
// Check against custom regex if provided
|
|
37
|
-
if (rules?.type === 'user') {
|
|
38
|
-
const defaultRegex = /^(?=.*[a-zA-ZÀ-ÿ])[a-zA-ZÀ-ÿ\s'-.]*$/;
|
|
39
|
-
const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
|
|
40
|
-
|
|
41
|
-
if (!regexToUse.test(value)) {
|
|
42
|
-
return {
|
|
43
|
-
isValid: false,
|
|
44
|
-
error:
|
|
45
|
-
rules?.errorMessages?.invalid || 'Name contains invalid characters',
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (rules?.type === 'corporate') {
|
|
51
|
-
const defaultRegex = /^(?=.*[A-Za-z0-9])[A-Za-z0-9&.,'()\- ]*$/;
|
|
52
|
-
const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
|
|
53
|
-
|
|
54
|
-
if (!regexToUse.test(value)) {
|
|
55
|
-
return {
|
|
56
|
-
isValid: false,
|
|
57
|
-
error:
|
|
58
|
-
rules?.errorMessages?.invalid ||
|
|
59
|
-
'Corporate Name contains invalid characters',
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return { isValid: true, error: '' };
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
module.exports = validateName;
|
|
@@ -1,75 +0,0 @@
|
|
|
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
|
-
// Whole number only regex
|
|
15
|
-
const defaultNumberRegex = /^\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 whole 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;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
const validatePanCard = (
|
|
2
|
-
value,
|
|
3
|
-
rules,
|
|
4
|
-
defaultErrorMessages,
|
|
5
|
-
type
|
|
6
|
-
) => {
|
|
7
|
-
const defaultRegex = /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/;
|
|
8
|
-
const regexToUse = rules?.regex ? new RegExp(rules.regex) : defaultRegex;
|
|
9
|
-
|
|
10
|
-
if (!regexToUse.test(value)) {
|
|
11
|
-
return {
|
|
12
|
-
isValid: false,
|
|
13
|
-
error:
|
|
14
|
-
rules?.errorMessages?.regex ||
|
|
15
|
-
rules?.errorMessages?.invalid ||
|
|
16
|
-
defaultErrorMessages(rules, type).invalid,
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return { isValid: true, error: "" };
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
module.exports = validatePanCard;
|
|
@@ -1,38 +0,0 @@
|
|
|
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;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// RTO validation
|
|
2
|
-
const validateRTO = (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 === 'new'
|
|
23
|
-
? /^[A-Z]{2}-[0-9]{1,2}(-[A-Z]{1,2})?$/
|
|
24
|
-
: rules?.version === 'all'
|
|
25
|
-
? /^[A-Z]{2}-?[0-9]{1,2}(-?[A-Z]{1,2})?$/
|
|
26
|
-
: /^[A-Z]{2}-[0-9]{1,2}$/;
|
|
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).rto ||
|
|
34
|
-
defaultErrorMessages(rules, type).invalid,
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return { isValid: true, error: '' };
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
module.exports = validateRTO;
|
|
@@ -1,41 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Enhanced dynamic validator with custom function support
|
|
3
|
-
* @param {string} value - The value to validate
|
|
4
|
-
* @param {string} type - Type of validation ('name', 'email', 'mobile', 'address', etc.)
|
|
5
|
-
* @param {object} rules - Custom rules for validation
|
|
6
|
-
* @param {function} [customValidator] - Optional custom validation function
|
|
7
|
-
* @returns {object} - Returns { isValid: boolean, error: string }
|
|
8
|
-
*/
|
|
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 validateFloatNumber = require('./Validators/validateFloatNumber');
|
|
15
|
-
const validateDate = require('./Validators/validateDate');
|
|
16
|
-
const validateName = require('./Validators/validateName');
|
|
17
|
-
const validateFirstMiddleLastName = require('./Validators/validateFirstMiddleLastName');
|
|
18
|
-
const validateEmail = require('./Validators/validateEmail');
|
|
19
|
-
const validateMobile = require('./Validators/validateMobile');
|
|
20
|
-
const validateAddress = require('./Validators/validateAddress');
|
|
21
|
-
const validatePassword = require('./Validators/validatePassword');
|
|
22
|
-
const validateCustom = require('./Validators/validateCustom');
|
|
23
|
-
const validateAadhaar = require('./Validators/validateAadhaar');
|
|
24
|
-
const validatePanCard = require('./Validators/validatePanCard');
|
|
25
|
-
const validateGST = require('./Validators/validateGST');
|
|
26
|
-
const validateChassisNumber = require('./Validators/ValidateChassisNumber');
|
|
27
|
-
const validateEngineNumber = require('./Validators/validateEngineNumber');
|
|
28
|
-
const validateIPAddress = require('./Validators/validateIPAddress');
|
|
29
|
-
const validateRTO = require('./Validators/validateRTO');
|
|
30
|
-
const validateBankIFSC = require('./Validators/validateBankIFSC');
|
|
31
|
-
|
|
32
|
-
const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
|
|
33
|
-
// Trim the value if it's a string
|
|
34
|
-
// const selectedValue = typeof value === "string" ? value.trim() : value;
|
|
35
|
-
|
|
36
|
-
const selectedValue = value;
|
|
37
|
-
|
|
38
|
-
// Globally prevent emoji characters from being validated as valid data
|
|
39
|
-
if (typeof selectedValue === 'string') {
|
|
40
|
-
const emojiRegex = /[\u{1F300}-\u{1F64F}\u{1F680}-\u{1F6FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}\u{1F900}-\u{1F9FF}\u{1FA70}-\u{1FAFF}\u{1F1E6}-\u{1F1FF}]/u;
|
|
41
|
-
if (emojiRegex.test(selectedValue)) {
|
|
42
|
-
return {
|
|
43
|
-
isValid: false,
|
|
44
|
-
error:
|
|
45
|
-
rules.errorMessages?.emoji ||
|
|
46
|
-
defaultErrorMessages(rules, type).emoji ||
|
|
47
|
-
"Value contains emojis which are not allowed",
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Check if field is required and empty
|
|
53
|
-
if (rules.required && (!selectedValue || selectedValue === '')) {
|
|
54
|
-
return {
|
|
55
|
-
isValid: false,
|
|
56
|
-
error:
|
|
57
|
-
rules.errorMessages?.required ||
|
|
58
|
-
defaultErrorMessages(rules, type).required,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Skip further validation if the field is empty and not required
|
|
63
|
-
if (!rules.required && (!selectedValue || selectedValue === '')) {
|
|
64
|
-
return { isValid: true, error: '' };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// First run custom validator if provided (highest priority)
|
|
68
|
-
if (customValidator && typeof customValidator === 'function') {
|
|
69
|
-
const customValidation = customValidator(selectedValue);
|
|
70
|
-
|
|
71
|
-
if (
|
|
72
|
-
typeof customValidation === 'object' &&
|
|
73
|
-
customValidation.isValid === false
|
|
74
|
-
) {
|
|
75
|
-
return {
|
|
76
|
-
isValid: false,
|
|
77
|
-
error:
|
|
78
|
-
customValidation.error ||
|
|
79
|
-
defaultErrorMessages(rules, type).customValidation,
|
|
80
|
-
};
|
|
81
|
-
} else if (customValidation === false) {
|
|
82
|
-
return {
|
|
83
|
-
isValid: false,
|
|
84
|
-
error: defaultErrorMessages(rules, type).customValidation,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
// If custom validator returns true or { isValid: true }, continue with other validations
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Then run any custom validator in rules
|
|
91
|
-
if (rules.customValidator && typeof rules.customValidator === 'function') {
|
|
92
|
-
const rulesCustomValidation = rules.customValidator(selectedValue);
|
|
93
|
-
|
|
94
|
-
if (
|
|
95
|
-
typeof rulesCustomValidation === 'object' &&
|
|
96
|
-
rulesCustomValidation.isValid === false
|
|
97
|
-
) {
|
|
98
|
-
return {
|
|
99
|
-
isValid: false,
|
|
100
|
-
error:
|
|
101
|
-
rulesCustomValidation.error ||
|
|
102
|
-
defaultErrorMessages(rules, type).custom,
|
|
103
|
-
};
|
|
104
|
-
} else if (rulesCustomValidation === false) {
|
|
105
|
-
return {
|
|
106
|
-
isValid: false,
|
|
107
|
-
error: defaultErrorMessages(rules, type).custom,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// Then validate based on type
|
|
113
|
-
switch (type) {
|
|
114
|
-
case 'string':
|
|
115
|
-
return validateString(selectedValue, rules, defaultErrorMessages, type);
|
|
116
|
-
case 'alphanumeric':
|
|
117
|
-
return validateAlphanumeric(
|
|
118
|
-
selectedValue,
|
|
119
|
-
rules,
|
|
120
|
-
defaultErrorMessages,
|
|
121
|
-
type
|
|
122
|
-
);
|
|
123
|
-
case 'number':
|
|
124
|
-
return validateNumber(selectedValue, rules, defaultErrorMessages, type);
|
|
125
|
-
case 'float':
|
|
126
|
-
return validateFloatNumber(
|
|
127
|
-
selectedValue,
|
|
128
|
-
rules,
|
|
129
|
-
defaultErrorMessages,
|
|
130
|
-
type
|
|
131
|
-
);
|
|
132
|
-
case 'date':
|
|
133
|
-
return validateDate(selectedValue, rules, defaultErrorMessages, type);
|
|
134
|
-
case 'name':
|
|
135
|
-
return validateName(selectedValue, rules, defaultErrorMessages, type);
|
|
136
|
-
case 'firstName':
|
|
137
|
-
case 'middleName':
|
|
138
|
-
case 'lastName':
|
|
139
|
-
return validateFirstMiddleLastName(
|
|
140
|
-
selectedValue,
|
|
141
|
-
rules,
|
|
142
|
-
defaultErrorMessages,
|
|
143
|
-
type
|
|
144
|
-
);
|
|
145
|
-
case 'email':
|
|
146
|
-
return validateEmail(selectedValue, rules, defaultErrorMessages, type);
|
|
147
|
-
case 'mobile':
|
|
148
|
-
return validateMobile(selectedValue, rules, defaultErrorMessages, type);
|
|
149
|
-
case 'address':
|
|
150
|
-
return validateAddress(selectedValue, rules, defaultErrorMessages, type);
|
|
151
|
-
case 'password':
|
|
152
|
-
return validatePassword(selectedValue, rules, defaultErrorMessages, type);
|
|
153
|
-
case 'chassis':
|
|
154
|
-
return validateChassisNumber(
|
|
155
|
-
selectedValue,
|
|
156
|
-
rules,
|
|
157
|
-
defaultErrorMessages,
|
|
158
|
-
type
|
|
159
|
-
);
|
|
160
|
-
case 'engine':
|
|
161
|
-
return validateEngineNumber(
|
|
162
|
-
selectedValue,
|
|
163
|
-
rules,
|
|
164
|
-
defaultErrorMessages,
|
|
165
|
-
type
|
|
166
|
-
);
|
|
167
|
-
case 'aadhaar':
|
|
168
|
-
return validateAadhaar(selectedValue, rules, defaultErrorMessages, type);
|
|
169
|
-
case 'pan':
|
|
170
|
-
return validatePanCard(selectedValue, rules, defaultErrorMessages, type);
|
|
171
|
-
case 'gst':
|
|
172
|
-
return validateGST(selectedValue, rules, defaultErrorMessages, type);
|
|
173
|
-
case 'rto':
|
|
174
|
-
return validateRTO(selectedValue, rules, defaultErrorMessages, type);
|
|
175
|
-
case 'ifsc':
|
|
176
|
-
return validateBankIFSC(selectedValue, rules, defaultErrorMessages, type);
|
|
177
|
-
case 'ip':
|
|
178
|
-
return validateIPAddress(
|
|
179
|
-
selectedValue,
|
|
180
|
-
rules,
|
|
181
|
-
defaultErrorMessages,
|
|
182
|
-
type
|
|
183
|
-
);
|
|
184
|
-
case 'custom':
|
|
185
|
-
return validateCustom(selectedValue, rules, defaultErrorMessages, type);
|
|
186
|
-
default:
|
|
187
|
-
return {
|
|
188
|
-
isValid: false,
|
|
189
|
-
error: defaultErrorMessages(rules, type)?.noValidation,
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
module.exports = dynamicValidator;
|