forked from BTBurke/twiml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
161 lines (143 loc) · 3.82 KB
/
validate.go
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
package twiml
import (
"fmt"
"regexp"
"strings"
)
// Validate aggregates the results of individual validation functions and returns true
// when all validation functions pass
func Validate(vf ...bool) bool {
for _, f := range vf {
if !f {
return false
}
}
return true
}
// OneOf validates that a field is one of the options provided
func OneOf(field string, options ...string) bool {
for _, w := range options {
if field == w {
return true
}
}
return false
}
// IntBetween validates that a field is an integer between high and low
func IntBetween(field int, high int, low int) bool {
if (field <= high) && (field >= low) {
return true
}
return false
}
// Required validates that a field is not the empty string
func Required(field string) bool {
if len(field) > 0 {
return true
}
return false
}
// OneOfOpt validates that a field is one of the options provided or the empty string (for optional fields)
func OneOfOpt(field string, options ...string) bool {
if field == "" {
return true
}
return OneOf(field, options...)
}
// AllowedMethod validates that a method is either of type GET or POST (or empty string to default)
func AllowedMethod(field string) bool {
// optional field always set with default (typically POST)
if field == "" {
return true
}
if (field != "GET") && (field != "POST") {
return false
}
return true
}
// Numeric validates that a string contains only digits 0-9
func Numeric(field string) bool {
matched, err := regexp.MatchString("^[0-9]+$", field)
if err != nil {
return false
}
return matched
}
// NumericOrWait validates that a string contains only digits 0-9 or the wait key 'w'
func NumericOrWait(field string) bool {
if field == "" {
return true
}
matched, err := regexp.MatchString("^[0-9w]+$", field)
if err != nil {
return false
}
return matched
}
// NumericOpt validates that the field is numeric or empty string (for optional fields)
func NumericOpt(field string) bool {
if field == "" {
return true
}
return Numeric(field)
}
// AllowedLanguage validates that the combination of speaker and language is allowable
func AllowedLanguage(speaker string, language string) bool {
switch speaker {
case Man, Woman:
return OneOfOpt(language, English, French, German, Spanish, EnglishUK)
case Alice:
return OneOfOpt(language,
DanishDenmark,
GermanGermany,
EnglishAustralia,
EnglishCanada,
EnglishUK,
EnglishIndia,
EnglishUSA,
SpanishCatalan,
SpanishSpain,
SpanishMexico,
FinishFinland,
FrenchCanada,
FrenchFrance,
ItalianItaly,
JapaneseJapan,
KoreanKorea,
NorwegianNorway,
DutchNetherlands,
PolishPoland,
PortugueseBrazil,
PortuguesePortugal,
RussianRussia,
SwedishSweden,
ChineseMandarin,
ChineseCantonese,
ChineseTaiwanese,
)
default:
return OneOfOpt(language, English, French, German, Spanish, EnglishUK)
}
}
// construct regexp to validate a list of callback events
func constructCallbackEventValidator(eventNames []string) *regexp.Regexp {
eventPatterns := make([]string, 0)
for _, eventName := range eventNames {
eventPatterns = append(eventPatterns, fmt.Sprintf("%s\\s?", eventName))
}
expression := fmt.Sprintf("^(%s)+$", strings.Join(eventPatterns, "|"))
return regexp.MustCompile(expression)
}
var (
// callback events valid for Sip TwiML block
SipCallbackEvents = constructCallbackEventValidator([]string{"initiated", "ringing", "answered", "completed"})
// callback events valid for Conference TwiML block
ConferenceCallbackEvents = constructCallbackEventValidator([]string{"start", "end", "join", "leave", "mute", "hold", "speaker"})
)
// AllowedCallbackEvent validates that the CallbackEvent is one of the allowed options
func AllowedCallbackEvent(events string, callbackValidator *regexp.Regexp) bool {
if events == "" {
return true
}
return callbackValidator.MatchString(events)
}