Skip to content

Commit

Permalink
join errors and add index of failed CEL expression
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzielenski committed Jan 9, 2024
1 parent ad6895a commit 34786c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions pkg/generators/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/generators/markers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 34786c4

Please sign in to comment.