config: add validation for exclude rules

This commit is contained in:
Aleksandr Razumov 2019-02-09 03:56:08 +03:00 committed by Isaev Denis
parent a3a04552bb
commit edd055f862
3 changed files with 39 additions and 1 deletions

View File

@ -1,6 +1,9 @@
package config package config
import ( import (
"errors"
"fmt"
"regexp"
"time" "time"
) )
@ -233,6 +236,37 @@ type ExcludeRule struct {
Text string 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 { type Issues struct {
ExcludePatterns []string `mapstructure:"exclude"` ExcludePatterns []string `mapstructure:"exclude"`
ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"` ExcludeRules []ExcludeRule `mapstructure:"exclude-rules"`

View File

@ -105,6 +105,11 @@ func (r *FileReader) validateConfig() error {
if c.Run.IsVerbose { if c.Run.IsVerbose {
return errors.New("can't set run.verbose option with config: only on command-line") 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 return nil
} }

View File

@ -55,7 +55,6 @@ func NewExcludeRules(rules []ExcludeRule) *ExcludeRules {
if rule.Path != "" { if rule.Path != "" {
parsedRule.path = regexp.MustCompile(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) r.rules = append(r.rules, parsedRule)
} }
return r return r