-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalidator.go
166 lines (134 loc) · 3.67 KB
/
validator.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
package core
import (
"encoding/json"
"strings"
"fmt"
"strconv"
"github.com/asaskevich/govalidator"
)
func NewValidationResult() ValidationResult {
return &validationResult{
fieldErrors: map[string][]ValidationError{},
unboundErrors: []ValidationError{},
}
}
type ValidationError struct {
Code string `json:"code,omitempty"`
Error string `json:"error"`
}
type ValidationResult interface {
// add field related error
AddFieldError(field string, err error, code ...string) ValidationResult
// add not field related error
AddUnboundError(err error, code ...string) ValidationResult
// returns whether result is valid
IsValid() bool
// marshals result to json
MarshalJSON() ([]byte, error)
// returns whether field has error
HasFieldError(field string) bool
}
/*
Implementation of ValidationResult
*/
type validationResult struct {
fieldErrors map[string][]ValidationError
unboundErrors []ValidationError
}
/*
Adds field error
*/
func (v *validationResult) AddFieldError(field string, err error, code ...string) ValidationResult {
if _, ok := v.fieldErrors[field]; !ok {
v.fieldErrors[field] = []ValidationError{}
}
ve := ValidationError{Error: err.Error()}
if len(code) > 0 {
ve.Code = code[0]
}
v.fieldErrors[field] = append(v.fieldErrors[field], ve)
return v
}
/*
Add unbound error (not related to field)
*/
func (v *validationResult) AddUnboundError(err error, code ...string) ValidationResult {
ve := ValidationError{Error: err.Error()}
if len(code) > 0 {
ve.Code = code[0]
}
v.unboundErrors = append(v.unboundErrors, ve)
return v
}
/*
HasFieldError returns whether any errors are on result
*/
func (v *validationResult) HasFieldError(field string) bool {
if value, ok := v.fieldErrors[field]; !ok {
return false
} else {
return len(value) > 0
}
}
/*
IsValid returns whether any errors are on result
*/
func (v *validationResult) IsValid() bool {
return (len(v.fieldErrors) + len(v.unboundErrors)) == 0
}
/*
MarshalJSON marshals result to json
*/
func (v *validationResult) MarshalJSON() ([]byte, error) {
result := map[string]interface{}{
"fields": v.fieldErrors,
"unbound": v.unboundErrors,
}
return json.Marshal(result)
}
/*
Common validators
*/
/*
StringMinMaxValidator returns validator for length
*/
func StringMinMaxValidator(min int, max int) func(field string, value *string, vr ValidationResult) bool {
minStr := strconv.Itoa(min)
maxStr := strconv.Itoa(max)
return func(field string, value *string, vr ValidationResult) bool {
if !govalidator.StringLength(*value, minStr, maxStr) {
vr.AddFieldError(field, fmt.Errorf("Value must have %v to %v characters", minStr, maxStr))
return false
}
return true
}
}
/*
Validate<field> function helpers
All these methods tah first the pointer to field (so it can manipulate the value e.g. TrimSpace) and also pointer to
ValidationResult to add errors
*/
var (
usernameValidator = StringMinMaxValidator(USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH)
// ValidatePassword validate password
ValidatePassword = StringMinMaxValidator(PASSWORD_MIN_LENGTH, PASSWORD_MAX_LENGTH)
)
/*
ValidateUsername validates username and adds error if available
*/
func ValidateUsername(field string, value *string, vr ValidationResult) bool {
*value = strings.TrimSpace(*value)
return usernameValidator(field, value, vr)
}
/*
ValidateEmail validates whether the value is valid email. Also it supports optional argument required.
*/
func ValidateEmail(field string, value *string, vr ValidationResult, required ...bool) bool {
*value = strings.TrimSpace(*value)
isRequired := IsEnabledOption(required)
// not required not provided
if !isRequired && *value == "" {
return true
}
return govalidator.IsEmail(*value)
}