Remove 'govalidator' package dependecy

This commit is contained in:
Kailash Nadh 2020-03-08 13:03:38 +05:30
parent d4aea0a436
commit 892d5d2a20
8 changed files with 25 additions and 18 deletions

View File

@ -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"
@ -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`")
// }

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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"
@ -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.
@ -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
@ -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)
}

View File

@ -9,7 +9,6 @@ import (
"github.com/knadh/listmonk/models"
"github.com/lib/pq"
"github.com/asaskevich/govalidator"
"github.com/labstack/echo"
)
@ -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.")
}

View File

@ -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"
@ -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`.")
}

View File

@ -8,7 +8,6 @@ import (
"regexp"
"strconv"
"github.com/asaskevich/govalidator"
"github.com/knadh/listmonk/models"
"github.com/labstack/echo"
)
@ -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`")
}

View File

@ -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
}