/
validations.go
44 lines (41 loc) · 1.09 KB
/
validations.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
package helpers
import (
"regexp"
"strings"
request "github.com/DronRathore/goexpress/request"
)
var EmailRegexp *regexp.Regexp
const emailregex string = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
var InitRegex = (func() bool {
EmailRegexp = regexp.MustCompile(emailregex)
return true
}())
func IsEmail(email string) bool {
email = strings.ToLower(email)
return EmailRegexp.MatchString(email)
}
/*
Validates a batch request body
*/
func HasEssentials(req *request.Request) bool {
if req.Body["from"] == nil || len(req.Body["from"]) == 0 || IsEmail(req.Body["from"][0]) == false {
return false
}
if req.Body["to"] != nil {
if len(req.Body["to"]) == 0 {
return false
}
for _, email := range req.Body["to"] {
if IsEmail(email) == false {
return false
}
}
}
if req.Body["subject"] == nil || len(req.Body["subject"][0]) == 0 {
return false
}
if req.Body["body"] == nil || len(req.Body["body"][0]) == 0 {
return false
}
return true
}