-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.js
184 lines (152 loc) · 4.47 KB
/
validators.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
var check = require('validator').check
, sanitize = require('validator').sanitize
, User = require('./models').User;
exports.userValidator = function (user, callback) {
console.log("validateUser");
var errors = new Array();
// Username
try {
check(user.username, 'Username empty').notEmpty();
user.username = sanitize(user.username).trim();
} catch (e) {
console.log(e.message); //Please enter a valid integer
errors['username'] = e.message;
}
//Email
try {
check(user.email, 'Email empty').notEmpty();
user.email = sanitize(user.email).trim();
check(user.email, 'Email not valid').isEmail();
} catch (e) {
console.log(e.message); //Please enter a valid integer
errors['email'] = e.message;
}
// Password
try {
check(user.password, 'Password empty').notEmpty();
check(user.password, 'Password must be more than 4 characters').len(4,9999);
} catch (e) {
console.log(e.message); //Please enter a valid integer
errors['password'] = e.message
}
User.findOne()
.$or([{'username': user.username},{'email': user.email}])
.run(function (err, doc) {
console.log("doc: ", doc);
if (doc && doc.username == user.username) {
errors['username'] = "Username already exists";
}
else if (doc && doc.email == user.email) {
errors['email'] = "Email already exists";
}
// console.log("errors: ", errors);
// console.log("callback: ", callback);
callback(errors);
});
}
exports.loginValidator = function (user, callback) {
//console.log("loginUser");
var errors = new Array();
// Username
try {
check(user.username, 'Please enter an username').notEmpty();
user.username = sanitize(user.username).trim();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['username'] = e.message;
}
// Password
try {
check(user.password, 'Please enter a password').notEmpty();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['password'] = e.message
}
callback(errors);
}
// customer validation
exports.customerValidator = function (customer, callback) {
console.log("validate customer");
var errors = new Array();
// customer first name
try {
check(customer.firstName, 'Please enter the customers first name').notEmpty();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['firstName'] = e.message;
}
// customer last name
try {
check(customer.lastName, 'Please enter the customers last name').notEmpty();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['lastName'] = e.message;
}
// customer address
try {
check(customer.address1, 'Please enter the customers address').notEmpty();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['address1'] = e.message;
}
// customer phone
try {
check(customer.tel1a, 'Please enter a valid phone number').notEmpty();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['phone'] = e.message;
}
try {
check(customer.tel1b, 'Please enter a valid phone number').notEmpty();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['phone'] = e.message;
}
try {
check(customer.tel1c, 'Please enter a valid phone number').notEmpty();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['phone'] = e.message;
}
// customer email
try {
check(customer.email, 'Please enter the customers address').notEmpty();
customer.email = sanitize(customer.email).trim();
check(customer.email, 'Email not valid').isEmail();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['email'] = e.message;
}
callback(errors);
}
// Job validator
exports.jobValidator = function (job, callback) {
console.log("validate Job");
var errors = new Array();
// Job name
try {
check(job.name, 'Please enter a name for this job').notEmpty();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['name'] = e.message;
}
// job description
try {
check(job.description, 'Please enter a brief desciption of the job').notEmpty();
}
catch (e) {
console.log(e.message); //Please enter a valid integer
errors['description'] = e.message
}
callback(errors);
}