-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmessage.go
More file actions
54 lines (46 loc) · 1.08 KB
/
Copy pathmessage.go
File metadata and controls
54 lines (46 loc) · 1.08 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
package wavecell
import "regexp"
// Message contains the body request
type Message struct {
From string `json:"source,omitempty"`
To string `json:"destination"`
Text string `json:"text"`
ClientMessageID string `json:"clientMessageId"`
Encoding string `json:"encoding"`
}
// Validate validates the body request values
func (m Message) Validate() (err error) {
if err = m.validateFromValue(); err != nil {
return
}
err = m.validateToValue()
return
}
func (m Message) validateFromValue() (err error) {
if isNumeric(m.From) && !isValidRange(m.From, 3, 14) {
err = ErrForFromNonAlphanumeric
return
}
if !isValidRange(m.From, 3, 13) {
err = ErrForFromAlphanumeric
return
}
return
}
func (m Message) validateToValue() (err error) {
if m.To == "" {
return
}
if isNumeric(m.To) && !isValidRange(m.To, 3, 14) {
err = ErrForToNonAlphanumeric
return
}
return
}
func isNumeric(s string) bool {
return regexp.MustCompile(`^[\d]*$`).MatchString(s)
}
func isValidRange(s string, a, b int) bool {
l := len(s)
return l > a && l <= b
}