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

Sanitize email when name has special characters #2026

Conversation

martinjaimem
Copy link
Contributor

Resolves #1974

Description:

This pull request fixes a small edge case when faking an email with a name param with special characters. Lets say, Faker::Internet.email(name: 'martín'), should not resolve to martín@example.com as it is not a valid email.

With that in mind, the fix replaces the forbidden character with a #. That decision was made basically trying to keep the number of characters in the name params.

I'm open to any suggestions not only regarding the replacement character (#) but also the way this pr is written. Also, willing to add more tests if needed

char_range.include?(char) ? char : '#'
end.join

[sanitized_local_part, domain_name].join('@')
Copy link
Contributor

Choose a reason for hiding this comment

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

to me sanitize doesn't mean that it will construct the email as well. Can we instead pass a full email string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, the naming isn't clear. What do you think about changing the name to construct_email?

That way we sanitize it and construct it in the method, so we avoid the repeated [..., ...].join('@') logic

Copy link
Contributor

Choose a reason for hiding this comment

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

Still feels like sperate concepts. The generators can probably continue to do the construction of the email addresses, and then pass it to sanitize_email before being returned

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

I think construct_email works better because it describes the functionality of the method better. Sanitization is the process of cleaning or filtering input data. Hence, single-responsibility principle comes into my mind.

@martinjaimem martinjaimem force-pushed the fix/sanitize-email-for-spec-char-username branch from 54543c5 to 384e602 Compare May 31, 2020 03:55
@Zeragamba
Copy link
Contributor

construct_email probably could now call sanitize but I'm not going to hold this PR up over a minor change. Looks good enough to me :)

@Zeragamba Zeragamba merged commit 74e27ff into faker-ruby:master Jun 8, 2020
@Zeragamba
Copy link
Contributor

Thanks!

@martinjaimem martinjaimem deleted the fix/sanitize-email-for-spec-char-username branch June 10, 2020 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Faker::Internet.email generates email address with umlauts
4 participants