Skip to content

Commit

Permalink
refactor web config validate
Browse files Browse the repository at this point in the history
  • Loading branch information
almostinf committed Sep 30, 2024
1 parent 372c4c8 commit a0eefbe
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions cmd/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ func (auth *authorization) toApiConfig(webConfig *webConfig) api.Authorization {

func (config *webConfig) validate() error {
for _, contactTemplate := range config.ContactsTemplate {
ValidationRegex := contactTemplate.ValidationRegex
if ValidationRegex != "" {
if _, err := regexp.Compile(ValidationRegex); err != nil {
return fmt.Errorf("contact template regex error '%s': %w", ValidationRegex, err)
}
validationRegex := contactTemplate.ValidationRegex
if validationRegex == "" {
continue
}

if _, err := regexp.Compile(validationRegex); err != nil {
return fmt.Errorf("contact template regex error '%s': %w", validationRegex, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/api/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func Test_webConfig_validate(t *testing.T) {
So(err, ShouldNotBeNil)
})

Convey("With invalid contact template pattern", t, func() {
Convey("With valid contact template pattern", t, func() {
config := webConfig{
ContactsTemplate: []webContact{
{
Expand Down

0 comments on commit a0eefbe

Please sign in to comment.