-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidationException.js
56 lines (47 loc) · 1.23 KB
/
validationException.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
const ValidationExceptionContract = require('@ostro/contracts/validation/validationException')
class ValidationException extends ValidationExceptionContract {
constructor() {
super()
this.name = this.constructor.name;
this.message = 'Given data was invalid';
this.errors = {};
this.status = 422;
Error.captureStackTrace(this, this.constructor);
}
add(attribute, message) {
if (!this.has(attribute)) {
this.errors[attribute] = [];
}
if (this.errors[attribute].indexOf(message) === -1) {
this.errors[attribute].push(message);
}
}
getMessage() {
return this.message
}
get(attribute) {
if (this.has(attribute)) {
return this.errors[attribute];
}
return [];
}
getErrors() {
return this.errors
}
first(attribute) {
if (this.has(attribute)) {
return this.errors[attribute][0];
}
return false;
}
all() {
return this.errors;
}
has(attribute) {
if (this.errors.hasOwnProperty(attribute)) {
return true;
}
return false;
}
}
module.exports = ValidationException