diff --git a/check/domain.go b/check/domain.go index 04342b7..c9ecec7 100644 --- a/check/domain.go +++ b/check/domain.go @@ -2,6 +2,7 @@ package check import ( "context" + "slices" "strings" "github.com/optimode/emailkit/internal/disposable" @@ -12,9 +13,10 @@ import ( // DomainConfig is the domain checker configuration. type DomainConfig struct { - CheckDisposable bool - CheckTypos bool - TypoThreshold int + CheckDisposable bool + CheckTypos bool + TypoThreshold int + AdditionalKnownProviders []string } // DomainChecker detects disposable domains and typos. @@ -46,7 +48,7 @@ var defaultKnownProviders = []string{ func NewDomainChecker(cfg DomainConfig) *DomainChecker { return &DomainChecker{ cfg: cfg, - knownProviders: defaultKnownProviders, + knownProviders: slices.Concat(defaultKnownProviders, cfg.AdditionalKnownProviders), } } diff --git a/check/domain_test.go b/check/domain_test.go new file mode 100644 index 0000000..5ad6736 --- /dev/null +++ b/check/domain_test.go @@ -0,0 +1,79 @@ +package check_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/optimode/emailkit/check" + "github.com/optimode/emailkit/internal/parse" +) + +func TestDomainChecker(t *testing.T) { + ctx := context.Background() + + t.Run("typo detection with default providers", func(t *testing.T) { + c := check.NewDomainChecker(check.DomainConfig{ + CheckTypos: true, + TypoThreshold: 2, + }) + + tests := []struct { + email string + expected string + }{ + {"user@gmall.com", "gmail.com"}, + {"user@hotmial.com", "hotmail.com"}, + {"user@gmail.com", ""}, // exact match + } + + for _, tt := range tests { + t.Run(tt.email, func(t *testing.T) { + parsed := parse.NewEmail(tt.email) + result := c.Check(ctx, parsed) + assert.True(t, result.Passed) + assert.Equal(t, tt.expected, result.Suggestion) + }) + } + }) + + t.Run("typo detection with additional providers", func(t *testing.T) { + c := check.NewDomainChecker(check.DomainConfig{ + CheckTypos: true, + TypoThreshold: 2, + AdditionalKnownProviders: []string{"yahoo.es"}, + }) + + tests := []struct { + email string + expected string + }{ + {"user@yahoo.es", ""}, + {"user@yahooo.es", "yahoo.es"}, + {"user@gmail.com", ""}, // still matches default + {"user@gmall.com", "gmail.com"}, // still matches default + } + + for _, tt := range tests { + t.Run(tt.email, func(t *testing.T) { + parsed := parse.NewEmail(tt.email) + result := c.Check(ctx, parsed) + assert.True(t, result.Passed) + assert.Equal(t, tt.expected, result.Suggestion) + }) + } + }) + + t.Run("disposable check", func(t *testing.T) { + c := check.NewDomainChecker(check.DomainConfig{ + CheckDisposable: true, + }) + + // 10minutemail.com is a known disposable domain + parsed := parse.NewEmail("user@10minutemail.com") + result := c.Check(ctx, parsed) + assert.False(t, result.Passed) + assert.Contains(t, result.Details, "disposable") + }) +} diff --git a/options.go b/options.go index e9878ae..e0ebd29 100644 --- a/options.go +++ b/options.go @@ -27,6 +27,8 @@ type DomainOptions struct { CheckTypos bool // TypoThreshold is the Levenshtein distance threshold for typo detection. Default: 2 TypoThreshold int + // AdditionalKnownProviders is a list of custom domains that will be merged with the default list. + AdditionalKnownProviders []string } func defaultDomainOptions() DomainOptions { diff --git a/validator.go b/validator.go index 4b4618c..55cff97 100644 --- a/validator.go +++ b/validator.go @@ -68,9 +68,10 @@ func (v *Validator) WithDomain(opts ...DomainOptions) *Validator { o = opts[0] } v.checkers = append(v.checkers, check.NewDomainChecker(check.DomainConfig{ - CheckDisposable: o.CheckDisposable, - CheckTypos: o.CheckTypos, - TypoThreshold: o.TypoThreshold, + CheckDisposable: o.CheckDisposable, + CheckTypos: o.CheckTypos, + TypoThreshold: o.TypoThreshold, + AdditionalKnownProviders: o.AdditionalKnownProviders, })) return v }