Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

827 : emails validation in person #965

Merged
merged 2 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Person < ApplicationRecord
accepts_nested_attributes_for :location

validate :preferred_contact_method_present!
validates :email, email: true
validates :email_2, email: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the validates_with methods a little better, because they use a class name. If the class name EmailValidator is here in this validation line, someone reading this code will have to do less thinking to guess where the logic exists. I'm sure there's other ways to write this that accomplish the same thing. What I care about is that these email: true arguments feel like too much "Rails magic" too me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍


def name_and_email
"#{name} (#{email})"
Expand Down
8 changes: 8 additions & 0 deletions app/validators/email_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.nil? || value.strip.empty?
unless /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i.match?(value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@exbinary went to see if any of our dependencies already have an email regex.

The Devise gem includes an email regex, but I think that one is way too permissive.

@exbinary pointed out that Ruby's standard library includes URI::MailTo::EMAIL_REGEXP, which may be good enough for us. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, never mind — I see that URI::MailTo::EMAIL_REGEXP would permit the bad example described in the issue

record.errors.add attribute, (options[:message] || "is not valid")
end
end
end
14 changes: 14 additions & 0 deletions spec/models/person_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@
end
end

describe 'email validation' do
let(:contact_method) { build :contact_method, name: 'Email', field: 'email' }
subject(:person) { build :person, preferred_contact_method: contact_method, email: 'test@missingtld' }

context 'when the email field is not valid' do
it { is_expected.not_to be_valid }

it 'generates an error on the correct field' do
person.valid?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think these tests would be better as tests on the validator itself instead of on the model?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

expect(person.errors.messages).to eq({email: ['is not valid']})
end
end
end

describe "#anonymized_name_and_email" do
it "returns blank if name and email are empty" do
person = build(:person, name: nil, email: nil)
Expand Down