-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.go
179 lines (146 loc) · 3.38 KB
/
error.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
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
package validation
import (
"strings"
"unicode"
"unicode/utf8"
)
type FormattableError interface {
Format(Localizer) string
}
type CustomError struct {
Label string
}
func (e CustomError) Format(l Localizer) string {
return l.T(e.Label)
}
type FileError struct {
Label string
Filename string
}
func (e FileError) Format(l Localizer) string {
return l.Format(e.Label, map[string]any{
"Filename": e.Filename,
})
}
type FilesInfectedError struct {
Label string
Filenames []string
}
func (e FilesInfectedError) Format(l Localizer) string {
joinedFilenames := l.Concat(e.Filenames, l.T("and"))
return l.FormatCount("errorFilesInfected", len(e.Filenames), map[string]interface{}{
"Filenames": joinedFilenames,
})
}
type SelectError struct {
Label string
}
func (e SelectError) Format(l Localizer) string {
return l.Format("errorSelect", map[string]any{
"Label": lowerFirst(l.T(e.Label)),
})
}
type EnterError struct {
Label string
}
func (e EnterError) Format(l Localizer) string {
return l.Format("errorEnter", map[string]any{
"Label": lowerFirst(l.T(e.Label)),
})
}
type StringTooLongError struct {
Label string
Length int
}
func (e StringTooLongError) Format(l Localizer) string {
return l.Format("errorStringTooLong", map[string]any{
"Label": l.T(e.Label),
"Length": e.Length,
})
}
type StringLengthError struct {
Label string
Length int
}
func (e StringLengthError) Format(l Localizer) string {
return l.Format("errorStringLength", map[string]any{
"Label": l.T(e.Label),
"Length": e.Length,
})
}
type PhoneError struct {
Tmpl string
Label string
}
func (e PhoneError) Format(l Localizer) string {
return l.Format(e.Tmpl, map[string]any{
"Label": l.T(e.Label),
})
}
type PostcodeError struct {
Label string
}
func (e PostcodeError) Format(l Localizer) string {
return l.Format("errorPostcode", map[string]any{
"Label": l.T(e.Label),
})
}
type EmailError struct {
Label string
}
func (e EmailError) Format(l Localizer) string {
return l.Format("errorEmail", map[string]any{
"Label": l.T(e.Label),
})
}
type DateMissingError struct {
Label string
// need to highlight the correct fields, if all then EnterError should be used
MissingDay bool
MissingMonth bool
MissingYear bool
}
func (e DateMissingError) Format(l Localizer) string {
var missing []string
if e.MissingDay {
missing = append(missing, lowerFirst(l.T("day")))
}
if e.MissingMonth {
missing = append(missing, lowerFirst(l.T("month")))
}
if e.MissingYear {
missing = append(missing, lowerFirst(l.T("year")))
}
return l.Format("errorDateMissing", map[string]any{
"Label": l.T(e.Label),
"Missing": l.T("a") + " " + strings.Join(missing, " "+l.T("and")+" "),
})
}
type DateMustBeRealError struct {
Label string
}
func (e DateMustBeRealError) Format(l Localizer) string {
return l.Format("errorDateMustBeReal", map[string]any{
"Label": l.T(e.Label),
})
}
type DateMustBePastError struct {
Label string
}
func (e DateMustBePastError) Format(l Localizer) string {
return l.Format("errorDateMustBePast", map[string]any{
"Label": l.T(e.Label),
})
}
type ReferenceNumberError struct {
Label string
}
func (e ReferenceNumberError) Format(l Localizer) string {
return l.Format("errorReferenceNumber", map[string]any{
"Label": l.T(e.Label),
})
}
func lowerFirst(s string) string {
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToLower(r)) + s[n:]
}