Skip to content

Commit edd055f

Browse files
ernadojirfag
authored andcommitted
config: add validation for exclude rules
1 parent a3a0455 commit edd055f

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

pkg/config/config.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package config
22

33
import (
4+
"errors"
5+
"fmt"
6+
"regexp"
47
"time"
58
)
69

@@ -233,6 +236,37 @@ type ExcludeRule struct {
233236
Text string
234237
}
235238

239+
func validateOptionalRegex(value string) error {
240+
if value == "" {
241+
return nil
242+
}
243+
_, err := regexp.Compile(value)
244+
return err
245+
}
246+
247+
func (e ExcludeRule) Validate() error {
248+
if err := validateOptionalRegex(e.Path); err != nil {
249+
return fmt.Errorf("invalid path regex: %v", err)
250+
}
251+
if err := validateOptionalRegex(e.Text); err != nil {
252+
return fmt.Errorf("invalid text regex: %v", err)
253+
}
254+
nonBlank := 0
255+
if len(e.Linters) > 0 {
256+
nonBlank++
257+
}
258+
if e.Path != "" {
259+
nonBlank++
260+
}
261+
if e.Text != "" {
262+
nonBlank++
263+
}
264+
if nonBlank < 2 {
265+
return errors.New("at least 2 of (text, path, linters) should be set")
266+
}
267+
return nil
268+
}
269+
236270
type Issues struct {
237271
ExcludePatterns []string `mapstructure:"exclude"`
238272
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`

pkg/config/reader.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ func (r *FileReader) validateConfig() error {
105105
if c.Run.IsVerbose {
106106
return errors.New("can't set run.verbose option with config: only on command-line")
107107
}
108+
for i, rule := range c.Issues.ExcludeRules {
109+
if err := rule.Validate(); err != nil {
110+
return fmt.Errorf("error in exclude rule #%d: %v", i, err)
111+
}
112+
}
108113

109114
return nil
110115
}

pkg/result/processors/exclude_rules.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func NewExcludeRules(rules []ExcludeRule) *ExcludeRules {
5555
if rule.Path != "" {
5656
parsedRule.path = regexp.MustCompile(rule.Path)
5757
}
58-
// TODO: Forbid text-only, linter-only or path-only exclude rule.
5958
r.rules = append(r.rules, parsedRule)
6059
}
6160
return r

0 commit comments

Comments
 (0)