diff options
Diffstat (limited to 'customer/validators.go')
-rw-r--r-- | customer/validators.go | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/customer/validators.go b/customer/validators.go index 2a37394..482d532 100644 --- a/customer/validators.go +++ b/customer/validators.go @@ -20,13 +20,14 @@ package customer import ( "regexp" "strings" + "net/mail" "vidhukant.com/openbills/errors" e "errors" ) // NOTE: very inefficient and really really really dumb but it works // TODO: find a better (or even a remotely good) way -func validateContactField(field, value string, userId uint) error { +func checkDuplicate(field, value string, userId uint) error { if value != "" { var count int64 err := db.Model(&Customer{}). @@ -90,9 +91,17 @@ func (c *Customer) validate() error { } } + // don't validate email if empty + if c.Email != "" { + _, err := mail.ParseAddress(c.Email) + if err != nil { + return errors.ErrInvalidEmail + } + } + var err error for _, i := range [][]string{{"phone", c.Phone}, {"email", c.Email}, {"website", c.Website}} { - err = validateContactField(i[0], i[1], c.UserID) + err = checkDuplicate(i[0], i[1], c.UserID) if err != nil { return err } |