aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--customer/validators.go25
-rw-r--r--errors/errors.go2
-rw-r--r--errors/status.go3
-rw-r--r--main.go2
4 files changed, 30 insertions, 2 deletions
diff --git a/customer/validators.go b/customer/validators.go
index 482d532..086750f 100644
--- a/customer/validators.go
+++ b/customer/validators.go
@@ -21,10 +21,17 @@ import (
"regexp"
"strings"
"net/mail"
+ "net/url"
"vidhukant.com/openbills/errors"
e "errors"
)
+var phoneRegex, gstinRegex, urlRegex *regexp.Regexp
+func init() {
+ phoneRegex = regexp.MustCompile(`^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$`)
+ gstinRegex = regexp.MustCompile(`^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$`)
+}
+
// NOTE: very inefficient and really really really dumb but it works
// TODO: find a better (or even a remotely good) way
func checkDuplicate(field, value string, userId uint) error {
@@ -69,7 +76,7 @@ func (c *Customer) validate() error {
// don't validate if GSTIN is empty
if c.Gstin != "" {
// GSTIN regex validation
- match, _ := regexp.MatchString("^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$", c.Gstin)
+ match := gstinRegex.MatchString(c.Gstin)
if (!match) {
return errors.ErrInvalidGSTIN
}
@@ -99,6 +106,22 @@ func (c *Customer) validate() error {
}
}
+ // don't validate phone if empty
+ if c.Phone != "" {
+ match := phoneRegex.MatchString(c.Phone)
+ if (!match) {
+ return errors.ErrInvalidPhone
+ }
+ }
+
+ // don't validate website if empty
+ if c.Website != "" {
+ _, err := url.ParseRequestURI(c.Website)
+ if err != nil {
+ return errors.ErrInvalidWebsite
+ }
+ }
+
var err error
for _, i := range [][]string{{"phone", c.Phone}, {"email", c.Email}, {"website", c.Website}} {
err = checkDuplicate(i[0], i[1], c.UserID)
diff --git a/errors/errors.go b/errors/errors.go
index 77d4d8c..3f1efc5 100644
--- a/errors/errors.go
+++ b/errors/errors.go
@@ -31,6 +31,8 @@ var (
ErrEmptyContactName = errors.New("Contact Name Cannot Be Empty")
ErrInvalidGSTIN = errors.New("Invalid GSTIN")
ErrInvalidEmail = errors.New("Invalid E-Mail Address")
+ ErrInvalidPhone = errors.New("Invalid Phone Number")
+ ErrInvalidWebsite = errors.New("Invalid Website URL")
ErrEmptyBrandName = errors.New("Brand Name Cannot Be Empty")
ErrInvalidUnitPrice = errors.New("Invalid Unit Price")
ErrInvalidGSTPercentage = errors.New("Invalid GST Percentage")
diff --git a/errors/status.go b/errors/status.go
index 1fa33d1..7a23ddf 100644
--- a/errors/status.go
+++ b/errors/status.go
@@ -33,6 +33,9 @@ func StatusCodeFromErr(err error) int {
errors.Is (err, ErrInvalidID) ||
errors.Is (err, ErrEmptyContactName) ||
errors.Is(err, ErrInvalidGSTIN) ||
+ errors.Is(err, ErrInvalidEmail) ||
+ errors.Is(err, ErrInvalidPhone) ||
+ errors.Is(err, ErrInvalidWebsite) ||
errors.Is(err, ErrEmptyBrandName) ||
errors.Is(err, ErrInvalidUnitPrice) ||
errors.Is(err, ErrPasswordTooShort) ||
diff --git a/main.go b/main.go
index ed540d2..2ec24dc 100644
--- a/main.go
+++ b/main.go
@@ -37,7 +37,7 @@ import (
"log"
)
-const OPENBILLS_VERSION = "v0.0.3"
+const OPENBILLS_VERSION = "v0.0.4"
func init() {
if viper.GetBool("production_mode") {