-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, never mind — I see that |
||
record.errors.add attribute, (options[:message] || "is not valid") | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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 nameEmailValidator
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 theseemail: true
arguments feel like too much "Rails magic" too meThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍