-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.go
More file actions
179 lines (145 loc) · 3.64 KB
/
validators.go
File metadata and controls
179 lines (145 loc) · 3.64 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package twiml
import (
"errors"
"fmt"
"net/url"
"strings"
"github.com/ttacon/libphonenumber"
)
const allowempty = "allowempty"
// validFromOrTo checks that a valid phone number or sip uri is provided
// param of "allowempty" will allow a nil value
func validFromOrTo(v interface{}, param string) error {
err := validPhoneNumber(v, param)
if err == nil {
return nil
}
if err := validSIPURI(v, param); err == nil {
return nil
}
return err
}
// validPhoneNumber checks that a valid phone number is provided
// param of "allowempty" will allow a nil value
func validPhoneNumber(v interface{}, param string) error {
switch num := v.(type) {
case string:
if num == "" {
if param == allowempty {
return nil
}
return errors.New("Required")
}
return validatePhoneNumber(num)
case *string:
if num == nil {
if param == allowempty {
return nil
}
return errors.New("Required")
}
return validatePhoneNumber(*num)
default:
return fmt.Errorf("validPhoneNumber: Unexpected type %T", num)
}
}
func validatePhoneNumber(num string) error {
n, err := libphonenumber.Parse(num, "US")
if err != nil {
return errors.New("invalid phone number")
}
if !libphonenumber.IsValidNumber(n) {
return errors.New("invalid phone number")
}
return nil
}
func validateKeyPadEntry(v interface{}, param string) error {
switch num := v.(type) {
case string:
if num == "" {
if param == allowempty {
return nil
}
return errors.New("Required")
}
return validateNumericPoundStar(num)
case *string:
if num == nil {
if param == allowempty {
return nil
}
return errors.New("Required")
}
return validateNumericPoundStar(*num)
default:
return fmt.Errorf("validateKeyPadEntry: Unexpected type %T", num)
}
}
func validateNumericPoundStar(v string) error {
return characterList(v, "0123456789#*")
}
// characterList checks a string against a list of acceptable characters.
// returns an erro if a character is found which is not in charList
func characterList(s, charList string) error {
for _, c := range s {
if !strings.Contains(charList, string(c)) {
return fmt.Errorf("invalid: character '%s' is not allowed", string(c))
}
}
return nil
}
// validSIPURI checks that a valid sip uri is provided
// param of "allowempty" will allow a nil value
func validSIPURI(v interface{}, param string) error {
switch num := v.(type) {
case string:
if num == "" {
if param == allowempty {
return nil
}
return errors.New("Required")
}
_, err := parseSIPURI(num)
return err
case *string:
if num == nil {
if param == allowempty {
return nil
}
return errors.New("Required")
}
_, err := parseSIPURI(*num)
return err
default:
return fmt.Errorf("validPhoneNumber: Unexpected type %T", num)
}
}
func parseSIPURI(uri string) (*url.URL, error) {
uri = strings.ToLower(uri)
if !strings.HasPrefix(uri, "sip") {
return nil, errors.New("invalid SIP URI")
}
// Insert the // after the Schema to enable full parsing and avoid Opaque
if strings.HasPrefix(uri, "sips:") {
uri = strings.Replace(uri, "sips:", "sips://", 1)
} else if strings.HasPrefix(uri, "sip:") {
uri = strings.Replace(uri, "sip:", "sip://", 1)
}
u, err := url.Parse(uri)
if err != nil {
return nil, errors.New("invalid SIP URI")
}
// Schema should be valid
if u.Scheme != "sip" && u.Scheme != "sips" {
return u, errors.New("invalid SIP URI")
}
// Path and Opaque should be empty
if u.Path != "" || u.Opaque != "" {
return u, errors.New("invalid SIP URI")
}
// Host and User should be provided
if u.Host == "" || u.User.String() == "" {
return u, errors.New("invalid SIP URI")
}
return u, nil
}