Skip to content

Commit

Permalink
fix loading type when transitive dependency is more than one step
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversun9 committed Sep 28, 2023
1 parent bfb9d45 commit c6f9ec2
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 25 deletions.
1 change: 1 addition & 0 deletions buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ managed:
except:
- buf.build/envoyproxy/protoc-gen-validate
- buf.build/bufbuild/protovalidate
- buf.build/googleapis/googleapis
plugins:
- plugin: buf.build/protocolbuffers/go:v1.31.0
out: internal/gen
Expand Down
2 changes: 1 addition & 1 deletion internal/evaluator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (bldr *Builder) processFieldExpressions(
var opts []cel.EnvOption
if fieldDesc.Kind() == protoreflect.MessageKind {
opts = []cel.EnvOption{
cel.TypeDescs(fieldDesc.Message().ParentFile()),
cel.Types(dynamicpb.NewMessage(fieldDesc.Message())),
cel.Variable("this", cel.ObjectType(string(fieldDesc.Message().FullName()))),
}
} else {
Expand Down
127 changes: 103 additions & 24 deletions internal/gen/tests/example/v1/validations.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ deps:
repository: protoc-gen-validate
commit: eac44469a7af47e7839a7f1f3d7ac004
digest: shake256:0feabcde01b6b11e3c75a5e3f807968d5995626546f39c37e5d4205892b3a59cced0ed83b35a2eb9e6dddd3309660ad46b737c9dcd224b425de0a6654ce04417
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 28151c0d0a1641bf938a7672c500e01d
digest: shake256:49215edf8ef57f7863004539deff8834cfb2195113f0b890dd1f67815d9353e28e668019165b9d872395871eeafcbab3ccfdb2b5f11734d3cca95be9e8d139de
1 change: 1 addition & 0 deletions proto/buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ deps:
- buf.build/bufbuild/protovalidate
- buf.build/bufbuild/protovalidate-testing
- buf.build/envoyproxy/protoc-gen-validate
- buf.build/googleapis/googleapis
breaking:
use:
- FILE
Expand Down
9 changes: 9 additions & 0 deletions proto/tests/example/v1/validations.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ syntax = "proto3";
package tests.example.v1;

import "buf/validate/validate.proto";
import "google/geo/type/viewport.proto";
import "google/protobuf/field_mask.proto";

message HasMsgExprs {
Expand Down Expand Up @@ -131,3 +132,11 @@ message TransitiveFieldConstraint {
expression: "has(this.paths)",
}];
}

message MultipleStepsTransitiveFieldConstraints {
google.geo.type.Viewport viewport = 1 [(buf.validate.field).cel = {
id: "viewport.low.latitude",
message: "viewport's low point's latitude must be greater than 10.0 degrees",
expression: "this.low.latitude > 10.0"
}];
}
12 changes: 12 additions & 0 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Tests:
- basic
- on repeated message field
- field message defined in this file
- field message defined in an imported file
- field message is wkt
- sub field rules has CEL
- the mesasge field has validation in its message definition
- the sub field has subfield has subfield, as in both this.foo.bar > 0 and foo.baz > 0 are defined
- top level defining two levels deep, equivalent to this.foo.bar.baz > 0
- define sub field rules on a scalar type should fail

17 changes: 17 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
pb "github.com/bufbuild/protovalidate-go/internal/gen/tests/example/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/genproto/googleapis/geo/type/viewport"
"google.golang.org/genproto/googleapis/type/latlng"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)

Expand Down Expand Up @@ -135,3 +137,18 @@ func TestValidator_Validate_TransitiveFieldConstraints(t *testing.T) {
err = val.Validate(msg)
require.NoError(t, err)
}

func TestValidator_Validate_MultipleStepsTransitiveFieldConstraints(t *testing.T) {
t.Parallel()
val, err := New()
require.NoError(t, err)
msg := &pb.MultipleStepsTransitiveFieldConstraints{
Viewport: &viewport.Viewport{
Low: &latlng.LatLng{
Latitude: 30.0,
},
},
}
err = val.Validate(msg)
require.NoError(t, err)
}

0 comments on commit c6f9ec2

Please sign in to comment.