Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CEL types for repeated field item expressions #92

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions internal/evaluator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (bldr *Builder) processIgnoreEmpty(
func (bldr *Builder) processFieldExpressions(
fieldDesc protoreflect.FieldDescriptor,
fieldConstraints *validate.FieldConstraints,
_ bool,
forItems bool,
eval *value,
_ MessageCache,
) error {
Expand All @@ -279,7 +279,7 @@ func (bldr *Builder) processFieldExpressions(
return nil
}

celTyp := celext.ProtoFieldToCELType(fieldDesc, false, false)
celTyp := celext.ProtoFieldToCELType(fieldDesc, false, forItems)
opts := append(
celext.RequiredCELEnvOptions(fieldDesc),
cel.Variable("this", celTyp),
Expand Down
134 changes: 100 additions & 34 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.

7 changes: 7 additions & 0 deletions proto/tests/example/v1/validations.proto
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,10 @@ message CelMapOnARepeated {
string name = 1;
}
}

message RepeatedItemCel {
repeated string paths = 1 [(buf.validate.field).repeated.items.cel = {
id: "paths.no_space",
expression: "!this.startsWith(' ')"
}];
}
14 changes: 14 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,17 @@ func TestValidator_Validate_CelMapOnARepeated(t *testing.T) {
valErr := &ValidationError{}
assert.ErrorAs(t, err, &valErr)
}

func TestValidator_Validate_RepeatedItemCel(t *testing.T) {
t.Parallel()
val, err := New()
require.NoError(t, err)
msg := &pb.RepeatedItemCel{Paths: []string{"foo"}}
err = val.Validate(msg)
require.NoError(t, err)
msg.Paths = append(msg.Paths, " bar")
err = val.Validate(msg)
valErr := &ValidationError{}
assert.ErrorAs(t, err, &valErr)
assert.Equal(t, "paths.no_space", valErr.Violations[0].ConstraintId)
}