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

Conversation

vurtn
Copy link
Contributor

@vurtn vurtn commented May 22, 2021

Sorry for my english.

Why

We need to validate emails in person (#827).

What

  • Create a validator
  • Add a test in person_spec.rb

How

The validator is in the guide about validations.

Testing

A test is available now in person_spec.rb.
email-not-valid

Next Steps

Unknown.

Outstanding Questions, Concerns and Other Notes

Not seem to be concerned.

Accessibility

Not seem to be concerned.

Security

Not seem to be concerned.

Meta

Not seem to be concerned.

Pre-Merge Checklist

I think the reviewer will check this so i let it empty.

  • Security & accessibility have been considered
  • Tests have been added, or an explanation has been given why the features cannot be tested
  • Documentation and comments have been added to the codebase where required
  • Entry added to CHANGELOG.md if appropriate
  • Outstanding questions and concerns have been resolved
  • Any next steps have been turned into Issues or Discussions as appropriate

Copy link
Collaborator

@h-m-m h-m-m left a comment

Choose a reason for hiding this comment

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

Thanks for writing this PR! I'm very happy to see all the work you've been contributing, and I like your approach to solving this problem

Comment on lines 26 to 27
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 👍

Comment on lines 31 to 35
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 👍

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

@h-m-m
Copy link
Collaborator

h-m-m commented May 29, 2021

I was able to pull this branch to my local dev environment, where it worked with no problems!

@solebared solebared merged commit f6acb8e into rubyforgood:main May 29, 2021
@solebared solebared mentioned this pull request Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants