-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidation.go
250 lines (228 loc) · 6.56 KB
/
validation.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"strings"
"github.com/envoyproxy/protoc-gen-validate/validate"
"github.com/istreamlabs/protoc-gen-huma/annotation"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)
// Validation represents Huma-supported validation rules. These follow JSON
// Schema closely.
type Validation struct {
ReadOnly bool
Deprecated bool
IsRequired bool
// The template system can't determine the difference between nil and 0, so
// we use a boolean to determine if the field was set below.
Minimum float64
HasMinimum bool
ExclusiveMinimum float64
HasExclusiveMinimum bool
Maximum float64
HasMaximum bool
ExclusiveMaximum float64
HasExclusiveMaximum bool
// Note: min/max length and items don't make sense when set to 0, so no need
// for the `HasXXX` booleans like above.
MinLength int64
MaxLengh int64
Pattern string
Format string
MinItems int64
MaxItems int64
Unique bool
EnumValues []string
MultipleOf int32
}
// convertValidation from protoc-gen-validate rules to Huma rules.
func convertValidation(protoField *descriptorpb.FieldDescriptorProto, f *Field) {
if proto.GetExtension(protoField.GetOptions(), annotation.E_ReadOnly).(bool) {
f.Validation.ReadOnly = true
}
if protoField.Options != nil && protoField.Options.Deprecated != nil && *protoField.Options.Deprecated {
f.Validation.Deprecated = true
f.Comment = strings.TrimSpace("Deprecated: Do not use. " + f.Comment)
}
// Default enum settings include all values. This is on each field because
// subsequent validation rules can disable certain enum values for *just*
// this particular field.
if f.Enum != nil {
values := []string{}
for _, v := range f.Enum.Values {
values = append(values, v.Label)
}
f.Validation.EnumValues = values
}
// protoc-gen-validate doesn't support multiple-of but JSON Schema & Huma do,
// so here we use a custom option for that.
if e := proto.GetExtension(protoField.GetOptions(), annotation.E_MultipleOf).(int32); e > 0 {
f.Validation.MultipleOf = e
}
if rules, ok := proto.GetExtension(protoField.GetOptions(), validate.E_Rules).(*validate.FieldRules); ok && rules != nil {
// Message rules, e.g. required fields.
if rules.Message != nil && rules.Message.Required != nil && *rules.Message.Required {
f.Validation.IsRequired = true
}
// Number rules. Each type gets its own struct... ugh. We just support the
// most common ones.
// TODO: add more types?
if i := rules.GetInt32(); i != nil {
if i.Gte != nil {
f.Validation.HasMinimum = true
f.Validation.Minimum = float64(*i.Gte)
}
if i.Gt != nil {
f.Validation.HasExclusiveMinimum = true
f.Validation.ExclusiveMinimum = float64(*i.Gt)
}
if i.Lte != nil {
f.Validation.HasMaximum = true
f.Validation.Maximum = float64(*i.Lte)
}
if i.Lt != nil {
f.Validation.HasExclusiveMaximum = true
f.Validation.ExclusiveMaximum = float64(*i.Lt)
}
}
if i := rules.GetInt64(); i != nil {
if i.Gte != nil {
f.Validation.HasMinimum = true
f.Validation.Minimum = float64(*i.Gte)
}
if i.Gt != nil {
f.Validation.HasExclusiveMinimum = true
f.Validation.ExclusiveMinimum = float64(*i.Gt)
}
if i.Lte != nil {
f.Validation.HasMaximum = true
f.Validation.Maximum = float64(*i.Lte)
}
if i.Lt != nil {
f.Validation.HasExclusiveMaximum = true
f.Validation.ExclusiveMaximum = float64(*i.Lt)
}
}
if i := rules.GetUint32(); i != nil {
if i.Gte != nil {
f.Validation.HasMinimum = true
f.Validation.Minimum = float64(*i.Gte)
}
if i.Gt != nil {
f.Validation.HasExclusiveMinimum = true
f.Validation.ExclusiveMinimum = float64(*i.Gt)
}
if i.Lte != nil {
f.Validation.HasMaximum = true
f.Validation.Maximum = float64(*i.Lte)
}
if i.Lt != nil {
f.Validation.HasExclusiveMaximum = true
f.Validation.ExclusiveMaximum = float64(*i.Lt)
}
}
if i := rules.GetUint64(); i != nil {
if i.Gte != nil {
f.Validation.HasMinimum = true
f.Validation.Minimum = float64(*i.Gte)
}
if i.Gt != nil {
f.Validation.HasExclusiveMinimum = true
f.Validation.ExclusiveMinimum = float64(*i.Gt)
}
if i.Lte != nil {
f.Validation.HasMaximum = true
f.Validation.Maximum = float64(*i.Lte)
}
if i.Lt != nil {
f.Validation.HasExclusiveMaximum = true
f.Validation.ExclusiveMaximum = float64(*i.Lt)
}
}
if i := rules.GetFloat(); i != nil {
if i.Gte != nil {
f.Validation.HasMinimum = true
f.Validation.Minimum = float64(*i.Gte)
}
if i.Gt != nil {
f.Validation.HasExclusiveMinimum = true
f.Validation.ExclusiveMinimum = float64(*i.Gt)
}
if i.Lte != nil {
f.Validation.HasMaximum = true
f.Validation.Maximum = float64(*i.Lte)
}
if i.Lt != nil {
f.Validation.HasExclusiveMaximum = true
f.Validation.ExclusiveMaximum = float64(*i.Lt)
}
}
if i := rules.GetDouble(); i != nil {
if i.Gte != nil {
f.Validation.HasMinimum = true
f.Validation.Minimum = float64(*i.Gte)
}
if i.Gt != nil {
f.Validation.HasExclusiveMinimum = true
f.Validation.ExclusiveMinimum = float64(*i.Gt)
}
if i.Lte != nil {
f.Validation.HasMaximum = true
f.Validation.Maximum = float64(*i.Lte)
}
if i.Lt != nil {
f.Validation.HasExclusiveMaximum = true
f.Validation.ExclusiveMaximum = float64(*i.Lt)
}
}
// String rules, e.g. min/max length and regular expression patterns.
if s := rules.GetString_(); s != nil {
if s.MinLen != nil {
f.Validation.MinLength = int64(*s.MinLen)
}
if s.MaxLen != nil {
f.Validation.MaxLengh = int64(*s.MaxLen)
}
if s.Pattern != nil {
f.Validation.Pattern = *s.Pattern
}
if s.GetUri() {
f.Validation.Format = "uri"
}
if s.GetUriRef() {
f.Validation.Format = "uri-reference"
}
}
// Array rules, e.g. min/max number of items.
if r := rules.GetRepeated(); r != nil {
if r.MinItems != nil {
f.Validation.MinItems = int64(*r.MinItems)
}
if r.MaxItems != nil {
f.Validation.MaxItems = int64(*r.MaxItems)
}
if r.Unique != nil {
f.Validation.Unique = bool(*r.Unique)
}
}
// Enum rules, e.g. filtering allowed values.
if f.Enum != nil {
values := []string{}
notIn := []int32{}
if e := rules.GetEnum(); e != nil {
if e.NotIn != nil {
notIn = e.NotIn
}
}
outer:
for _, v := range f.Enum.Values {
for _, num := range notIn {
if num == v.Value {
continue outer
}
}
values = append(values, v.Label)
}
f.Validation.EnumValues = values
}
}
}