quantique-field-validator 1.0.0 → 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.
- package/Messages/defaultErrorMessages.js +20 -0
- package/Validators/validateAadhaar.js +33 -0
- package/Validators/validateAddress.js +38 -0
- package/Validators/validateAlphanumeric.js +47 -0
- package/Validators/validateCustom.js +18 -0
- package/Validators/validateDate.js +118 -0
- package/Validators/validateEmail.js +38 -0
- package/Validators/validateMobile.js +116 -0
- package/Validators/validateName.js +59 -0
- package/Validators/validateNumber.js +75 -0
- package/Validators/validatePanCard.js +34 -0
- package/Validators/validatePassword.js +38 -0
- package/Validators/validateString.js +41 -0
- package/index.js +55 -225
- package/package.json +22 -22
|
@@ -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
|
@@ -6,26 +6,34 @@
|
|
|
6
6
|
* @param {function} [customValidator] - Optional custom validation function
|
|
7
7
|
* @returns {object} - Returns { isValid: boolean, error: string }
|
|
8
8
|
*/
|
|
9
|
-
const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
|
|
10
|
-
// Default error messages
|
|
11
|
-
const defaultErrorMessages = {
|
|
12
|
-
required: "This field is required",
|
|
13
|
-
minLength: `Minimum length should be ${rules.minLength}`,
|
|
14
|
-
maxLength: `Maximum length should be ${rules.maxLength}`,
|
|
15
|
-
invalid: `Invalid ${type}`,
|
|
16
|
-
custom: "Validation failed",
|
|
17
|
-
customValidation:
|
|
18
|
-
rules.errorMessages?.customValidation || "Custom validation failed",
|
|
19
|
-
};
|
|
20
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) => {
|
|
21
25
|
// Trim the value if it's a string
|
|
22
|
-
const trimmedValue = typeof value === "string" ? value.trim() : value;
|
|
26
|
+
// const trimmedValue = typeof value === "string" ? value.trim() : value;
|
|
27
|
+
|
|
28
|
+
const trimmedValue = value;
|
|
23
29
|
|
|
24
30
|
// Check if field is required and empty
|
|
25
31
|
if (rules.required && (!trimmedValue || trimmedValue === "")) {
|
|
26
32
|
return {
|
|
27
33
|
isValid: false,
|
|
28
|
-
error:
|
|
34
|
+
error:
|
|
35
|
+
rules.errorMessages?.required ||
|
|
36
|
+
defaultErrorMessages(rules, type).required,
|
|
29
37
|
};
|
|
30
38
|
}
|
|
31
39
|
|
|
@@ -44,12 +52,14 @@ const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
|
|
|
44
52
|
) {
|
|
45
53
|
return {
|
|
46
54
|
isValid: false,
|
|
47
|
-
error:
|
|
55
|
+
error:
|
|
56
|
+
customValidation.error ||
|
|
57
|
+
defaultErrorMessages(rules, type).customValidation,
|
|
48
58
|
};
|
|
49
59
|
} else if (customValidation === false) {
|
|
50
60
|
return {
|
|
51
61
|
isValid: false,
|
|
52
|
-
error: defaultErrorMessages.customValidation,
|
|
62
|
+
error: defaultErrorMessages(rules, type).customValidation,
|
|
53
63
|
};
|
|
54
64
|
}
|
|
55
65
|
// If custom validator returns true or { isValid: true }, continue with other validations
|
|
@@ -65,235 +75,55 @@ const dynamicValidator = (value, type, rules = {}, customValidator = null) => {
|
|
|
65
75
|
) {
|
|
66
76
|
return {
|
|
67
77
|
isValid: false,
|
|
68
|
-
error:
|
|
78
|
+
error:
|
|
79
|
+
rulesCustomValidation.error ||
|
|
80
|
+
defaultErrorMessages(rules, type).custom,
|
|
69
81
|
};
|
|
70
82
|
} else if (rulesCustomValidation === false) {
|
|
71
83
|
return {
|
|
72
84
|
isValid: false,
|
|
73
|
-
error: defaultErrorMessages.custom,
|
|
85
|
+
error: defaultErrorMessages(rules, type).custom,
|
|
74
86
|
};
|
|
75
87
|
}
|
|
76
88
|
}
|
|
77
89
|
|
|
78
90
|
// Then validate based on type
|
|
79
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);
|
|
80
105
|
case "name":
|
|
81
|
-
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
|
+
|
|
82
112
|
case "email":
|
|
83
|
-
return validateEmail(trimmedValue, rules, defaultErrorMessages);
|
|
113
|
+
return validateEmail(trimmedValue, rules, defaultErrorMessages, type);
|
|
84
114
|
case "mobile":
|
|
85
|
-
return validateMobile(trimmedValue, rules, defaultErrorMessages);
|
|
115
|
+
return validateMobile(trimmedValue, rules, defaultErrorMessages, type);
|
|
86
116
|
case "address":
|
|
87
|
-
return validateAddress(trimmedValue, rules, defaultErrorMessages);
|
|
117
|
+
return validateAddress(trimmedValue, rules, defaultErrorMessages, type);
|
|
88
118
|
case "password":
|
|
89
|
-
return validatePassword(trimmedValue, rules, defaultErrorMessages);
|
|
119
|
+
return validatePassword(trimmedValue, rules, defaultErrorMessages, type);
|
|
120
|
+
case "custom":
|
|
121
|
+
return validateCustom(trimmedValue, rules, defaultErrorMessages, type);
|
|
90
122
|
default:
|
|
91
|
-
return validateCustom(trimmedValue, rules, defaultErrorMessages);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
// ... (keep all the existing validateName, validateEmail, etc. functions from previous solution)
|
|
96
|
-
|
|
97
|
-
// Name validation
|
|
98
|
-
const validateName = (value, rules, defaultErrorMessages) => {
|
|
99
|
-
// Check length constraints
|
|
100
|
-
if (rules?.minLength && value?.length < rules?.minLength) {
|
|
101
|
-
return {
|
|
102
|
-
isValid: false,
|
|
103
|
-
error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (rules?.maxLength && value?.length > rules?.maxLength) {
|
|
108
|
-
return {
|
|
109
|
-
isValid: false,
|
|
110
|
-
error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Check against custom regex if provided
|
|
115
|
-
if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
|
|
116
|
-
return {
|
|
117
|
-
isValid: false,
|
|
118
|
-
error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Default name validation (only letters, spaces, and basic punctuation)
|
|
123
|
-
const nameRegex = /^[a-zA-ZÀ-ÿ\s'-.]*$/;
|
|
124
|
-
if (!nameRegex.test(value)) {
|
|
125
|
-
return {
|
|
126
|
-
isValid: false,
|
|
127
|
-
error:
|
|
128
|
-
rules?.errorMessages?.invalid || "Name contains invalid characters",
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return { isValid: true, error: "" };
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
// Email validation
|
|
136
|
-
const validateEmail = (value, rules, defaultErrorMessages) => {
|
|
137
|
-
// Check length constraints
|
|
138
|
-
if (rules?.minLength && value.length < rules?.minLength) {
|
|
139
|
-
return {
|
|
140
|
-
isValid: false,
|
|
141
|
-
error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (rules?.maxLength && value.length > rules?.maxLength) {
|
|
146
|
-
return {
|
|
147
|
-
isValid: false,
|
|
148
|
-
error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// Check against custom regex if provided
|
|
153
|
-
if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
|
|
154
|
-
return {
|
|
155
|
-
isValid: false,
|
|
156
|
-
error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// Default email regex
|
|
161
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
162
|
-
if (!emailRegex.test(value)) {
|
|
163
|
-
return {
|
|
164
|
-
isValid: false,
|
|
165
|
-
error:
|
|
166
|
-
rules?.errorMessages?.invalid || "Please enter a valid email address",
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return { isValid: true, error: "" };
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
// Mobile validation
|
|
174
|
-
const validateMobile = (value, rules, defaultErrorMessages) => {
|
|
175
|
-
// Check length constraints
|
|
176
|
-
if (rules?.minLength && value.length < rules?.minLength) {
|
|
177
|
-
return {
|
|
178
|
-
isValid: false,
|
|
179
|
-
error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (rules?.maxLength && value.length > rules?.maxLength) {
|
|
184
|
-
return {
|
|
185
|
-
isValid: false,
|
|
186
|
-
error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// Check against custom regex if provided
|
|
191
|
-
if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
|
|
192
|
-
return {
|
|
193
|
-
isValid: false,
|
|
194
|
-
error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// Default mobile regex (accepts numbers, +, -, and spaces)
|
|
199
|
-
const mobileRegex = /^[\d\s+\-()]*$/;
|
|
200
|
-
if (!mobileRegex.test(value)) {
|
|
201
|
-
return {
|
|
202
|
-
isValid: false,
|
|
203
|
-
error:
|
|
204
|
-
rules?.errorMessages?.invalid || "Please enter a valid mobile number",
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return { isValid: true, error: "" };
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
// Address validation
|
|
212
|
-
const validateAddress = (value, rules, defaultErrorMessages) => {
|
|
213
|
-
// Check length constraints
|
|
214
|
-
if (rules?.minLength && value.length < rules?.minLength) {
|
|
215
|
-
return {
|
|
216
|
-
isValid: false,
|
|
217
|
-
error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if (rules?.maxLength && value.length > rules?.maxLength) {
|
|
222
|
-
return {
|
|
223
|
-
isValid: false,
|
|
224
|
-
error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Check against custom regex if provided
|
|
229
|
-
if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
|
|
230
|
-
return {
|
|
231
|
-
isValid: false,
|
|
232
|
-
error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
return { isValid: true, error: "" };
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
// Add password validation
|
|
240
|
-
const validatePassword = (value, rules, defaultErrorMessages) => {
|
|
241
|
-
// Check length constraints
|
|
242
|
-
if (rules?.minLength && value.length < rules?.minLength) {
|
|
243
|
-
return {
|
|
244
|
-
isValid: false,
|
|
245
|
-
error: rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
if (rules?.maxLength && value.length > rules?.maxLength) {
|
|
250
|
-
return {
|
|
251
|
-
isValid: false,
|
|
252
|
-
error: rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// Check against custom regex if provided
|
|
257
|
-
if (rules?.regex && !new RegExp(rules?.regex).test(value)) {
|
|
258
|
-
return {
|
|
259
|
-
isValid: false,
|
|
260
|
-
error: rules?.errorMessages?.invalid || defaultErrorMessages.invalid,
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Default password validation (at least one letter, one number)
|
|
265
|
-
const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d).+$/;
|
|
266
|
-
if (!passwordRegex.test(value)) {
|
|
267
|
-
return {
|
|
268
|
-
isValid: false,
|
|
269
|
-
error:
|
|
270
|
-
rules?.errorMessages?.invalid ||
|
|
271
|
-
"Password must contain at least one letter and one number",
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
return { isValid: true, error: "" };
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
// Custom validation
|
|
279
|
-
const validateCustom = (value, rules, defaultErrorMessages) => {
|
|
280
|
-
// Check length constraints if value is a string
|
|
281
|
-
if (typeof value === "string") {
|
|
282
|
-
if (rules?.minLength && value.length < rules?.minLength) {
|
|
283
123
|
return {
|
|
284
124
|
isValid: false,
|
|
285
|
-
error:
|
|
286
|
-
rules?.errorMessages?.minLength || defaultErrorMessages.minLength,
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if (rules?.maxLength && value.length > rules?.maxLength) {
|
|
291
|
-
return {
|
|
292
|
-
isValid: false,
|
|
293
|
-
error:
|
|
294
|
-
rules?.errorMessages?.maxLength || defaultErrorMessages.maxLength,
|
|
125
|
+
error: defaultErrorMessages(rules, type)?.noValidation,
|
|
295
126
|
};
|
|
296
|
-
}
|
|
297
127
|
}
|
|
298
128
|
};
|
|
299
129
|
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "quantique-field-validator",
|
|
3
|
-
"version": "1.0.
|
|
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
|
-
}
|
|
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
|
+
}
|