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
package/index.js
CHANGED
|
@@ -1,180 +1,194 @@
|
|
|
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
|
-
//
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
type
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
case '
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
case '
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
);
|
|
153
|
-
case '
|
|
154
|
-
return
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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;
|
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.9",
|
|
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
|
+
}
|