-
Notifications
You must be signed in to change notification settings - Fork 2
/
validation.go
84 lines (67 loc) · 1.41 KB
/
validation.go
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
package validation
type Localizer interface {
Concat([]string, string) string
Format(string, map[string]any) string
FormatCount(string, int, map[string]interface{}) string
T(string) string
}
type Field struct {
Name string
Error FormattableError
}
type List []Field
func With(name string, error FormattableError) List {
return List{Field{Name: name, Error: error}}
}
func (l List) None() bool {
return len(l) == 0
}
func (l List) Any() bool {
return len(l) > 0
}
func (l List) With(name string, error FormattableError) List {
if l.Has(name) {
return l
}
return append(l, Field{Name: name, Error: error})
}
func (l *List) Add(name string, error FormattableError) {
if l.Has(name) {
return
}
*l = append(*l, Field{Name: name, Error: error})
}
func (l List) Has(name string) bool {
for _, field := range l {
if field.Name == name {
return true
}
}
return false
}
func (l List) HasForDate(name, part string) bool {
for _, field := range l {
if field.Name == name {
if err, ok := field.Error.(DateMissingError); ok {
switch part {
case "day":
return err.MissingDay
case "month":
return err.MissingMonth
case "year":
return err.MissingYear
}
}
return true
}
}
return false
}
func (l List) Format(localizer Localizer, name string) string {
for _, field := range l {
if field.Name == name {
return field.Error.Format(localizer)
}
}
return ""
}