Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ GOLANGCI_LINT_VERSION ?= v2.4.0
# Set to use a different version of protovalidate-conformance.
# Should be kept in sync with the version referenced in buf.yaml and
# 'buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go' in go.mod.
CONFORMANCE_VERSION ?= v1.0.0
CONFORMANCE_VERSION ?= v1.1.0

.PHONY: help
help: ## Describe useful make targets
Expand Down
20 changes: 7 additions & 13 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"slices"

"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
pvcel "buf.build/go/protovalidate/cel"
"github.com/google/cel-go/cel"
"google.golang.org/protobuf/reflect/protoreflect"
)
Expand Down Expand Up @@ -90,6 +89,7 @@ func (set astSet) ReduceResiduals(rules protoreflect.Message, opts ...cel.Progra
residuals = append(residuals, compiledAST{
AST: residual,
Env: ast.Env,
Rules: ast.Rules,
Source: ast.Source,
Path: ast.Path,
Value: ast.Value,
Expand All @@ -116,23 +116,15 @@ func (set astSet) ToProgramSet(opts ...cel.ProgramOption) (out programSet, err e
return out, nil
}

// SetRuleValue sets the rule value for the programs in the ASTSet.
func (set astSet) WithRuleValue(
// SetRuleValue sets the rule and rules value for the programs in the ASTSet.
func (set astSet) WithRuleValues(
rules protoreflect.Message,
ruleValue protoreflect.Value,
ruleDescriptor protoreflect.FieldDescriptor,
) (out astSet, err error) {
out = slices.Clone(set)
for i := range set {
out[i].Env, err = out[i].Env.Extend(
cel.Constant(
"rule",
pvcel.ProtoFieldToType(ruleDescriptor, true, false),
pvcel.ProtoFieldToValue(ruleDescriptor, ruleValue, false),
),
)
Comment on lines -126 to -132
Copy link
Member Author

@srikrsna-buf srikrsna-buf Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This never worked for message fields. The ProtoFieldToValue is using the default adapter of cel-go that doesn't handle protobuf messages except for wrappers. This always returned an error value.

Because we try to reduce the AST ahead of time and the rule variable was always being optimized away, we never hit a problem.

if err != nil {
return nil, err
}
out[i].Rules = rules
out[i].Value = ruleValue
out[i].Descriptor = ruleDescriptor
}
Expand All @@ -142,6 +134,7 @@ func (set astSet) WithRuleValue(
type compiledAST struct {
AST *cel.Ast
Env *cel.Env
Rules protoreflect.Message
Source *validate.Rule
Path []*validate.FieldPathElement
Value protoreflect.Value
Expand All @@ -155,6 +148,7 @@ func (ast compiledAST) toProgram(env *cel.Env, opts ...cel.ProgramOption) (out c
}
return compiledProgram{
Program: prog,
Rules: ast.Rules,
Source: ast.Source,
Path: ast.Path,
Value: ast.Value,
Expand Down
69 changes: 51 additions & 18 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import (

//nolint:gochecknoglobals
var (
celRuleDescriptor = (&validate.FieldRules{}).ProtoReflect().Descriptor().Fields().ByName("cel")
celRuleField = fieldPathElement(celRuleDescriptor)
celExpressionDescriptor = (&validate.FieldRules{}).ProtoReflect().Descriptor().Fields().ByName("cel_expression")
celExpressionField = fieldPathElement(celExpressionDescriptor)
celRuleDescriptor = (&validate.FieldRules{}).ProtoReflect().Descriptor().Fields().ByName("cel")
celRuleField = fieldPathElement(celRuleDescriptor)
)

// builder is a build-through cache of message evaluators keyed off the provided
Expand Down Expand Up @@ -149,7 +151,7 @@ func (bldr *builder) processMessageExpressions(
_ messageCache,
) {
exprs := expressions{
Rules: msgRules.GetCel(),
Rules: append(expressionsToRules(msgRules.GetCelExpression()), msgRules.GetCel()...),
}
compiledExprs, err := compile(
exprs,
Expand Down Expand Up @@ -323,30 +325,50 @@ func (bldr *builder) processFieldExpressions(
eval *value,
_ messageCache,
) error {
exprs := expressions{
Rules: fieldRules.GetCel(),
}

celTyp := pvcel.ProtoFieldToType(fieldDesc, false, eval.NestedRule != nil)
opts := append(
pvcel.RequiredEnvOptions(fieldDesc),
cel.Variable("this", celTyp),
)
compiledExpressions, err := compile(exprs, bldr.env, opts...)
compileWithPath := func(exprs expressions, fieldPathElement *validate.FieldPathElement, descriptor protoreflect.FieldDescriptor) (programSet, error) {
compiledExpressions, err := compile(exprs, bldr.env, opts...)
if err != nil {
return nil, err
}
for i := range compiledExpressions {
compiledExpressions[i].Path = []*validate.FieldPathElement{
validate.FieldPathElement_builder{
FieldNumber: proto.Int32(fieldPathElement.GetFieldNumber()),
FieldType: fieldPathElement.GetFieldType().Enum(),
FieldName: proto.String(fieldPathElement.GetFieldName()),
Index: proto.Uint64(uint64(i)), //nolint:gosec // indices are guaranteed to be non-negative
}.Build(),
}
compiledExpressions[i].Descriptor = descriptor
}
return compiledExpressions, nil
}
compiledExpressions, err := compileWithPath(
expressions{
Rules: expressionsToRules(fieldRules.GetCelExpression()),
},
celExpressionField,
celExpressionDescriptor,
)
if err != nil {
return err
}
for i := range compiledExpressions {
compiledExpressions[i].Path = []*validate.FieldPathElement{
validate.FieldPathElement_builder{
FieldNumber: proto.Int32(celRuleField.GetFieldNumber()),
FieldType: celRuleField.GetFieldType().Enum(),
FieldName: proto.String(celRuleField.GetFieldName()),
Index: proto.Uint64(uint64(i)), //nolint:gosec // indices are guaranteed to be non-negative
}.Build(),
}
compiledExpressions[i].Descriptor = celRuleDescriptor
celRuleCompiledExpressions, err := compileWithPath(
expressions{
Rules: fieldRules.GetCel(),
},
celRuleField,
celRuleDescriptor,
)
if err != nil {
return err
}
compiledExpressions = append(compiledExpressions, celRuleCompiledExpressions...)
if len(compiledExpressions) > 0 {
eval.Rules = append(eval.Rules,
celPrograms{
Expand Down Expand Up @@ -613,3 +635,14 @@ func isPartOfMessageOneof(msgRules *validate.MessageRules, field protoreflect.Fi
return slices.Contains(oneof.GetFields(), string(field.Name()))
})
}

func expressionsToRules(expressions []string) []*validate.Rule {
rules := make([]*validate.Rule, 0, len(expressions))
for _, expr := range expressions {
rules = append(rules, validate.Rule_builder{
Id: proto.String(expr),
Expression: proto.String(expr),
}.Build())
}
return rules
}
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (c *cache) Build(
return false
}
}
precomputedASTs, compileErr = precomputedASTs.WithRuleValue(rule, desc)
precomputedASTs, compileErr = precomputedASTs.WithRuleValues(rules, rule, desc)
if compileErr != nil {
err = compileErr
return false
Expand Down
4 changes: 4 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func TestCache_GetExpectedRuleDescriptor(t *testing.T) {
desc: getFieldDesc(t, &cases.DurationNone{}, "val"),
ex: expectedWKTRules["google.protobuf.Duration"],
},
{
desc: getFieldDesc(t, &cases.FieldMaskNone{}, "val"),
ex: expectedWKTRules["google.protobuf.FieldMask"],
},
{
desc: getFieldDesc(t, &cases.StringNone{}, "val"),
ex: expectedStandardRules[protoreflect.StringKind],
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module buf.build/go/protovalidate
go 1.24.0

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20250912141014-52f32327d4b0.1
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20251209175733-2a1774d88802.1
buf.build/go/hyperpb v0.1.3
github.com/brianvoe/gofakeit/v6 v6.28.0
github.com/google/cel-go v0.26.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buf.build/gen/go/bufbuild/hyperpb-examples/protocolbuffers/go v1.36.7-20250725192734-0dd56aa9cbbc.1 h1:bFnppdLYActzr2F0iomSrkjUnGgVufb0DtZxjKgTLGc=
buf.build/gen/go/bufbuild/hyperpb-examples/protocolbuffers/go v1.36.7-20250725192734-0dd56aa9cbbc.1/go.mod h1:x7jYNX5/7EPnsKHEq596krkOGzvR97/MsZw2fw3Mrq0=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20250912141014-52f32327d4b0.1 h1:31on4W/yPcV4nZHL4+UCiCvLPsMqe/vJcNg8Rci0scc=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20250912141014-52f32327d4b0.1/go.mod h1:fUl8CEN/6ZAMk6bP8ahBJPUJw7rbp+j4x+wCcYi2IG4=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20251209175733-2a1774d88802.1 h1:ZnX3qpF/pDiYrf+Q3p+/zCzZ5ELSpszy5hdVarDMSV4=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.10-20251209175733-2a1774d88802.1/go.mod h1:fUl8CEN/6ZAMk6bP8ahBJPUJw7rbp+j4x+wCcYi2IG4=
buf.build/go/hyperpb v0.1.3 h1:wiw2F7POvAe2VA2kkB0TAsFwj91lXbFrKM41D3ZgU1w=
buf.build/go/hyperpb v0.1.3/go.mod h1:IHXAM5qnS0/Fsnd7/HGDghFNvUET646WoHmq1FDZXIE=
cel.dev/expr v0.24.0 h1:56OvJKSH3hDGL0ml5uSxZmz3/3Pq4tJ+fb1unVLAFcY=
Expand Down
Loading