-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
64 lines (55 loc) · 1.46 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
package forms
type ErrorSite interface {
AddError(err error)
NoteChildError()
Init(parent ErrorSite)
}
type SingleErrorSite struct {
Error error
ParentErrorSite ErrorSite
ErrCount int
}
func (errs *SingleErrorSite) AddError(err error) {
if err != nil && errs.Error == nil {
errs.Error = err
errs.ErrCount++
if errs.ParentErrorSite != nil {
errs.ParentErrorSite.NoteChildError()
}
}
}
func (errs *SingleErrorSite) NoteChildError() {
errs.ErrCount++
if errs.ParentErrorSite != nil {
errs.ParentErrorSite.NoteChildError()
}
}
func (errs *SingleErrorSite) Invalid() bool { return errs.ErrCount > 0 }
func (errs *SingleErrorSite) Init(parent ErrorSite) { errs.ParentErrorSite = parent }
type MultiErrorSite struct {
Errors []error
ParentErrorSite ErrorSite
ErrCount int
CaptureErrors bool
}
func (errs *MultiErrorSite) AddError(err error) {
if err != nil {
errs.ErrCount++
if !errs.CaptureErrors && errs.ParentErrorSite != nil {
errs.ParentErrorSite.AddError(err)
} else {
errs.Errors = append(errs.Errors, err)
if errs.ParentErrorSite != nil {
errs.ParentErrorSite.NoteChildError()
}
}
}
}
func (errs *MultiErrorSite) NoteChildError() {
errs.ErrCount++
if errs.ParentErrorSite != nil {
errs.ParentErrorSite.NoteChildError()
}
}
func (errs *MultiErrorSite) Invalid() bool { return errs.ErrCount > 0 }
func (errs *MultiErrorSite) Init(parent ErrorSite) { errs.ParentErrorSite = parent }