This repository was archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_string.go
More file actions
146 lines (127 loc) · 3.66 KB
/
validate_string.go
File metadata and controls
146 lines (127 loc) · 3.66 KB
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
package proto_validate_reflect
import (
"fmt"
"net"
"net/mail"
"net/url"
"regexp"
"strings"
"github.com/protoconf/proto-validate-reflect/validate"
"google.golang.org/protobuf/reflect/protoreflect"
)
func ValidateString(value protoreflect.Value, rules *validate.StringRules) (bool, []error) {
s := value.String()
var errors []error
if s == "" && *rules.IgnoreEmpty {
return true, errors
}
if rules.Const != nil && s != *rules.Const {
errors = append(errors, ErrorConst)
}
// Character length checks
charLen := uint64(len([]rune(s)))
if rules.Len != nil && charLen != *rules.Len {
errors = append(errors, ErrorLen)
}
if rules.MinLen != nil && charLen < *rules.MinLen {
errors = append(errors, ErrorMinLen)
}
if rules.MaxLen != nil && charLen > *rules.MaxLen {
errors = append(errors, ErrorMaxLen)
}
// Byte length checks
byteLen := uint64(len(s))
if rules.LenBytes != nil && byteLen != *rules.LenBytes {
errors = append(errors, ErrorLenBytes)
}
if rules.MinBytes != nil && byteLen < *rules.MinBytes {
errors = append(errors, ErrorMinBytes)
}
if rules.MaxBytes != nil && byteLen > *rules.MaxBytes {
errors = append(errors, ErrorMaxBytes)
}
// Pattern checks
if rules.Pattern != nil {
re := regexp.MustCompile(*rules.Pattern)
if !re.MatchString(s) {
errors = append(errors, ErrorPattern)
}
}
// Substring checks
if rules.Prefix != nil && !strings.HasPrefix(s, *rules.Prefix) {
errors = append(errors, ErrorPrefix)
}
if rules.Suffix != nil && !strings.HasSuffix(s, *rules.Suffix) {
errors = append(errors, ErrorSuffix)
}
if rules.Contains != nil && !strings.Contains(s, *rules.Contains) {
errors = append(errors, ErrorContains)
}
if rules.NotContains != nil && strings.Contains(s, *rules.NotContains) {
errors = append(errors, ErrorNotContains)
}
// Membership checks
if len(rules.In) > 0 && !stringInSlice(s, rules.In) {
errors = append(errors, ErrorIn)
}
if len(rules.NotIn) > 0 && stringInSlice(s, rules.NotIn) {
errors = append(errors, ErrorNotIn)
}
// Well-known format checks
switch rules.WellKnown.(type) {
case *validate.StringRules_Email:
if _, err := mail.ParseAddress(s); err != nil {
errors = append(errors, ErrorInvalidEmail)
}
case *validate.StringRules_Hostname:
host := strings.ToLower(strings.TrimSuffix(s, "."))
if len(host) > 253 {
errors = append(errors, ErrorHostnameTooLong)
}
for _, part := range strings.Split(s, ".") {
if l := len(part); l == 0 || l > 63 {
errors = append(errors, ErrorHostnameInvalidPart)
continue
}
if part[0] == '-' {
errors = append(errors, ErrorHostnamePartBeginWithHyphens)
}
if part[len(part)-1] == '-' {
errors = append(errors, ErrorHostnamePartEndsWithHyphens)
}
for _, r := range part {
if (r < 'a' || r > 'z') && (r < '0' || r > '9') && r != '-' {
errors = append(errors,
fmt.Errorf("hostname parts can only contain alphanumeric characters or hyphens, got %q", string(r)),
)
}
}
}
case *validate.StringRules_Ip:
if net.ParseIP(s) == nil {
errors = append(errors, ErrorInvalidIp)
}
case *validate.StringRules_Ipv4:
if net.ParseIP(s).To4() == nil {
errors = append(errors, ErrorInvalidIpv4)
}
case *validate.StringRules_Ipv6:
if net.ParseIP(s).To16() == nil {
errors = append(errors, ErrorInvalidIpv6)
}
case *validate.StringRules_Uri:
if _, err := url.ParseRequestURI(s); err != nil {
errors = append(errors, ErrorInvalidUri)
}
//... Handle other rules like UriRef, Address, Uuid, WellKnownRegex, Strict here
}
return len(errors) == 0, errors
}
func stringInSlice(s string, slice []string) bool {
for _, v := range slice {
if v == s {
return true
}
}
return false
}