This repository was archived by the owner on Feb 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidator.js
More file actions
163 lines (153 loc) · 5.64 KB
/
Validator.js
File metadata and controls
163 lines (153 loc) · 5.64 KB
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
var Validator = function (options) {
var rules = options.rules || {};
var data = options.data || {};
var errors = {};
var messages = {
required: 'The :attribute field is required.',
email: 'The :attribute must be a valid email address.',
min: {
numeric: 'The :attribute must be at least :min.',
string: 'The :attribute must be at least :min characters.'
},
max: {
numeric: 'The :attribute may not be greater than :max.',
string: 'The :attribute may not be greater than :max characters.'
}
};
var prepareRules = function () {
for (var index in rules) {
var property = rules[index];
if (!Array.isArray(property)) {
rules[index] = property.split('|');
}
}
};
var addErrors = function (attribute, attributeErrors) {
errors[attribute] = attributeErrors;
};
var createError = function (validationAttribute, validationRule, validationParams) {
var errorMessage = '';
switch (validationRule) {
case 'required':
case 'email':
errorMessage = messages[validationRule].replace(":attribute", validationAttribute);
break;
case 'min':
var type = 'numeric';
if (typeof data[validationAttribute] == 'string') {
type = 'string';
}
errorMessage = messages[validationRule][type].replace(":attribute", validationAttribute);
errorMessage = errorMessage.replace(":min", validationParams[0]);
break;
case 'max':
var type = 'numeric';
if (typeof data[validationAttribute] == 'string') {
type = 'string';
}
errorMessage = messages[validationRule][type].replace(":attribute", validationAttribute);
errorMessage = errorMessage.replace(":max", validationParams[0]);
break;
}
return errorMessage;
};
var isPassed = function (validationAttribute, validationRule, validationParams) {
var result = false;
switch (validationRule) {
case 'required':
if (!isEmpty(data[validationAttribute])) {
result = true;
}
break;
case 'email':
if (isEmpty(data[validationAttribute])) {
result = true;
}
else if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(data[validationAttribute])) {
result = true;
}
break;
case 'min':
if (isEmpty(data[validationAttribute])) {
result = true;
} else if (typeof validationAttribute == 'number') {
if (data[validationAttribute] >= validationParams[0]) {
result = true;
}
} else if (typeof validationAttribute == 'string') {
if (data[validationAttribute].length >= validationParams[0]) {
result = true;
}
}
break;
case 'max':
if (isEmpty(data[validationAttribute])) {
result = true;
} else if (typeof validationAttribute == 'number') {
if (data[validationAttribute] <= validationParams[0]) {
result = true;
}
} else if (typeof validationAttribute == 'string') {
if (data[validationAttribute].length <= validationParams[0]) {
result = true;
}
}
break;
}
return result;
};
var isEmpty = function (value) {
var result = false;
switch (value) {
case "":
case undefined:
result = true;
break;
}
return result;
};
var fails = function () {
return !passes();
};
var passes = function () {
prepareRules();
for (var validationAttribute in rules) {
var attributeErrors = [];
for (var index in rules[validationAttribute]) {
var validationRule = rules[validationAttribute][index];
var validationParams = [];
if (validationRule.indexOf(':') != -1) {
var arr = validationRule.split(':');
for (var i in arr) {
if (i == 0) {
validationRule = arr[i];
} else {
validationParams.push(arr[i])
}
}
}
var result = isPassed(validationAttribute, validationRule, validationParams);
if (!result) {
attributeErrors.push(createError(validationAttribute, validationRule, validationParams))
}
}
if (attributeErrors.length > 0) {
addErrors(validationAttribute, attributeErrors)
}
}
if (Object.keys(errors).length > 0) {
return false;
}
return true;
};
var getErrors = function () {
return errors;
};
var getFirstError = function (attribute) {
return errors[attribute][0];
};
this.getErrors = getErrors;
this.getFirstError = getFirstError;
this.fails = fails;
this.passes = passes;
};