-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
176 lines (148 loc) · 3.5 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package validate
import (
"encoding/pem"
"errors"
"fmt"
"net"
"net/mail"
"strings"
"github.com/google/uuid"
)
type Validator interface {
GotenValidate() error
}
type CustomValidator interface {
GotenCustomValidate() error
}
type ValidationError interface {
error
ProtoMessageName() string
FieldName() string
Value() interface{}
ErrorMessage() string
Cause() error
}
type validationError struct {
protoMessageName string
fieldName string
value interface{}
errorMessage string
cause error
}
func NewValidationError(
protoMessageName string, fieldName string,
value interface{}, errorMessage string,
cause error,
) ValidationError {
return &validationError{
protoMessageName, fieldName,
value, errorMessage,
cause,
}
}
func (vErr *validationError) ProtoMessageName() string {
return vErr.protoMessageName
}
func (vErr *validationError) FieldName() string {
return vErr.fieldName
}
func (vErr *validationError) Value() interface{} {
return vErr.value
}
func (vErr *validationError) ErrorMessage() string {
return vErr.errorMessage
}
func (vErr *validationError) Cause() error {
return vErr.cause
}
func trystringer(v interface{}) string {
if sv, ok := v.(fmt.Stringer); ok {
return sv.String()
} else {
return fmt.Sprintf("%#v", v)
}
}
func extractRootCause(verr ValidationError) (fp []string, msg string, value interface{}) {
fp = append(fp, verr.FieldName())
if cause := verr.Cause(); cause == nil {
return fp, verr.ErrorMessage(), verr.Value()
} else {
if cverr, ok := cause.(ValidationError); ok {
cvfp, cmsg, cv := extractRootCause(cverr)
fp = append(fp, cvfp...)
return fp, cmsg, cv
} else {
return fp, cause.Error(), verr.Value()
}
}
}
func (vErr *validationError) Error() string {
fp, msg, v := extractRootCause(vErr)
return fmt.Sprintf("validation error: %s.%s: %s (got: %s)", vErr.protoMessageName, strings.Join(fp, "."), msg, trystringer(v))
}
func ValidateEmail(s string) error {
if s == "" {
return nil
}
a, err := mail.ParseAddress(s)
if err != nil {
return err
}
parts := strings.SplitN(a.Address, "@", 2)
return ValidateHostname(parts[1])
}
func ValidateAddress(s string) error {
if s == "" {
return nil
}
if net.ParseIP(s) == nil {
return nil
}
if err := ValidateHostname(s); err == nil {
return nil
}
return errors.New("address must be either a valid IP, or a valid hostname")
}
func ValidateHostname(s string) error {
if s == "" {
return nil
}
if len(s) > 253 {
return errors.New("hostname cannot exceed 253 characters")
}
s = strings.ToLower(strings.TrimSuffix(s, "."))
for _, part := range strings.Split(s, ".") {
if len(part) == 0 || len(part) > 63 {
return errors.New("hostname part cannot be empty and cannot exceed 63 characters")
}
if part[0] == '-' || part[len(part)-1] == '-' {
return errors.New("hostname part cannot start or end with a hyphen")
}
for _, c := range part {
if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-' {
return fmt.Errorf("hostname part can contain only letters, digits or hyphens, got %q", string(c))
}
}
}
return nil
}
func ValidateUUID(s string) error {
_, err := uuid.Parse(s)
return err
}
func ValidatePEM(s string) error {
var block *pem.Block
rest := []byte(s)
bytesOk := 0
for {
block, rest = pem.Decode([]byte(rest))
if block == nil {
if len(rest) > 0 {
return fmt.Errorf("PEM encoding not satisfied starting at byte %d", bytesOk+1)
} else {
return nil
}
}
bytesOk += len(block.Bytes)
}
}