-
Notifications
You must be signed in to change notification settings - Fork 0
/
formCatcher.js
307 lines (306 loc) · 12 KB
/
formCatcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* FormCatcher.js
* https://github.com/Nobledsmarts/formCatcher
* Richard Franklin C [Noble desmarts]
* October 2020
*/
class FormCatcher{
constructor(rules){
this.rules = rules || {};
this.punct = [
'!', '#', '$', '%', '&', '*', '-', '_', '+', '=', '|', ':', '.',
];
}
validate(group, formData){
let hasError = this.hasError.bind(this, group, formData);
let getErrors = this.getErrors.bind(this, group, formData);
let getErrorsObj = this.getErrorsObj.bind(this, group, formData);
return {
hasError,
getErrors,
getErrorsObj
}
}
hasError(group, formData, field){
let pileError = this.pileError(group, formData, field, 'obj', true);
if(field) return pileError.errorsObj[field] ? !!pileError.errorsObj[field].length : pileError.hasError;
return pileError.hasError
}
pileError(group, formData, field, type, isHasError){
let formKeys = [... formData.keys()];
let formValues = [... formData.values()];
let errors = [];
let errorsObj = {};
let method = isHasError ? 'some' : 'forEach';
let hasError = formKeys[method]((key, idx) => {
let checks = (this.rules[group][key]).split("|");
errorsObj[key] = [];
return checks[method]((rule) => {
let regexMch = (/^(.+?)(\[(.+?)\])?$/ig).exec(rule);
let ruleLabel = regexMch[1];
let inputValue = formValues[idx].trim();
let hasPermitEmpty = checks.includes('permit_empty');
let groupErrors = this.rules[group + '_errors'];
let ruleLabels = Object.keys(this.ruleMethods());
let obj = {regexMch, inputValue, hasPermitEmpty};
if(ruleLabels.includes(ruleLabel)){
let ruleMethods = this.ruleMethods(obj);
let condition = !ruleMethods[ruleLabel]['run'](key, formData, checks);
let defaultErrorMsg = ruleMethods[ruleLabel]['error'](key, formData, checks);
let fieldError = groupErrors[key][ruleLabel] || defaultErrorMsg;
if(groupErrors[key]){
if(condition){
if( !errorsObj[key].length ){
errorsObj[key].push(fieldError);
errors.push(fieldError);
}
}
}
if( isHasError ) return condition;
} else {
let groupMethods = this.rules[group + '_rules'];
if(groupMethods && groupMethods[key]){
let customRules = Object.keys(groupMethods[key]);
if(customRules.includes(ruleLabel)){
let conditionObj = groupMethods[key][ruleLabel].call(this, inputValue, formData, ruleLabel) || {};
let condition = !!Object.keys(conditionObj).length;
if( condition ){
if( !errorsObj[key].length ){
errorsObj[key].push(conditionObj.error || groupErrors[key][ruleLabel] || key + ' Error !');
errors.push(conditionObj.error || groupErrors[key][ruleLabel] || key + ' Error !');
}
}
if( isHasError ) return condition;
}
}
}
});
});
if( isHasError ) return {hasError, errors, errorsObj};
return type ? (type == 'obj' ? errorsObj : errors) : errors;
}
ruleMethods(obj = {}){
let [regexMch, inputValue, hasPermitEmpty] = Object.values(obj);
let field = inputValue ? regexMch[3] : '';
return {
alpha : {
run : () => {
return (/^([a-z])+$/ig.test(inputValue));
},
error : (key) => {
return key + " can only contain alphabets"
}
},
alpha_space : {
run : () => {
return (/^([a-z\s])+$/ig.test(inputValue));
},
error : (key) => {
return key + " can only contain alphabets and spaces"
}
},
alpha_dash : {
run : () => {
return (/^([a-z-_])+$/ig.test(inputValue));
},
error : (key) => {
return key + " can only contain alphabets and dashes"
}
},
alpha_numeric : {
run : () => {
return (/^([a-z0-9])+$/ig.test(inputValue));
},
error : (key) => {
return key + " can only contain alphabets and numbers"
}
},
alpha_numeric_space : {
run : () => {
return (/^([a-z0-9\s])+$/ig.test(inputValue));
},
error : (key) => {
return key + " can only contain alphabets, numbers and spaces"
}
},
alpha_numeric_punct : {
run : () => {
return (new RegExp("^([a-z0-9\s" + this.punct.join('') + "])+$", "ig").test(inputValue));
}
},
decimal : {
run : () => {
return (/^((\-|\+)?[0-9]\.[0-9])$/ig.test(inputValue));
},
error : (key) => {
return key + " can only contain decimals"
}
},
numeric : {
run : () => {
return (/^(\+|\-)?([0-9])+$/ig.test(inputValue));
},
error : (key) => {
return key + " can only contain numeric values"
}
},
is_natural : {
run : () => {
return (/^([0-9])+$/ig.test(inputValue));
},
error : (key) => {
return key + " can only contain natural numbers"
}
},
is_natural_no_zero : {
run : () => {
return (/^([1-9][0-9]*)$/ig.test(inputValue));
},
error : (key) => {
return key + " can only contain counting numbers"
}
},
min_length : {
run : () => {
return (inputValue.length >= field);
},
error : (key) => {
return key + " length should not be less than " + field;
}
},
max_length : {
run : () => {
return inputValue.length <= field;
},
error : (key) => {
return key + " length should not be greater than " + field;
}
},
exact_length : {
run : () => {
return inputValue.length === field;
},
error : (key) => {
return key + " length should equal " + field;
}
},
valid_email : {
run : () => {
return (/^[a-y-0-9._]+@[a-y-0-9_]+\.[a-z]{2,}$/).test(inputValue);
},
error : (key) => {
return key + " should contain a valid email";
}
},
less_than : {
run : () => {
return inputValue < +field;
},
error : (key) => {
return key + " should be less than " + field;
}
},
less_than_equal_to : {
run : () => {
return inputValue >= +field;
},
error : (key) => {
return key + " should be less than or equal to " + field;
}
},
greater_than : {
run : () => {
return inputValue > +field;
},
error : (key) => {
return key + " should be greater than " + field;
}
},
greater_than_equal_to : {
run : () => {
return inputValue >= +field;
},
error : (key) => {
return key + " should be greater or equal to " + field;
}
},
differs : {
run : (key, formData) => {
return formData.get(field) !== inputValue
}
},
matches : {
run : (key, formData) => {
return formData.get(field) === inputValue
},
error : (key, formDara) => {
return key + " should contain same value as " + field;
}
},
in_list : {
run : () => {
let list = field.split(',').map((el) => el.trim());
return list.includes(inputValue);
},
error : (key, formDara) => {
return key + " error ";
}
},
required : {
run : () => {
return hasPermitEmpty ? true : inputValue;
},
error : (key, formDara) => {
return key + " is required";
}
},
regex_match : {
run : () => {
let regexHolder = field.split(',').map((e) => e.trim());
let regexStr = regexHolder[0];
let quantifier = regexHolder[1] || 'ig'
return (new RegExp(regexStr, quantifier).test(inputValue));
},
error : (key, formDara) => {
return key + " error ";
}
}
}
}
getErrorsObj(group, formData, field){
let errors = this.pileError(group, formData, field, 'obj');
return field && errors[field] ? errors[field] : errors;
}
getErrors(group, formData, field){
let errors = this.pileError(group, formData, field, 'array');
return errors;
}
setRule(group, field, ruleObj){
if ( !this.rules[group] ) this.rules[group] = {};
if ( !this.rules[group + '_errors'] ) this.rules[group + '_errors'] = {};
this.rules[group] = {
[field] : ruleObj.rule,
...this.rules[group]
}
this.rules[group + '_errors'][field] = ruleObj.errors;
}
setRules(group, rules){
for(let rule of rules){
this.setRule(group, rule.field, rule);
}
}
setRulesMethod(group, field, ruleObject){
if ( !this.rules[group] ) throw Error('cant set set custom rules on ' + group + ', group doesnt exist');
if ( !(this.rules[group + '_rules']) ) this.rules[group + '_rules'] = {};
this.rules[group + '_rules'][field] = {
...ruleObject,
...this.rules[group + '_rules'][field]
}
}
setRulesMethods(group, customRules){
if ( !this.rules[group] ) throw Error('cant set set custom rules on ' + group + ', group doesnt exist');
for(let field in customRules){
this.setRulesMethod(group, field, customRules[field])
}
}
}