Skip to content

Commit

Permalink
[knf/validators/network] Added 'Mail' validator
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Mar 2, 2024
1 parent ca1e46d commit 4d3e9c2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 12.102.0

* `[knf/validators/network]` Added `Mail` validator

### 12.101.0

* `[req]` Added Bearer Token property to `Request` struct
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.101.0"
const VERSION = "12.102.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
18 changes: 18 additions & 0 deletions knf/validators/network/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"net"
"net/url"
"strings"

"github.com/essentialkaos/ek/v12/knf"
)
Expand All @@ -34,6 +35,9 @@ var (
// URL returns error if config property isn't a valid URL
URL = validateURL

// Mail returns error if config property isn't a valid email address
Mail = validateMail

// HasIP returns error if system doesn't have interface with IP from config property
HasIP = validateHasIP
)
Expand Down Expand Up @@ -120,6 +124,20 @@ func validateURL(config *knf.Config, prop string, value any) error {
return nil
}

func validateMail(config *knf.Config, prop string, value any) error {
mailStr := config.GetS(prop)

if mailStr == "" {
return nil
}

if !strings.ContainsRune(mailStr, '@') || !strings.ContainsRune(mailStr, '.') {
return fmt.Errorf("%q is not a valid email address", mailStr)
}

return nil
}

func validateHasIP(config *knf.Config, prop string, value any) error {
ipStr := config.GetS(prop)

Expand Down
26 changes: 25 additions & 1 deletion knf/validators/network/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const _CONFIG_DATA = `
test0:
test1: https://google.com
test2: google.com/abcd.php
[mail]
test0:
test1: unknown
test2: unknown@domain
test3: unknown@domain.com
`

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -188,7 +194,25 @@ func (s *ValidatorSuite) TestURLValidator(c *C) {
})

c.Assert(errs, HasLen, 1)
c.Assert(errs[0].Error(), Equals, "google.com/abcd.php is not a valid URL address: parse \"google.com/abcd.php\": invalid URI for request")
c.Assert(errs[0].Error(), Equals, `google.com/abcd.php is not a valid URL address: parse "google.com/abcd.php": invalid URI for request`)
}

func (s *ValidatorSuite) TestMailValidator(c *C) {
configFile := createConfig(c, _CONFIG_DATA)

err := knf.Global(configFile)
c.Assert(err, IsNil)

errs := knf.Validate([]*knf.Validator{
{"mail:test0", Mail, nil},
{"mail:test1", Mail, nil},
{"mail:test2", Mail, nil},
{"mail:test3", Mail, nil},
})

c.Assert(errs, HasLen, 2)
c.Assert(errs[0].Error(), Equals, `"unknown" is not a valid email address`)
c.Assert(errs[1].Error(), Equals, `"unknown@domain" is not a valid email address`)
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down

0 comments on commit 4d3e9c2

Please sign in to comment.