44 "context"
55 "fmt"
66 "net/url"
7+ "regexp"
78
89 "github.com/NdoleStudio/httpsms/pkg/events"
910
@@ -14,9 +15,10 @@ import (
1415type validator struct {}
1516
1617const (
17- phoneNumberRule = "phoneNumber"
18- multiplePhoneNumberRule = "multiplePhoneNumber"
19- webhookEventsRule = "webhookEvents"
18+ phoneNumberRule = "phoneNumber"
19+ contactPhoneNumberRule = "contactPhoneNumber"
20+ multipleContactPhoneNumberRule = "multipleContactPhoneNumber"
21+ webhookEventsRule = "webhookEvents"
2022)
2123
2224func init () {
@@ -25,27 +27,41 @@ func init() {
2527 govalidator .AddCustomRule (phoneNumberRule , func (field string , rule string , message string , value interface {}) error {
2628 phoneNumber , ok := value .(string )
2729 if ! ok {
28- return fmt .Errorf ("the %s field must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164" , field )
30+ return fmt .Errorf ("The %s field must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164" , field )
2931 }
3032
3133 _ , err := phonenumbers .Parse (phoneNumber , phonenumbers .UNKNOWN_REGION )
3234 if err != nil {
33- return fmt .Errorf ("the %s field must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164" , field )
35+ return fmt .Errorf ("The %s field must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164" , field )
3436 }
3537
3638 return nil
3739 })
3840
39- govalidator .AddCustomRule (multiplePhoneNumberRule , func (field string , rule string , message string , value interface {}) error {
41+ // custom rules to take fixed length word.
42+ // e.g: max_word:5 will throw error if the field contains more than 5 words
43+ govalidator .AddCustomRule (contactPhoneNumberRule , func (field string , rule string , message string , value interface {}) error {
44+ phoneNumber , ok := value .(string )
45+ if ! ok {
46+ return fmt .Errorf ("The %s field must contain only digits and must be less than 14 characters" , field )
47+ }
48+
49+ if match , err := regexp .MatchString ("^\\ +?[0-9]\\ d{1,14}$" , phoneNumber ); err != nil || ! match {
50+ return fmt .Errorf ("The %s field must contain only digits and must be less than 14 characters" , field )
51+ }
52+
53+ return nil
54+ })
55+
56+ govalidator .AddCustomRule (multipleContactPhoneNumberRule , func (field string , rule string , message string , value interface {}) error {
4057 phoneNumbers , ok := value .([]string )
4158 if ! ok {
42- return fmt .Errorf ("the %s field must be an array of a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164 " , field )
59+ return fmt .Errorf ("The %s field must be an array of valid phone numbers " , field )
4360 }
4461
4562 for index , number := range phoneNumbers {
46- _ , err := phonenumbers .Parse (number , phonenumbers .UNKNOWN_REGION )
47- if err != nil {
48- return fmt .Errorf ("the %s value in index [%d] must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164" , field , index )
63+ if match , err := regexp .MatchString ("^\\ +?[0-9]\\ d{1,14}$" , number ); err != nil || ! match {
64+ return fmt .Errorf ("The %s field in index [%d] must contain only digits and must be less than 14 characters" , field , index )
4965 }
5066 }
5167
@@ -55,11 +71,11 @@ func init() {
5571 govalidator .AddCustomRule (webhookEventsRule , func (field string , rule string , message string , value interface {}) error {
5672 input , ok := value .([]string )
5773 if ! ok {
58- return fmt .Errorf ("the %s field must be a string array" , field )
74+ return fmt .Errorf ("The %s field must be a string array" , field )
5975 }
6076
6177 if len (input ) == 0 {
62- return fmt .Errorf ("the %s field is an empty array" , field )
78+ return fmt .Errorf ("The %s field is an empty array" , field )
6379 }
6480
6581 validEvents := map [string ]bool {
@@ -68,7 +84,7 @@ func init() {
6884
6985 for _ , event := range input {
7086 if _ , ok := validEvents [event ]; ! ok {
71- return fmt .Errorf ("the %s field has an invalid event with name [%s]" , field , event )
87+ return fmt .Errorf ("The %s field has an invalid event with name [%s]" , field , event )
7288 }
7389 }
7490
0 commit comments