-
Notifications
You must be signed in to change notification settings - Fork 4
/
validate.go
173 lines (152 loc) · 4.97 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
package generator
import (
"log"
"github.com/envoyproxy/protoc-gen-validate/validate"
v3 "github.com/google/gnostic/openapiv3"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
func (g *OpenAPIv3Generator) addValidationRules(fieldSchema *v3.SchemaOrReference, field protoreflect.FieldDescriptor) {
validationRules := proto.GetExtension(field.Options(), validate.E_Rules)
if validationRules == nil {
return
}
fieldRules, ok := validationRules.(*validate.FieldRules)
if !ok {
return
}
schema, ok := fieldSchema.Oneof.(*v3.SchemaOrReference_Schema)
if !ok {
return
}
//TODO: implement map validation
if field.IsMap() {
return
}
if field.IsList() {
repeatedRules := fieldRules.GetRepeated()
if repeatedRules == nil {
// no rules
return
}
// MinItems specifies that this field must have the specified number of
// items at a minimum
// MaxItems specifies that this field must have the specified number of
// items at a maximum
// Unique specifies that all elements in this field must be unique. This
// contraint is only applicable to scalar and enum types (messages are not
// supported).
// Items specifies the contraints to be applied to each item in the field.
// Repeated message fields will still execute validation against each item
// unless skip is specified here.
// IgnoreEmpty specifies that the validation rules of this field should be
// evaluated only if the field is not empty
if repeatedRules.MinItems != nil {
schema.Schema.MinItems = int64(*repeatedRules.MinItems)
}
if repeatedRules.MaxItems != nil {
schema.Schema.MaxItems = int64(*repeatedRules.MaxItems)
}
// pull out the array items field rules
fieldRules := repeatedRules.Items
if fieldRules == nil {
// no item specific rules
return
}
schema := schema.Schema.Items.SchemaOrReference[0]
fieldRule(fieldRules, field, schema.Oneof.(*v3.SchemaOrReference_Schema))
log.Printf("(TODO) Unsupported field type: list.")
return
}
fieldRule(fieldRules, field, schema)
}
func fieldRule(fieldRules *validate.FieldRules, field protoreflect.FieldDescriptor, schema *v3.SchemaOrReference_Schema) {
kind := field.Kind()
switch kind {
case protoreflect.MessageKind:
return // TO DO: Implement message validators from protoc-gen-validate
case protoreflect.StringKind:
stringRules := fieldRules.GetString_()
if stringRules == nil {
break
}
// Set Format
// format is an open value, so you can use any formats, even not those defined by the OpenAPI Specification
if stringRules.GetEmail() {
schema.Schema.Format = "email"
} else if stringRules.GetHostname() {
schema.Schema.Format = "hostname"
} else if stringRules.GetIp() {
schema.Schema.Format = "ip"
} else if stringRules.GetIpv4() {
schema.Schema.Format = "ipv4"
} else if stringRules.GetIpv6() {
schema.Schema.Format = "ipv6"
} else if stringRules.GetUri() {
schema.Schema.Format = "uri"
} else if stringRules.GetUriRef() {
schema.Schema.Format = "uri_ref"
} else if stringRules.GetUuid() {
schema.Schema.Format = "uuid"
}
// Set min/max
if stringRules.GetMinLen() > 0 {
schema.Schema.MinLength = int64(stringRules.GetMinLen())
}
if stringRules.GetMaxLen() > 0 {
schema.Schema.MaxLength = int64(stringRules.GetMaxLen())
}
// Set Pattern
if stringRules.GetPattern() != "" {
schema.Schema.Pattern = stringRules.GetPattern()
}
case protoreflect.Int32Kind:
int32Rules := fieldRules.GetInt32()
if int32Rules == nil {
break
}
if int32Rules.GetGte() > 0 {
schema.Schema.Minimum = float64(int32Rules.GetGte())
}
if int32Rules.GetLte() > 0 {
schema.Schema.Maximum = float64(int32Rules.GetLte())
}
case protoreflect.Int64Kind:
int64Rules := fieldRules.GetInt64()
if int64Rules == nil {
break
}
if int64Rules.GetGte() > 0 {
schema.Schema.Minimum = float64(int64Rules.GetGte())
}
if int64Rules.GetLte() > 0 {
schema.Schema.Maximum = float64(int64Rules.GetLte())
}
case protoreflect.EnumKind:
enumRules := fieldRules.GetEnum()
if enumRules == nil {
break
}
var validEnums []int32
if enumRules.Const != nil {
validEnums = enumValues(field, true)
} else if enumRules.In != nil {
validEnums = enumRules.In
} else if enumRules.NotIn != nil {
validEnums = remove(enumValues(field, false), enumRules.NotIn...)
}
// we don't check enumRules.DefinedOnly because we already list the set of valid enums
list := enumsToV3Any(field, validEnums...)
schema.Schema.Enum = list
//TODO: implement protoc-gen-validate rules for the following types
case protoreflect.Sint32Kind, protoreflect.Uint32Kind,
protoreflect.Sint64Kind, protoreflect.Uint64Kind,
protoreflect.Sfixed32Kind, protoreflect.Fixed32Kind, protoreflect.Sfixed64Kind,
protoreflect.Fixed64Kind:
case protoreflect.BoolKind:
case protoreflect.FloatKind, protoreflect.DoubleKind:
case protoreflect.BytesKind:
default:
log.Printf("(TODO) Unsupported field type: %+v", fullMessageTypeName(field.Message()))
}
}