Skip to content

Commit

Permalink
Add rule path for custom field constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
jchadwick-buf committed Nov 25, 2024
1 parent e948b95 commit 5e3ce17
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ $(BIN)/golangci-lint: $(BIN) Makefile
github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)

$(BIN)/protovalidate-conformance: $(BIN) Makefile
# TODO: DO NOT MERGE
GOBIN=$(abspath $(BIN)) $(GO) install \
github.com/bufbuild/protovalidate/tools/protovalidate-conformance@$(CONFORMANCE_VERSION)
github.com/bufbuild/protovalidate/tools/protovalidate-conformance@fab41785ae5758b533d64e40b3f400d84ebbba61

.PHONY: protovalidate-conformance-go
protovalidate-conformance-go: $(BIN)
Expand Down
21 changes: 21 additions & 0 deletions internal/evaluator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@ import (
"github.com/bufbuild/protovalidate-go/internal/errors"
"github.com/bufbuild/protovalidate-go/internal/expression"
"github.com/google/cel-go/cel"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/dynamicpb"
)

//nolint:gochecknoglobals
var celRuleField = validate.FieldPathElement{
FieldName: proto.String("cel"),
FieldNumber: proto.Int32(23),
FieldType: descriptorpb.FieldDescriptorProto_Type(11).Enum(),
}

// Builder is a build-through cache of message evaluators keyed off the provided
// descriptor.
type Builder struct {
Expand Down Expand Up @@ -297,6 +306,18 @@ func (bldr *Builder) processFieldExpressions(
if err != nil {
return err
}
for i := range compiledExpressions {
compiledExpressions[i].Path = []*validate.FieldPathElement{
{
FieldNumber: proto.Int32(celRuleField.GetFieldNumber()),
FieldType: celRuleField.GetFieldType().Enum(),
FieldName: proto.String(celRuleField.GetFieldName()),
Subscript: &validate.FieldPathElement_Index{
Index: uint64(i),
},
},
}
}
if len(compiledExpressions) > 0 {
eval.Constraints = append(eval.Constraints, celPrograms(compiledExpressions))
}
Expand Down

0 comments on commit 5e3ce17

Please sign in to comment.