-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
validate.go
156 lines (130 loc) · 3.48 KB
/
validate.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
package validate
import (
"encoding/json"
"encoding/xml"
"strings"
"sync"
)
// Errors holds onto all of the error messages
// that get generated during the validation process.
type Errors struct {
Errors map[string][]string `json:"errors" xml:"errors"`
Lock *sync.RWMutex `json:"-"`
}
func (e Errors) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
start.Name = xml.Name{Local: "errors"}
tokens := []xml.Token{start}
for name, messages := range e.Errors {
outer := xml.StartElement{Name: xml.Name{Local: name}}
tks := []xml.Token{outer}
for _, m := range messages {
t := xml.StartElement{Name: xml.Name{Local: "message"}}
tks = append(tks, t, xml.CharData(m), xml.EndElement{Name: xml.Name{Local: "message"}})
}
tokens = append(tokens, tks...)
tokens = append(tokens, xml.EndElement{Name: outer.Name})
}
tokens = append(tokens, xml.EndElement{Name: start.Name})
for _, t := range tokens {
err := enc.EncodeToken(t)
if err != nil {
return err
}
}
// flush to ensure tokens are written
return enc.Flush()
}
// Validator must be implemented in order to pass the
// validator object into the Validate function.
type Validator interface {
IsValid(errors *Errors)
}
type vfWrapper struct {
vf func(errors *Errors)
}
func (v vfWrapper) IsValid(errors *Errors) {
v.vf(errors)
}
// ValidatorFunc wraps any function in a "Validator" to make
// it easy to write custom ones.
func ValidatorFunc(fn func(errors *Errors)) Validator {
return vfWrapper{fn}
}
// NewErrors returns a pointer to a Errors
// object that has been primed and ready to go.
func NewErrors() *Errors {
return &Errors{
Errors: make(map[string][]string),
Lock: new(sync.RWMutex),
}
}
// Error implements the error interface
func (v *Errors) Error() string {
errs := []string{}
for _, v := range v.Errors {
errs = append(errs, v...)
}
return strings.Join(errs, "\n")
}
// Count returns the number of errors.
func (v *Errors) Count() int {
return len(v.Errors)
}
// HasAny returns true/false depending on whether any errors
// have been tracked.
func (v *Errors) HasAny() bool {
if v == nil {
return false
}
return v.Count() > 0
}
// Append concatenates two Errors objects together.
// This will modify the first object in place.
func (v *Errors) Append(ers *Errors) {
for key, value := range ers.Errors {
for _, msg := range value {
v.Add(key, msg)
}
}
}
// Add will add a new message to the list of errors using
// the given key. If the key already exists the message will
// be appended to the array of the existing messages.
func (v *Errors) Add(key string, msg string) {
v.Lock.Lock()
v.Errors[key] = append(v.Errors[key], msg)
v.Lock.Unlock()
}
// Get returns an array of error messages for the given key.
func (v *Errors) Get(key string) []string {
return v.Errors[key]
}
func (v *Errors) String() string {
b, _ := json.Marshal(v)
return string(b)
}
// Keys return all field names which have error
func (v *Errors) Keys() []string {
keys := []string{}
for key := range v.Errors {
keys = append(keys, key)
}
return keys
}
// Validate takes in n number of Validator objects and will run
// them and return back a point to a Errors object that
// will contain any errors.
func Validate(validators ...Validator) *Errors {
errors := NewErrors()
wg := &sync.WaitGroup{}
for i, _ := range validators {
wg.Add(1)
go func(wg *sync.WaitGroup, i int) {
defer wg.Done()
validator := validators[i]
validator.IsValid(errors)
}(wg, i)
}
wg.Wait()
return errors
}