aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2023-09-03 22:08:12 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2023-09-03 22:08:12 +0530
commit620fb2f89b5acd70af4ed075b4797f81a3f1d109 (patch)
tree191bbadfbe77fb71342853b85649b4ee5cb3c2bb
parentc6bc8d0c6d1c1ab5d26b9e47e0248002d22ecf8f (diff)
validating email addressesv0.0.3
-rw-r--r--customer/validators.go13
-rw-r--r--errors/errors.go1
-rw-r--r--main.go2
3 files changed, 13 insertions, 3 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
}
diff --git a/errors/errors.go b/errors/errors.go
index 6716fdc..77d4d8c 100644
--- a/errors/errors.go
+++ b/errors/errors.go
@@ -30,6 +30,7 @@ var (
ErrInvalidID = errors.New("Invalid ID")
ErrEmptyContactName = errors.New("Contact Name Cannot Be Empty")
ErrInvalidGSTIN = errors.New("Invalid GSTIN")
+ ErrInvalidEmail = errors.New("Invalid E-Mail Address")
ErrEmptyBrandName = errors.New("Brand Name Cannot Be Empty")
ErrInvalidUnitPrice = errors.New("Invalid Unit Price")
ErrInvalidGSTPercentage = errors.New("Invalid GST Percentage")
diff --git a/main.go b/main.go
index 7db2d75..ed540d2 100644
--- a/main.go
+++ b/main.go
@@ -37,7 +37,7 @@ import (
"log"
)
-const OPENBILLS_VERSION = "v0.0.2"
+const OPENBILLS_VERSION = "v0.0.3"
func init() {
if viper.GetBool("production_mode") {