-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidator.js
More file actions
53 lines (43 loc) · 1.03 KB
/
Copy pathValidator.js
File metadata and controls
53 lines (43 loc) · 1.03 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
const FieldValidator = require('./FieldValidator');
class Validator {
constructor(input, rules) {
this.valid = null;
this.errors = [];
this.fieldErrors = [];
this.reverse = false;
this.input = input;
this.rules = rules;
}
validate() {
let valid = true;
this.errors = [];
for (const field in this.rules) {
const fieldValidator = new FieldValidator(field, this.input[field], this.rules[field], this.input);
if (!fieldValidator.validate() && !this.reverse) {
valid = false;
this.errors.push(fieldValidator.error);
this.fieldErrors.push
(
{
field: field,
error: fieldValidator.fieldError
}
);
} else if (this.reverse && fieldValidator.validate()) { // This is pretty much just here for testing //
valid = false;
this.errors.push(field + ' is valid');
this.fieldErrors.push
(
{
field: field,
error: 'Is valid'
}
);
}
}
this.valid = valid;
return valid;
}
}
Validator.FieldValidator = FieldValidator;
module.exports = Validator;