diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2024-04-05 11:36:43 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2024-04-05 11:36:43 +0530 |
commit | 10aed3cea7935f2f22ef713cff9839dd16afce48 (patch) | |
tree | 54e8e150e93f172ecba37792059da2b2658c59df /customer/validators.go | |
parent | bb088225a27d04c8237bfcdc7658a5564e838082 (diff) |
moved some validator logic and common structs to util package
Diffstat (limited to 'customer/validators.go')
-rw-r--r-- | customer/validators.go | 46 |
1 files changed, 9 insertions, 37 deletions
diff --git a/customer/validators.go b/customer/validators.go index 086750f..b5d1d96 100644 --- a/customer/validators.go +++ b/customer/validators.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * Copyright (C) 2023-2024 Vidhu Kant Sharma <vidhukant@vidhukant.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,27 +18,18 @@ package customer import ( - "regexp" "strings" - "net/mail" - "net/url" "vidhukant.com/openbills/errors" + u "vidhukant.com/openbills/util" 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 { if value != "" { var count int64 err := db.Model(&Customer{}). - //Select(""). Where("user_id = ? and " + field + " = ?", userId, value). Count(&count). Error @@ -55,6 +46,8 @@ func checkDuplicate(field, value string, userId uint) error { return errors.ErrNonUniqueEmail case "website": return errors.ErrNonUniqueWebsite + case "gstin": + return errors.ErrNonUniqueGSTIN default: return e.New(field + " is not unique") } @@ -75,55 +68,34 @@ func (c *Customer) validate() error { // don't validate if GSTIN is empty if c.Gstin != "" { - // GSTIN regex validation - match := gstinRegex.MatchString(c.Gstin) - if (!match) { + if !u.ValidateGstin(c.Gstin) { return errors.ErrInvalidGSTIN } - - // make sure GSTIN is unique - var count int64 - err := db.Model(&Customer{}). - Select("gstin"). - Where("gstin = ? and user_id = ?", c.Gstin, c.UserID). - Count(&count). - Error - - if err != nil { - return err - } - - if count > 0 { - return errors.ErrNonUniqueGSTIN - } } // don't validate email if empty if c.Email != "" { - _, err := mail.ParseAddress(c.Email) - if err != nil { + if !u.ValidateEmail(c.Email) { return errors.ErrInvalidEmail } } // don't validate phone if empty if c.Phone != "" { - match := phoneRegex.MatchString(c.Phone) - if (!match) { + if !u.ValidatePhone(c.Phone) { return errors.ErrInvalidPhone } } // don't validate website if empty if c.Website != "" { - _, err := url.ParseRequestURI(c.Website) - if err != nil { + if !u.ValidateWebsite(c.Website) { return errors.ErrInvalidWebsite } } var err error - for _, i := range [][]string{{"phone", c.Phone}, {"email", c.Email}, {"website", c.Website}} { + for _, i := range [][]string{{"phone", c.Phone}, {"email", c.Email}, {"website", c.Website}, {"gstin", c.Gstin}} { err = checkDuplicate(i[0], i[1], c.UserID) if err != nil { return err |