github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/presentation/prompt-email-verification-api/dep.go (about)

     1  package promptemailverificationapi
     2  
     3  import (
     4  	"github.com/go-email-validator/go-email-validator/pkg/ev"
     5  	"github.com/go-email-validator/go-email-validator/pkg/ev/evmail"
     6  	"github.com/go-email-validator/go-email-validator/pkg/presentation/converter"
     7  )
     8  
     9  //go:generate go run cmd/dep_test_generator/gen.go
    10  
    11  const (
    12  	// Name is a name of converter
    13  	Name converter.Name = "PromptEmailVerificationApi"
    14  
    15  	// ErrorSyntaxInvalid is a syntax error
    16  	ErrorSyntaxInvalid = "Invalid email syntax."
    17  )
    18  
    19  type mx struct {
    20  	AcceptsMail bool     `json:"accepts_mail"`
    21  	Records     []string `json:"records"`
    22  }
    23  
    24  // DepPresentation is representation of https://promptapi.com/marketplace/description/email_verification-api
    25  type DepPresentation struct {
    26  	Email          string `json:"email"`
    27  	SyntaxValid    bool   `json:"syntax_valid"`
    28  	IsDisposable   bool   `json:"is_disposable"`
    29  	IsRoleAccount  bool   `json:"is_role_account"`
    30  	IsCatchAll     bool   `json:"is_catch_all"`
    31  	IsDeliverable  bool   `json:"is_deliverable"`
    32  	CanConnectSMTP bool   `json:"can_connect_smtp"`
    33  	IsInboxFull    bool   `json:"is_inbox_full"`
    34  	IsDisabled     bool   `json:"is_disabled"`
    35  	MxRecords      mx     `json:"mx_records"`
    36  	Message        string `json:"message"`
    37  }
    38  
    39  // NewDepConverter creates DepConverter
    40  func NewDepConverter() DepConverter {
    41  	return DepConverter{}
    42  }
    43  
    44  // DepConverter is the converter for https://promptapi.com/marketplace/description/email_verification-api
    45  type DepConverter struct{}
    46  
    47  // Can be used for ev.ValidationResult
    48  func (DepConverter) Can(_ evmail.Address, result ev.ValidationResult, _ converter.Options) bool {
    49  	return result.ValidatorName() == ev.DepValidatorName
    50  }
    51  
    52  // Convert converts ev.ValidationResult in DepPresentation
    53  func (d DepConverter) Convert(email evmail.Address, resultInterface ev.ValidationResult, _ converter.Options) (result interface{}) {
    54  	var message string
    55  	depResult := resultInterface.(ev.DepValidationResult)
    56  	validationResults := depResult.GetResults()
    57  	mxResult := validationResults[ev.MXValidatorName].(ev.MXValidationResult)
    58  
    59  	smtpPresentation := converter.NewSMTPConverter().Convert(email, validationResults[ev.SMTPValidatorName], nil).(converter.SMTPPresentation)
    60  
    61  	Email := email.String()
    62  	isSyntaxValid := validationResults[ev.SyntaxValidatorName].IsValid()
    63  	if !isSyntaxValid && len(Email) == 0 {
    64  		message = ErrorSyntaxInvalid
    65  	}
    66  
    67  	depPresentation := DepPresentation{
    68  		Email:          Email,
    69  		SyntaxValid:    isSyntaxValid,
    70  		IsDisposable:   !validationResults[ev.DisposableValidatorName].IsValid(),
    71  		IsRoleAccount:  !validationResults[ev.RoleValidatorName].IsValid(),
    72  		IsCatchAll:     smtpPresentation.IsCatchAll,
    73  		IsDeliverable:  smtpPresentation.IsDeliverable,
    74  		CanConnectSMTP: smtpPresentation.CanConnectSMTP,
    75  		IsInboxFull:    smtpPresentation.HasFullInbox,
    76  		IsDisabled:     smtpPresentation.IsDisabled,
    77  		MxRecords: mx{
    78  			AcceptsMail: mxResult.IsValid(),
    79  			Records:     converter.MX2String(mxResult.MX()),
    80  		},
    81  		Message: message,
    82  	}
    83  
    84  	return depPresentation
    85  }
    86  
    87  // NewDepValidator is a common dep validator object
    88  func NewDepValidator(smtpValidator ev.Validator) ev.Validator {
    89  	builder := ev.NewDepBuilder(nil)
    90  	if smtpValidator == nil {
    91  		smtpValidator = builder.Get(ev.SMTPValidatorName)
    92  	}
    93  
    94  	return ev.NewDepBuilder(nil).Set(
    95  		ev.SyntaxValidatorName,
    96  		ev.NewSyntaxRegexValidator(nil),
    97  	).Set(ev.SMTPValidatorName, smtpValidator).Build()
    98  }