From 892d5d2a20d2ac3600b76f23697a00c9464d9d4d Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sun, 8 Mar 2020 13:03:38 +0530 Subject: [PATCH] Remove 'govalidator' package dependecy --- campaigns.go | 10 +++++----- go.mod | 1 - go.sum | 2 -- internal/subimporter/importer.go | 14 +++++++++++--- lists.go | 3 +-- subscribers.go | 5 ++--- templates.go | 3 +-- utils.go | 5 +++++ 8 files changed, 25 insertions(+), 18 deletions(-) diff --git a/campaigns.go b/campaigns.go index cb61bdb..0a6c437 100644 --- a/campaigns.go +++ b/campaigns.go @@ -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`") // } diff --git a/go.mod b/go.mod index 1ffb196..02ad514 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 23b0483..be50175 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/subimporter/importer.go b/internal/subimporter/importer.go index b329eed..eb70ed1 100644 --- a/internal/subimporter/importer.go +++ b/internal/subimporter/importer.go @@ -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) +} diff --git a/lists.go b/lists.go index c1a4cf4..d203fee 100644 --- a/lists.go +++ b/lists.go @@ -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.") } diff --git a/subscribers.go b/subscribers.go index ed8b687..b09656d 100644 --- a/subscribers.go +++ b/subscribers.go @@ -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`.") } diff --git a/templates.go b/templates.go index 31d7d71..28db89a 100644 --- a/templates.go +++ b/templates.go @@ -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`") } diff --git a/utils.go b/utils.go index 4bf914c..3113593 100644 --- a/utils.go +++ b/utils.go @@ -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 +}