This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
validator_mx.go
executable file
路68 lines (54 loc) 路 1.64 KB
/
validator_mx.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
package ev
import (
"github.com/go-email-validator/go-email-validator/pkg/ev/evsmtp"
"github.com/go-email-validator/go-email-validator/pkg/ev/utils"
)
// MXValidatorName is name of mx validator
const MXValidatorName ValidatorName = "MXValidator"
// EmptyMXsError is error of MXValidatorName
type EmptyMXsError struct{}
func (EmptyMXsError) Error() string {
return "EmptyMXsError"
}
// MXValidationResult is result of MXValidatorName
type MXValidationResult interface {
MX() evsmtp.MXs
ValidationResult
}
// NewMXValidationResult instantiates result of MXValidatorName
func NewMXValidationResult(mx evsmtp.MXs, result *AValidationResult) MXValidationResult {
return mxValidationResult{mx: mx, AValidationResult: result}
}
type mxValidationResult struct {
*AValidationResult
mx evsmtp.MXs
}
func (v mxValidationResult) MX() evsmtp.MXs {
return v.mx
}
// DefaultNewMXValidator instantiates default MXValidatorName based on evsmtp.LookupMX
func DefaultNewMXValidator() Validator {
return NewMXValidator(evsmtp.LookupMX)
}
// NewMXValidator instantiates MXValidatorName based on evsmtp.FuncLookupMX
func NewMXValidator(lookupMX evsmtp.FuncLookupMX) Validator {
return mxValidator{
lookupMX: lookupMX,
}
}
type mxValidator struct {
AValidatorWithoutDeps
lookupMX evsmtp.FuncLookupMX
}
func (v mxValidator) Validate(input Input, _ ...ValidationResult) ValidationResult {
var mxs evsmtp.MXs
var err error
mxs, err = v.lookupMX(input.Email().Domain())
if hasMXs := len(mxs) > 0; err == nil && !hasMXs {
err = EmptyMXsError{}
}
return NewMXValidationResult(
mxs,
NewResult(err == nil, utils.Errs(err), nil, MXValidatorName).(*AValidationResult),
)
}