-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessage.go
89 lines (78 loc) · 1.96 KB
/
message.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
package infobip
import "regexp"
// BulkMessage contains the body request for multiple messages
type BulkMessage struct {
ID string `json:"bulkId,omitempty"`
Messages []Message `json:"messages"`
}
// Message contains the body request
type Message struct {
From string `json:"from,omitempty"`
Destinations []Destination `json:"destinations,omitempty"`
To string `json:"to,omitempty"`
Text string `json:"text"`
Transliteration string `json:"transliteration,omitempty"`
LanguageCode string `json:"languageCode,omitempty"`
}
// Destination contains the recipient
type Destination struct {
ID string `json:"messageId,omitempty"`
To string `json:"to"`
}
// Validate validates the entire message values
func (b BulkMessage) Validate() (err error) {
for _, m := range b.Messages {
if err = m.Validate(); err != nil {
break
}
}
return
}
// Validate validates the body request values
func (m Message) Validate() (err error) {
if err = m.validateFromValue(); err != nil {
return
}
if err = m.validateDestinationValues(); 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) validateDestinationValues() (err error) {
for _, d := range m.Destinations {
if isNumeric(d.To) && !isValidRange(d.To, 3, 14) {
err = ErrForDestinationNonAlphanumeric
break
}
}
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
}