forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
32 lines (25 loc) · 836 Bytes
/
validator.go
File metadata and controls
32 lines (25 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package validators
import (
"fmt"
"github.com/nyaruka/phonenumbers"
"github.com/thedevsaddam/govalidator"
)
type validator struct{}
const (
phoneNumberRule = "phoneNumber"
)
func init() {
// custom rules to take fixed length word.
// e.g: max_word:5 will throw error if the field contains more than 5 words
govalidator.AddCustomRule(phoneNumberRule, func(field string, rule string, message string, value interface{}) error {
phoneNumber, ok := value.(string)
if !ok {
return fmt.Errorf("the %s field must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164", field)
}
_, err := phonenumbers.Parse(phoneNumber, phonenumbers.UNKNOWN_REGION)
if err != nil {
return fmt.Errorf("the %s field must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164", field)
}
return nil
})
}