config: add validation for exclude rules
This commit is contained in:
parent
a3a04552bb
commit
edd055f862
@ -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"`
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user