forked from maksymgaraiev/orwell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
50 lines (43 loc) · 850 Bytes
/
email.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
package orwell
import (
"fmt"
"net"
"net/mail"
"strings"
)
// EMail func
func (*Orwell) Email(doLookUp bool) *email {
return &email{
doLookUp: doLookUp,
msg: "Validation error for 'Email' rule",
}
}
type email struct {
doLookUp bool
msg string
}
// Apply rule
func (r *email) Apply(value interface{}) error {
v, isNil := IsNil(value)
if isNil || IsEmpty(value) {
return nil
}
vStr, err := ToString(v)
if err != nil {
return fmt.Errorf("%s: %s", r.msg, err.Error())
}
_, err = mail.ParseAddress(vStr)
if err != nil {
return fmt.Errorf("%s: %s", r.msg, err.Error())
}
if r.doLookUp {
mx, err := net.LookupMX(strings.Split(vStr, "@")[1])
if err != nil {
return fmt.Errorf("%s: %s", r.msg, err.Error())
}
if len(mx) == 0 {
return fmt.Errorf("%s: no mx record found", r.msg)
}
}
return nil
}