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

Ability to validate against the normalized input #64

Merged
merged 1 commit into from
Oct 23, 2014

Conversation

dimroc
Copy link
Contributor

@dimroc dimroc commented Oct 22, 2014

Useful in the scenario when you want to allow

#10 digits are extremely common in the US
555 555 5555

even though it would fail Phony.plausible?().

After normalizing for “US”, this would pass.
i.e. Phony.plausible?(PhonyRails.normalize_number)

Usage looks like:

validates_plausible_phone :phone_number, normalized_country_code: 'US'

Useful in the scenario when you want to allow
555 555 5555 # 10 digits are extremely common in the US
even though it would fail Phony.plausible?().

After normalizing for “US”, this would pass.
i.e. Phony.plausible?(PhonyRails.normalize_number)
@joost
Copy link
Owner

joost commented Oct 23, 2014

Seems nice addition but can't you just put the validation on the normalized version?

@dimroc
Copy link
Contributor Author

dimroc commented Oct 23, 2014

That's what I had done at first but it allowed bad data in the phone_number because normalization would blank out the field which in turn would pass validation. And I still do want the field to allow_blank:

  phony_normalize :phone_number, as: :phone_number_normalized, :default_country_code => 'US'
  validates_plausible_phone :phone_number_normalized

Input "xxxx" as phone_number will pass validation, saving "xxxx" in phone_number and saving blank in phone_number_normalized. My PR prevents this.

This was also an issue for me here:

  phony_normalize :phone_number, :default_country_code => 'US'
  validates_plausible_phone :phone_number

image

@joost
Copy link
Owner

joost commented Oct 23, 2014

What if you add something like:

validates :phone_number, presence: true

So nil or blank is not allowed?!

@dimroc
Copy link
Contributor Author

dimroc commented Oct 23, 2014

I either want a plausible phone number, or no phone number at all. Blank or a plausible phone number. I'm not sure how that line would help me.

@dimroc
Copy link
Contributor Author

dimroc commented Oct 23, 2014

The crux of the issue is this: when normalizing fails, it blanks out the field, so allowing blank will allow entries that fail normalization.

joost pushed a commit that referenced this pull request Oct 23, 2014
Ability to validate against the normalized input
@joost joost merged commit 08f6949 into joost:master Oct 23, 2014
@joost
Copy link
Owner

joost commented Oct 23, 2014

👍 merged will push new gem soon

@joost
Copy link
Owner

joost commented Oct 23, 2014

This now is in v0.7.3.

@dimroc
Copy link
Contributor Author

dimroc commented Oct 23, 2014

Just updated the gem and it all worked great. Thanks.

On Thu, Oct 23, 2014 at 10:44 AM, Joost Hietbrink [email protected]
wrote:

This now is in v0.7.3.


Reply to this email directly or view it on GitHub
#64 (comment).

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.

2 participants