-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (45 loc) · 976 Bytes
/
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
import _ from 'lodash';
export default class FormErrors {
constructor() {
this.bag = {};
}
record(errors) {
this.bag = {};
if (Array.isArray(errors)) {
errors.forEach(error => {
if (error.key) {
this.bag[_.camelCase(error.key)] = error.detail;
}
});
} else {
for (const [key, value] of Object.entries(errors)) {
let message = '';
if (Array.isArray(value)) {
message = value.join(' ');
} else {
message = value;
}
this.bag[_.camelCase(key)] = message;
}
}
}
get(field) {
if (this.bag[_.camelCase(field)]) {
return this.bag[_.camelCase(field)];
}
return null;
}
has(field) {
return this.bag.hasOwnProperty(_.camelCase(field));
}
clear(field) {
if (field) {
delete this.bag[field];
return;
}
this.bag = {};
}
any() {
return Object.keys(this.bag).length > 0;
}
}