From 34786c48c899324e44b91252c1ff8fa41acaa480 Mon Sep 17 00:00:00 2001 From: Alexander Zielenski <351783+alexzielenski@users.noreply.github.com> Date: Tue, 9 Jan 2024 11:45:55 -0800 Subject: [PATCH] join errors and add index of failed CEL expression --- pkg/generators/markers.go | 20 +++++++++++++++----- pkg/generators/markers_test.go | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/pkg/generators/markers.go b/pkg/generators/markers.go index 68668fad7..7177c6c74 100644 --- a/pkg/generators/markers.go +++ b/pkg/generators/markers.go @@ -42,15 +42,21 @@ func (c *CELTag) Validate() error { return fmt.Errorf("empty CEL tag is not allowed") } + var errs []error if c.Rule == "" { - return fmt.Errorf("rule cannot be empty") + errs = append(errs, fmt.Errorf("rule cannot be empty")) } if c.Message == "" && c.MessageExpression == "" { - return fmt.Errorf("message or messageExpression must be set") + errs = append(errs, fmt.Errorf("message or messageExpression must be set")) } if c.Message != "" && c.MessageExpression != "" { - return fmt.Errorf("message and messageExpression cannot both be set") + errs = append(errs, fmt.Errorf("message and messageExpression cannot be set at the same time")) + } + + if len(errs) > 0 { + return errors.Join(errs...) } + return nil } @@ -113,8 +119,12 @@ func (c CommentTags) Validate() error { err = errors.Join(err, fmt.Errorf("multipleOf cannot be 0")) } - for _, celTag := range c.CEL { - err = errors.Join(err, celTag.Validate()) + for i, celTag := range c.CEL { + celError := celTag.Validate() + if celError == nil { + continue + } + err = errors.Join(err, fmt.Errorf("invalid CEL tag at index %d: %w", i, celError)) } return err diff --git a/pkg/generators/markers_test.go b/pkg/generators/markers_test.go index aef2eb675..6f7cbb623 100644 --- a/pkg/generators/markers_test.go +++ b/pkg/generators/markers_test.go @@ -171,7 +171,7 @@ func TestParseCommentTags(t *testing.T) { `+k8s:validation:cel[2]:rule="self > 5"`, `+k8s:validation:cel[2]:message="must be greater than 5"`, }, - expectedError: `invalid marker comments: empty CEL tag is not allowed`, + expectedError: `invalid marker comments: invalid CEL tag at index 1: empty CEL tag is not allowed`, }, { t: types.Float64,