Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions check/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package check

import (
"context"
"slices"
"strings"

"github.com/optimode/emailkit/internal/disposable"
Expand All @@ -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.
Expand Down Expand Up @@ -46,7 +48,7 @@ var defaultKnownProviders = []string{
func NewDomainChecker(cfg DomainConfig) *DomainChecker {
return &DomainChecker{
cfg: cfg,
knownProviders: defaultKnownProviders,
knownProviders: slices.Concat(defaultKnownProviders, cfg.AdditionalKnownProviders),
}
}

Expand Down
79 changes: 79 additions & 0 deletions check/domain_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}
2 changes: 2 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading