1
1
package protovalidate
2
2
3
3
import (
4
+ "log/slog"
4
5
"strconv"
5
6
"strings"
6
7
7
8
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
8
- "github.com/bufbuild/protovalidate- go/resolve "
9
+ "buf.build/ go/protovalidate "
9
10
"github.com/pb33f/libopenapi/datamodel/high/base"
10
11
"github.com/pb33f/libopenapi/utils"
11
- "google.golang.org/protobuf/reflect/protoreflect"
12
- "gopkg.in/yaml.v3"
13
-
14
12
"github.com/pubgo/protoc-gen-openapi/internal/converter/options"
15
13
"github.com/pubgo/protoc-gen-openapi/internal/converter/util"
14
+ "google.golang.org/protobuf/reflect/protoreflect"
15
+ "gopkg.in/yaml.v3"
16
16
)
17
17
18
18
func SchemaWithMessageAnnotations (opts options.Options , schema * base.Schema , desc protoreflect.MessageDescriptor ) * base.Schema {
19
- constraints := resolve .MessageConstraints (desc )
19
+ constraints , err := protovalidate .ResolveMessageRules (desc )
20
+ if err != nil {
21
+ slog .Warn ("unable to resolve message rules" , slog .Any ("error" , err ))
22
+ return schema
23
+ }
20
24
if constraints == nil || constraints .GetDisabled () {
21
25
return schema
22
26
}
@@ -25,7 +29,11 @@ func SchemaWithMessageAnnotations(opts options.Options, schema *base.Schema, des
25
29
}
26
30
27
31
func SchemaWithFieldAnnotations (opts options.Options , schema * base.Schema , desc protoreflect.FieldDescriptor , onlyScalar bool ) * base.Schema {
28
- constraints := resolve .FieldConstraints (desc )
32
+ constraints , err := protovalidate .ResolveFieldRules (desc )
33
+ if err != nil {
34
+ slog .Warn ("unable to resolve field rules" , slog .Any ("error" , err ))
35
+ return schema
36
+ }
29
37
if constraints == nil {
30
38
return schema
31
39
}
@@ -36,15 +44,19 @@ func SchemaWithFieldAnnotations(opts options.Options, schema *base.Schema, desc
36
44
parent .Required = util .AppendStringDedupe (parent .Required , util .MakeFieldName (opts , desc ))
37
45
}
38
46
}
39
- updateSchemaWithFieldConstraints (schema , constraints , onlyScalar )
47
+ updateSchemaWithFieldRules (schema , constraints , onlyScalar )
40
48
return schema
41
49
}
42
50
43
51
func PopulateParentProperties (opts options.Options , parent * base.Schema , desc protoreflect.FieldDescriptor ) * base.Schema {
44
52
if parent == nil {
45
53
return parent
46
54
}
47
- constraints := resolve .FieldConstraints (desc )
55
+ constraints , err := protovalidate .ResolveFieldRules (desc )
56
+ if err != nil {
57
+ slog .Warn ("unable to resolve field rules" , slog .Any ("error" , err ))
58
+ return parent
59
+ }
48
60
if constraints == nil {
49
61
return parent
50
62
}
@@ -55,62 +67,62 @@ func PopulateParentProperties(opts options.Options, parent *base.Schema, desc pr
55
67
}
56
68
57
69
//gocyclo:ignore
58
- func updateSchemaWithFieldConstraints (schema * base.Schema , constraints * validate.FieldConstraints , onlyScalar bool ) {
70
+ func updateSchemaWithFieldRules (schema * base.Schema , constraints * validate.FieldRules , onlyScalar bool ) {
59
71
if constraints == nil {
60
72
return
61
73
}
62
74
switch t := constraints .Type .(type ) {
63
- case * validate.FieldConstraints_Float :
75
+ case * validate.FieldRules_Float :
64
76
updateSchemaFloat (schema , t .Float )
65
- case * validate.FieldConstraints_Double :
77
+ case * validate.FieldRules_Double :
66
78
updateSchemaDouble (schema , t .Double )
67
- case * validate.FieldConstraints_Int32 :
79
+ case * validate.FieldRules_Int32 :
68
80
updateSchemaInt32 (schema , t .Int32 )
69
- case * validate.FieldConstraints_Int64 :
81
+ case * validate.FieldRules_Int64 :
70
82
updateSchemaInt64 (schema , t .Int64 )
71
- case * validate.FieldConstraints_Uint32 :
83
+ case * validate.FieldRules_Uint32 :
72
84
updateSchemaUint32 (schema , t .Uint32 )
73
- case * validate.FieldConstraints_Uint64 :
85
+ case * validate.FieldRules_Uint64 :
74
86
updateSchemaUint64 (schema , t .Uint64 )
75
- case * validate.FieldConstraints_Sint32 :
87
+ case * validate.FieldRules_Sint32 :
76
88
updateSchemaSint32 (schema , t .Sint32 )
77
- case * validate.FieldConstraints_Sint64 :
89
+ case * validate.FieldRules_Sint64 :
78
90
updateSchemaSint64 (schema , t .Sint64 )
79
- case * validate.FieldConstraints_Fixed32 :
91
+ case * validate.FieldRules_Fixed32 :
80
92
updateSchemaFixed32 (schema , t .Fixed32 )
81
- case * validate.FieldConstraints_Fixed64 :
93
+ case * validate.FieldRules_Fixed64 :
82
94
updateSchemaFixed64 (schema , t .Fixed64 )
83
- case * validate.FieldConstraints_Sfixed32 :
95
+ case * validate.FieldRules_Sfixed32 :
84
96
updateSchemaSfixed32 (schema , t .Sfixed32 )
85
- case * validate.FieldConstraints_Sfixed64 :
97
+ case * validate.FieldRules_Sfixed64 :
86
98
updateSchemaSfixed64 (schema , t .Sfixed64 )
87
- case * validate.FieldConstraints_Bool :
99
+ case * validate.FieldRules_Bool :
88
100
updateSchemaBool (schema , t .Bool )
89
- case * validate.FieldConstraints_String_ :
101
+ case * validate.FieldRules_String_ :
90
102
updateSchemaString (schema , t .String_ )
91
- case * validate.FieldConstraints_Bytes :
103
+ case * validate.FieldRules_Bytes :
92
104
updateSchemaBytes (schema , t .Bytes )
93
- case * validate.FieldConstraints_Enum :
105
+ case * validate.FieldRules_Enum :
94
106
updateSchemaEnum (schema , t .Enum )
95
- case * validate.FieldConstraints_Any :
107
+ case * validate.FieldRules_Any :
96
108
updateSchemaAny (schema , t .Any )
97
- case * validate.FieldConstraints_Duration :
109
+ case * validate.FieldRules_Duration :
98
110
updateSchemaDuration (schema , t .Duration )
99
- case * validate.FieldConstraints_Timestamp :
111
+ case * validate.FieldRules_Timestamp :
100
112
updateSchemaTimestamp (schema , t .Timestamp )
101
113
}
102
114
103
115
if ! onlyScalar {
104
116
switch t := constraints .Type .(type ) {
105
- case * validate.FieldConstraints_Repeated :
117
+ case * validate.FieldRules_Repeated :
106
118
updateSchemaRepeated (schema , t .Repeated )
107
- case * validate.FieldConstraints_Map :
119
+ case * validate.FieldRules_Map :
108
120
updateSchemaMap (schema , t .Map )
109
121
}
110
122
}
111
123
}
112
124
113
- func updateWithCEL (schema * base.Schema , constraints []* validate.Constraint ) {
125
+ func updateWithCEL (schema * base.Schema , constraints []* validate.Rule ) {
114
126
if len (constraints ) == 0 {
115
127
return
116
128
}
@@ -178,18 +190,18 @@ func updateSchemaDouble(schema *base.Schema, constraint *validate.DoubleRules) {
178
190
}
179
191
switch tt := constraint .LessThan .(type ) {
180
192
case * validate.DoubleRules_Lt :
181
- v := tt .Lt
193
+ v := float64 ( tt .Lt )
182
194
schema .ExclusiveMaximum = & base.DynamicValue [bool , float64 ]{N : 1 , B : v }
183
195
case * validate.DoubleRules_Lte :
184
- v := tt .Lte
196
+ v := float64 ( tt .Lte )
185
197
schema .Maximum = & v
186
198
}
187
199
switch tt := constraint .GreaterThan .(type ) {
188
200
case * validate.DoubleRules_Gt :
189
- v := tt .Gt
201
+ v := float64 ( tt .Gt )
190
202
schema .ExclusiveMinimum = & base.DynamicValue [bool , float64 ]{N : 1 , B : v }
191
203
case * validate.DoubleRules_Gte :
192
- v := tt .Gte
204
+ v := float64 ( tt .Gte )
193
205
schema .Minimum = & v
194
206
}
195
207
if len (constraint .In ) > 0 {
@@ -801,7 +813,7 @@ func updateSchemaRepeated(schema *base.Schema, constraint *validate.RepeatedRule
801
813
schema .MaxItems = & v
802
814
}
803
815
if constraint .Items != nil && schema .Items != nil && schema .Items .A != nil && ! schema .Items .A .IsReference () {
804
- updateSchemaWithFieldConstraints (schema .Items .A .Schema (), constraint .Items , false )
816
+ updateSchemaWithFieldRules (schema .Items .A .Schema (), constraint .Items , false )
805
817
}
806
818
}
807
819
@@ -815,9 +827,9 @@ func updateSchemaMap(schema *base.Schema, constraint *validate.MapRules) {
815
827
schema .MaxProperties = & v
816
828
}
817
829
// NOTE: Most of these properties don't make sense for object keys
818
- // updateSchemaWithFieldConstraints (schema, constraint.Keys)
830
+ // updateSchemaWithFieldRules (schema, constraint.Keys)
819
831
if schema .AdditionalProperties != nil && constraint .Values != nil {
820
- updateSchemaWithFieldConstraints (schema .AdditionalProperties .A .Schema (), constraint .Values , false )
832
+ updateSchemaWithFieldRules (schema .AdditionalProperties .A .Schema (), constraint .Values , false )
821
833
}
822
834
}
823
835
0 commit comments