github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/ev/validator.go (about) 1 package ev 2 3 import ( 4 "github.com/go-email-validator/go-email-validator/pkg/ev/evsmtp" 5 "github.com/go-email-validator/go-email-validator/pkg/ev/utils" 6 "github.com/vmihailenco/msgpack" 7 ) 8 9 func init() { 10 msgpack.RegisterExt(evsmtp.ExtID(), new(DepsError)) 11 msgpack.RegisterExt(evsmtp.ExtID(), new(AValidationResult)) 12 } 13 14 // OtherValidator is ValidatorName for unknown Validator 15 const OtherValidator ValidatorName = "other" 16 17 // Validator is interface for validators 18 type Validator interface { 19 GetDeps() []ValidatorName 20 Validate(input Input, results ...ValidationResult) ValidationResult 21 } 22 23 // ChangeableValidationResult is ValidationResult with changeable errors and warnings 24 type ChangeableValidationResult interface { 25 SetErrors([]error) 26 SetWarnings([]error) 27 } 28 29 // ValidationResult is interface to represent result of validation 30 type ValidationResult interface { 31 // IsValid is status of validation 32 IsValid() bool 33 // Errors of result after validation 34 Errors() []error 35 // HasErrors checks for the presence of the Errors 36 HasErrors() bool 37 // Warnings of result after validation 38 Warnings() []error 39 // HasWarnings checks for the presence of the Warnings 40 HasWarnings() bool 41 // ValidatorName returns name of validator 42 ValidatorName() ValidatorName 43 } 44 45 // AValidationResult is abstract class for extending of validation 46 type AValidationResult struct { 47 isValid bool 48 errors []error 49 warnings []error 50 name ValidatorName 51 } 52 53 // IsValid is status of validation 54 func (a *AValidationResult) IsValid() bool { 55 return a.isValid 56 } 57 58 // SetErrors sets errors 59 func (a *AValidationResult) SetErrors(errors []error) { 60 a.errors = errors 61 a.isValid = !a.HasErrors() 62 } 63 64 // Errors of result after validation 65 func (a *AValidationResult) Errors() []error { 66 return a.errors 67 } 68 69 // HasErrors checks for the presence of the Errors 70 func (a *AValidationResult) HasErrors() bool { 71 return utils.RangeLen(a.Errors()) > 0 72 } 73 74 // SetWarnings sets warnings 75 func (a *AValidationResult) SetWarnings(warnings []error) { 76 a.warnings = warnings 77 } 78 79 // Warnings of result after validation 80 func (a *AValidationResult) Warnings() []error { 81 return a.warnings 82 } 83 84 // HasWarnings checks for the presence of the Warnings 85 func (a *AValidationResult) HasWarnings() bool { 86 return utils.RangeLen(a.Warnings()) > 0 87 } 88 89 // ValidatorName returns name of validator 90 func (a *AValidationResult) ValidatorName() ValidatorName { 91 return a.name 92 } 93 94 // EncodeMsgpack is used to fix this problem https://github.com/vmihailenco/msgpack/issues/294 95 func (a *AValidationResult) EncodeMsgpack(enc *msgpack.Encoder) error { 96 return enc.EncodeMulti( 97 a.isValid, 98 evsmtp.ErrorsToEVSMTPErrors(a.errors), 99 evsmtp.ErrorsToEVSMTPErrors(a.warnings), 100 a.name, 101 ) 102 } 103 104 // DecodeMsgpack is used to fix this problem https://github.com/vmihailenco/msgpack/issues/294 105 func (a *AValidationResult) DecodeMsgpack(dec *msgpack.Decoder) error { 106 return dec.DecodeMulti(&a.isValid, &a.errors, &a.warnings, &a.name) 107 } 108 109 type validationResult = AValidationResult 110 111 // NewValidResult returns valid validation result 112 func NewValidResult(name ValidatorName) ValidationResult { 113 return NewResult(true, nil, nil, name) 114 } 115 116 // NewResult returns result of validation by parameters 117 func NewResult(isValid bool, errors []error, warnings []error, name ValidatorName) ValidationResult { 118 if name == "" { 119 name = OtherValidator 120 } 121 122 return &validationResult{isValid, errors, warnings, name} 123 } 124 125 var emptyDeps = make([]ValidatorName, 0) 126 127 // AValidatorWithoutDeps is an abstract structure for validator without dependencies 128 type AValidatorWithoutDeps struct{} 129 130 // GetDeps returns dependencies of Validator 131 func (a AValidatorWithoutDeps) GetDeps() []ValidatorName { 132 return emptyDeps 133 }