Skip to content

Commit

Permalink
Remove 'govalidator' package dependecy
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Mar 8, 2020
1 parent d4aea0a commit 892d5d2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 18 deletions.
10 changes: 5 additions & 5 deletions campaigns.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"strings"
"time"

"github.com/asaskevich/govalidator"
"github.com/gofrs/uuid"
"github.com/knadh/listmonk/internal/subimporter"
"github.com/knadh/listmonk/models"
"github.com/labstack/echo"
"github.com/lib/pq"
Expand Down Expand Up @@ -574,19 +574,19 @@ func validateCampaignFields(c campaignReq, app *App) (campaignReq, error) {
if c.FromEmail == "" {
c.FromEmail = app.constants.FromEmail
} else if !regexFromAddress.Match([]byte(c.FromEmail)) {
if !govalidator.IsEmail(c.FromEmail) {
if !subimporter.IsEmail(c.FromEmail) {
return c, errors.New("invalid `from_email`")
}
}

if !govalidator.IsByteLength(c.Name, 1, stdInputMaxLen) {
if !strHasLen(c.Name, 1, stdInputMaxLen) {
return c, errors.New("invalid length for `name`")
}
if !govalidator.IsByteLength(c.Subject, 1, stdInputMaxLen) {
if !strHasLen(c.Subject, 1, stdInputMaxLen) {
return c, errors.New("invalid length for `subject`")
}

// if !govalidator.IsByteLength(c.Body, 1, bodyMaxLen) {
// if !hasLen(c.Body, 1, bodyMaxLen) {
// return c,errors.New("invalid length for `body`")
// }

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module github.com/knadh/listmonk

require (
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf
github.com/disintegration/imaging v1.5.0
github.com/gofrs/uuid v3.2.0+incompatible
github.com/jinzhu/gorm v1.9.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf h1:eg0MeVzsP1G42dRafH3vf+al2vQIJU0YHX+1Tw87oco=
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
14 changes: 11 additions & 3 deletions internal/subimporter/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"sync"

"github.com/asaskevich/govalidator"
"github.com/gofrs/uuid"
"github.com/knadh/listmonk/models"
"github.com/lib/pq"
Expand Down Expand Up @@ -101,6 +101,9 @@ var (
csvHeaders = map[string]bool{"email": true,
"name": true,
"attributes": true}

// https://www.alexedwards.net/blog/validation-snippets-for-go#email-validation
regexEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
)

// New returns a new instance of Importer.
Expand Down Expand Up @@ -567,10 +570,10 @@ func ValidateFields(s SubReq) error {
if len(s.Email) > 1000 {
return errors.New(`e-mail too long`)
}
if !govalidator.IsEmail(s.Email) {
if !IsEmail(s.Email) {
return errors.New(`invalid e-mail "` + s.Email + `"`)
}
if !govalidator.IsByteLength(s.Name, 1, stdInputMaxLen) {
if len(s.Name) == 0 || len(s.Name) > stdInputMaxLen {
return errors.New(`invalid or empty name "` + s.Name + `"`)
}
return nil
Expand Down Expand Up @@ -599,3 +602,8 @@ func countLines(r io.Reader) (int, error) {
}
}
}

// IsEmail checks whether the given string is a valid e-mail address.
func IsEmail(email string) bool {
return regexEmail.MatchString(email)
}
3 changes: 1 addition & 2 deletions lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/knadh/listmonk/models"
"github.com/lib/pq"

"github.com/asaskevich/govalidator"
"github.com/labstack/echo"
)

Expand Down Expand Up @@ -80,7 +79,7 @@ func handleCreateList(c echo.Context) error {
}

// Validate.
if !govalidator.IsByteLength(o.Name, 1, stdInputMaxLen) {
if !strHasLen(o.Name, 1, stdInputMaxLen) {
return echo.NewHTTPError(http.StatusBadRequest,
"Invalid length for the name field.")
}
Expand Down
5 changes: 2 additions & 3 deletions subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strconv"
"strings"

"github.com/asaskevich/govalidator"
"github.com/gofrs/uuid"
"github.com/knadh/listmonk/internal/subimporter"
"github.com/knadh/listmonk/models"
Expand Down Expand Up @@ -204,10 +203,10 @@ func handleUpdateSubscriber(c echo.Context) error {
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID.")
}
if req.Email != "" && !govalidator.IsEmail(req.Email) {
if req.Email != "" && !subimporter.IsEmail(req.Email) {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid `email`.")
}
if req.Name != "" && !govalidator.IsByteLength(req.Name, 1, stdInputMaxLen) {
if req.Name != "" && !strHasLen(req.Name, 1, stdInputMaxLen) {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid length for `name`.")
}

Expand Down
3 changes: 1 addition & 2 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"regexp"
"strconv"

"github.com/asaskevich/govalidator"
"github.com/knadh/listmonk/models"
"github.com/labstack/echo"
)
Expand Down Expand Up @@ -241,7 +240,7 @@ func handleDeleteTemplate(c echo.Context) error {

// validateTemplate validates template fields.
func validateTemplate(o models.Template) error {
if !govalidator.IsByteLength(o.Name, 1, stdInputMaxLen) {
if !strHasLen(o.Name, 1, stdInputMaxLen) {
return errors.New("invalid length for `name`")
}

Expand Down
5 changes: 5 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,8 @@ func generateRandomString(n int) (string, error) {

return string(bytes), nil
}

// strHasLen checks if the given string has a length within min-max.
func strHasLen(str string, min, max int) bool {
return len(str) >= min && len(str) <= max
}

0 comments on commit 892d5d2

Please sign in to comment.