diff --git a/pkg/config/config.go b/pkg/config/config.go index 58a8c2d9..83b7b581 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -1,6 +1,9 @@ package config import ( + "errors" + "fmt" + "regexp" "time" ) @@ -233,6 +236,37 @@ type ExcludeRule struct { Text string } +func validateOptionalRegex(value string) error { + if value == "" { + return nil + } + _, err := regexp.Compile(value) + return err +} + +func (e ExcludeRule) Validate() error { + if err := validateOptionalRegex(e.Path); err != nil { + return fmt.Errorf("invalid path regex: %v", err) + } + if err := validateOptionalRegex(e.Text); err != nil { + return fmt.Errorf("invalid text regex: %v", err) + } + nonBlank := 0 + if len(e.Linters) > 0 { + nonBlank++ + } + if e.Path != "" { + nonBlank++ + } + if e.Text != "" { + nonBlank++ + } + if nonBlank < 2 { + return errors.New("at least 2 of (text, path, linters) should be set") + } + return nil +} + type Issues struct { ExcludePatterns []string `mapstructure:"exclude"` ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"` diff --git a/pkg/config/reader.go b/pkg/config/reader.go index 9f6598e2..df72632f 100644 --- a/pkg/config/reader.go +++ b/pkg/config/reader.go @@ -105,6 +105,11 @@ func (r *FileReader) validateConfig() error { if c.Run.IsVerbose { return errors.New("can't set run.verbose option with config: only on command-line") } + for i, rule := range c.Issues.ExcludeRules { + if err := rule.Validate(); err != nil { + return fmt.Errorf("error in exclude rule #%d: %v", i, err) + } + } return nil } diff --git a/pkg/result/processors/exclude_rules.go b/pkg/result/processors/exclude_rules.go index dec3a57e..c38a6778 100644 --- a/pkg/result/processors/exclude_rules.go +++ b/pkg/result/processors/exclude_rules.go @@ -55,7 +55,6 @@ func NewExcludeRules(rules []ExcludeRule) *ExcludeRules { if rule.Path != "" { parsedRule.path = regexp.MustCompile(rule.Path) } - // TODO: Forbid text-only, linter-only or path-only exclude rule. r.rules = append(r.rules, parsedRule) } return r