Skip to content

Commit

Permalink
Merge pull request #13 from charles-jan/master
Browse files Browse the repository at this point in the history
Email validator add custom options support
  • Loading branch information
jclusso authored Mar 18, 2024
2 parents ee0af31 + 2abc916 commit bfec899
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/emailable/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ def validate_each(record, attribute, value)
error ||= :disposable if ev.disposable? && !disposable
error ||= :accept_all if ev.accept_all? && !accept_all

record.errors.add(attribute, error) if error
if error
error_options = options.except(
:smtp, :states, :free, :role, :disposable, :accept_all, :timeout
)
record.errors.add(attribute, error, **error_options)
end
rescue Emailable::Error
# silence errors
end
Expand Down
16 changes: 14 additions & 2 deletions test/email_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class EmailValidatorTest < Minitest::Test

def user_class(
smtp: true, states: %i[deliverable risky unknown], free: true, role: true,
accept_all: true, disposable: true, timeout: 3
accept_all: true, disposable: true, timeout: 3, **options
)
Class.new do
include ActiveModel::Model
Expand All @@ -15,7 +15,7 @@ def user_class(
smtp: smtp, states: states,
free: free, role: role, disposable: disposable, accept_all: accept_all,
timeout: timeout
}
}.merge(options)

def self.name
'TestClass'
Expand Down Expand Up @@ -83,4 +83,16 @@ def test_timeout_option
assert_raises(ArgumentError) { invalid_user2.valid? }
end

def test_custom_option
message = 'invalid message'
invalid_user = user_class(message: message, reportable: true).new(
email: '[email protected]'
)

refute invalid_user.valid?
assert invalid_user.errors[:email].present?
assert_equal message, invalid_user.errors[:email].first
assert invalid_user.errors.where(:email, reportable: true).present?
end

end

0 comments on commit bfec899

Please sign in to comment.