-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
265 lines (257 loc) · 8.17 KB
/
index.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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof =
typeof Symbol === "function" && typeof Symbol.iterator === "symbol"
? function(obj) {
return typeof obj;
}
: function(obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol
? "symbol"
: typeof obj;
};
/*
* Checks whether string is valid email or not
*/
var isValidEmail = function isValidEmail(email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
};
/*
* Checks whether string is valid numeric or not
*/
var isNumeric = function isNumeric(num) {
return !isNaN(num);
};
/*
* Checks whether string is valid alpha numeric or not
*/
var isAlphaNumeric = function isAlphaNumeric(str) {
var regExp = /^[A-Za-z0-9]+$/;
return regExp.test(String(str).toLowerCase());
};
/*
* Checks whether string is valid alphabatic string or not
*/
var isAlpha = function isAlpha(str) {
return /^[a-zA-Z ]+$/.test(str);
};
/*
* Checks whether string is valid username string or not
*/
var isValidUsername = function isValidUsername(str) {
return new RegExp("^[a-zA-Z0-9_.]+$").test(str);
};
/*
* Checks whether string is valid password string or not
*/
var isValidPassword = function isValidPassword(str) {
return new RegExp(
"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$"
).test(str);
};
/**
*
*/
var isValidURL = (exports.isValidURL = function isValidURL(str) {
var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-/]))?/;
return regex.test(str);
});
/*
* Main Validator function to validate any object
*/
var errors = {};
// set default form valid to true
var isValid = true;
var Validator = (exports.Validator = function Validator(data) {
var validation =
arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var messages =
arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var reCalled =
arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];
if (!reCalled) {
errors = {};
isValid = true;
}
/* Cheks if data is valid object or not */
if (
(typeof data === "undefined" ? "undefined" : _typeof(data)) !== "object"
) {
throw new Error("Data should be an object.");
}
/* Cheks if validation is valid object or not */
if (
(typeof validation === "undefined" ? "undefined" : _typeof(validation)) !==
"object"
) {
throw new Error("Validation should be an object.");
}
/* Cheks if messages is valid object or not */
if (
(typeof messages === "undefined" ? "undefined" : _typeof(messages)) !==
"object"
) {
throw new Error("Messages should be an object.");
}
// loop through the data object
for (var key in data) {
// check data has key and also validation key exists
if (data.hasOwnProperty(key) && validation.hasOwnProperty(key)) {
// set the current value
var value = data[key];
// check if the value is object type
if (
(typeof value === "undefined" ? "undefined" : _typeof(value)) ===
"object"
) {
var _Validator = Validator(
value,
validation[key] || {},
messages[key] || {},
true
);
var newIsValid = _Validator.isValid;
var newErrors = _Validator.errors;
if (isValid) {
isValid = newIsValid;
}
errors[key] = newErrors;
} else {
// store validation's current value in a variable
var validations = validation[key];
// store message's current value in a variable
var message = messages[key] || {};
// store validation types in variables
var required = "required";
var email = "email";
var username = "username";
var password = "password";
var numeric = "numeric";
var maxValue = "maxnumber";
var minValue = "minnumbers";
var alphaNumeric = "alphanumeric";
var alpha = "alpha";
var maxlength = "maxlength";
var minlength = "minlength";
var equal = "equal";
var url = "url";
/* validation checks start */
if (validations[required] && (value === "" || !value)) {
// check for undefined or required
errors[key] = message[required] || key + " field is required.";
isValid = false;
} else if (validations[email] && value && !isValidEmail(value)) {
// check for valid email
errors[key] = message[email] || key + " field must be a valid email.";
isValid = false;
} else if (validations[numeric] && value && !isNumeric(value)) {
// check for valid number
errors[key] =
message[numeric] || key + " field can only have numbers.";
isValid = false;
} else if (
validations[alphaNumeric] &&
value &&
!isAlphaNumeric(value)
) {
// check for alphanumeric value
errors[key] =
message[alphaNumeric] ||
key + " field can only have aplhabates and numbers.";
isValid = false;
} else if (validations[alpha] && value && !isAlpha(value)) {
// check for alphabates
errors[key] =
message[alpha] || key + " field can only have aplhabates.";
isValid = false;
} else if (
validations[maxlength] &&
value &&
value.length > validations[maxlength]
) {
// check for maxlength
errors[key] =
message[maxlength] ||
key +
" field can only have " +
validations[maxlength] +
" charaters.";
isValid = false;
} else if (
validations[minlength] &&
value &&
value.length < validations[minlength]
) {
// check for minlength
errors[key] =
message[minlength] ||
key +
" field should have atleast " +
validations[minlength] +
" charaters.";
isValid = false;
} else if (
validations[minValue] &&
value &&
parseFloat(value) < parseFloat(validations[minValue])
) {
// check for min value
errors[key] =
message[minlength] ||
key +
" field should be greater than " +
validations[minValue] +
" charaters.";
isValid = false;
} else if (
validations[maxValue] &&
value &&
parseFloat(value) > parseFloat(validations[maxValue])
) {
// check for max value
errors[key] =
message[maxValue] ||
key +
" field should be less than " +
validations[maxValue] +
" charaters.";
isValid = false;
} else if (
validations[equal] &&
value &&
value !== data[validations[equal]]
) {
// check for equal values
errors[key] =
message[equal] ||
key + " and " + validations[equal] + " field did not matched.";
isValid = false;
} else if (validations[password] && value && !isValidPassword(value)) {
errors[key] =
message[password] ||
key +
" must contain one uppercase, one lowercase, one number and one special character and should be 8 charater long.";
isValid = false;
} else if (validations[username] && value && !isValidUsername(value)) {
errors[key] =
message[username] ||
key + " can only have alphanumeric, _ and . values.";
isValid = false;
} else if (validations[url] && value && !isValidURL(value)) {
errors[key] = message[url] || key + " should be a valid URL.";
isValid = false;
}
/* Validation check ends */
}
}
}
/* returns the object */
return {
isValid: isValid,
errors: errors
};
});
exports.default = Validator;