Skip to content

Commit

Permalink
feat: Add name validation for custom analyzer creation
Browse files Browse the repository at this point in the history
To ensure the integrity and consistency of analyzer names, we introduced a validation step that checks the format of the name against a predefined regex pattern. This change aims to prevent the creation of analyzers with invalid names, enhancing the system's reliability and usability.

Signed-off-by: Matthis Holleville <[email protected]>
  • Loading branch information
matthisholleville committed Aug 5, 2024
1 parent f137b60 commit 108424b
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/customAnalyzer/customAnalyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package customanalyzer
import (
"fmt"
"reflect"
"regexp"
)

type CustomAnalyzerConfiguration struct {
Expand All @@ -22,6 +23,12 @@ func NewCustomAnalyzer() *CustomAnalyzer {
}

func (*CustomAnalyzer) Check(actualConfig []CustomAnalyzerConfiguration, name, url string, port int) error {
validNameRegex := `^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`
validName := regexp.MustCompile(validNameRegex)
if !validName.MatchString(name) {
return fmt.Errorf("invalid name format. Must match %s", validNameRegex)
}

for _, analyzer := range actualConfig {
if analyzer.Name == name {
return fmt.Errorf("custom analyzer with the name '%s' already exists. Please use a different name", name)
Expand Down

0 comments on commit 108424b

Please sign in to comment.