forked from bufbuild/protoc-gen-validate
/
string.go
170 lines (163 loc) · 5.2 KB
/
string.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
package cc
const strTpl = `
{{ $f := .Field }}{{ $r := .Rules }}
{{ template "const" . }}
{{ template "in" . }}
{{ if or $r.Len (and $r.MinLen $r.MaxLen (eq $r.GetMinLen $r.GetMaxLen)) }}
{{ unimplemented }}
{{ if $r.Len }}
{{/* TODO(akonradi) implement UTF-8 length constraints
if utf8.RuneCountInString({{ accessor . }}) != {{ $r.GetLen }} {
return {{ err . "value length must be " $r.GetLen " runes" }}
}
*/}}
{{ else }}
{{/* TODO(akonradi) implement UTF-8 length constraints
if utf8.RuneCountInString({{ accessor . }}) != {{ $r.GetMinLen }} {
return {{ err . "value length must be " $r.GetMinLen " runes" }}
}
*/}}
{{ end }}
{{ else if $r.MinLen }}
{{ unimplemented }}
{{/* TODO(akonradi) implement UTF-8 length constraints
{{ if $r.MaxLen }}
if l := utf8.RuneCountInString({{ accessor . }}); l < {{ $r.GetMinLen }} || l > {{ $r.GetMaxLen }} {
return {{ err . "value length must be between " $r.GetMinLen " and " $r.GetMaxLen " runes, inclusive" }}
}
{{ else }}
if utf8.RuneCountInString({{ accessor . }}) < {{ $r.GetMinLen }} {
return {{ err . "value length must be at least " $r.GetMinLen " runes" }}
}
{{ end }}
*/}}
{{ else if $r.MaxLen }}
{{ unimplemented }}
{{/* TODO(akonradi) implement UTF-8 length constraints
if utf8.RuneCountInString({{ accessor . }}) > {{ $r.GetMaxLen }} {
return {{ err . "value length must be at most " $r.GetMaxLen " runes" }}
}
*/}}
{{ end }}
{{ if or $r.LenBytes (and $r.MinBytes $r.MaxBytes (eq $r.GetMinBytes $r.GetMaxBytes)) }}
{
const auto length = {{ accessor . }}.size();
{{ if $r.LenBytes }}
if (length != {{ $r.GetLenBytes }}) {
{{ err . "value length must be " $r.GetLenBytes " bytes" }}
}
{{ else }}
if (length != {{ $r.GetMinBytes }}) {
{{ err . "value length must be " $r.GetMinBytes " bytes" }}
}
{{ end }}
}
{{ else if $r.MinBytes }}
{
const auto length = {{ accessor . }}.size();
{{ if $r.MaxBytes }}
{{ if eq $r.GetMinBytes $r.GetMaxBytes }}
if (length != {{ $r.GetMinBytes }}) {
{{ err . "value length must be " $r.GetMinBytes " bytes" }}
}
{{ else }}
if (length < {{ $r.GetMinBytes }} || length > {{ $r.GetMaxBytes }}) {
{{ err . "value length must be between " $r.GetMinBytes " and " $r.GetMaxBytes " bytes, inclusive" }}
}
{{ end }}
{{ else }}
if (length < {{ $r.GetMinBytes }}) {
{{ err . "value length must be at least " $r.GetMinBytes " bytes" }}
}
{{ end }}
}
{{ else if $r.MaxBytes }}
if ({{ accessor . }}.size() > {{ $r.GetMaxBytes }}) {
{{ err . "value length must be at most " $r.GetMaxBytes " bytes" }}
}
{{ end }}
{{ if $r.Prefix }}
{
const std::string prefix = {{ lit $r.GetPrefix }};
if (!pgv::IsPrefix(prefix, {{ accessor . }})) {
{{ err . "value does not have prefix " (lit $r.GetPrefix) }}
}
}
{{ end }}
{{ if $r.Suffix }}
{
const std::string suffix = {{ lit $r.GetSuffix }};
const std::string& value = {{ accessor . }};
if (!pgv::IsSuffix(suffix, value)) {
{{ err . "value does not have suffix " (lit $r.GetSuffix) }}
}
}
{{ end }}
{{ if $r.Contains }}
{
if (!pgv::Contains({{ accessor . }}, {{ lit $r.GetContains }})) {
{{ err . "value does not contain substring " (lit $r.GetContains) }}
}
}
{{ end }}
{{ if $r.GetIp }}
{{ unimplemented }}
{{/* TODO(akonradi) implement IP address constraints
if ip := net.ParseIP({{ accessor . }}); ip == nil {
return {{ err . "value must be a valid IP address" }}
}
*/}}
{{ else if $r.GetIpv4 }}
{{ unimplemented }}
{{/* TODO(akonradi) implement IP address constraints
if ip := net.ParseIP({{ accessor . }}); ip == nil || ip.To4() == nil {
return {{ err . "value must be a valid IPv4 address" }}
}
*/}}
{{ else if $r.GetIpv6 }}
{{ unimplemented }}
{{/* TODO(akonradi) implement IP address constraints
if ip := net.ParseIP({{ accessor . }}); ip == nil || ip.To4() != nil {
return {{ err . "value must be a valid IPv6 address" }}
}
*/}}
{{ else if $r.GetEmail }}
{{ unimplemented }}
{{/* TODO(akonradi) implement email address constraints
if err := m._validateEmail({{ accessor . }}); err != nil {
return {{ errCause . "err" "value must be a valid email address" }}
}
*/}}
{{ else if $r.GetHostname }}
{{ unimplemented }}
{{/* TODO(akonradi) implement hostname constraints
if err := m._validateHostname({{ accessor . }}); err != nil {
return {{ errCause . "err" "value must be a valid hostname" }}
}
*/}}
{{ else if $r.GetUri }}
{{ unimplemented }}
{{/* TODO(akonradi) implement URI constraints
if uri, err := url.Parse({{ accessor . }}); err != nil {
return {{ errCause . "err" "value must be a valid URI" }}
} else if !uri.IsAbs() {
return {{ err . "value must be absolute" }}
}
*/}}
{{ else if $r.GetUriRef }}
{{ unimplemented }}
{{/* TODO(akonradi) implement URI constraints
if _, err := url.Parse({{ accessor . }}); err != nil {
return {{ errCause . "err" "value must be a valid URI" }}
}
*/}}
{{ end }}
{{ if $r.Pattern }}
{{ unimplemented }}
{{/* TODO(akonradi) implement regular expression constraints.
if !{{ lookup $f "Pattern" }}.MatchString({{ accessor . }}) {
return {{ err . "value does not match regex pattern " (lit $r.GetPattern) }}
}
*/}}
{{ end }}
`