How to add a User model validation to block certain email domains - ruby-on-rails

I would like to have a list of email domains that are validated against to prevent from registering on my app.
10minutemail.com, yopmail.com, mail.com, mail.ru etc...
I have a list of domains in my user model like so:
BAD_DOMAINS = [/10minutemail.com/, /yopmail.com/, /mail/
I would like to add a User validates on the email field to add an error if the user is registering with one of these domains.
BAD_DOMAINS.each { |rule| return true if !domain.match(rule).nil? }
I have that regex working, but how do I add that as a validates? I tried this:
validates :email, :format => { : exclusion => BAD_DOMAINS,
:message => "%{value} no good." }
Thanks

You need to combine all of your separate regular expressions into a singular one, but you might find it's easier to do that if you have a list of strings instead of a list of regular expressions:
EXCLUSION_DOMAINS = %w[
example.com
test.com
asdf.com
]
EXCLUSION_REGEXP = Regexp.new('(?:' + EXCLUSION_DOMAINS.collect { |d| Regexp.escape(d) }.join('|') + ')$')
You'll want to ensure that things don't match this, so it's a little different to use:
validates :email,
:format => {
:with => VALID_EMAIL_REGEXP,
:without => EXCLUSION_REGEXP,
:message => "%{value} no good."
}
You should use some kind of valid email tester as well to be sure the address is plausible. It's expressed here as VALID_EMAIL_REGEXP which is some kind of email validator regular expression. Try and use an RFC compliant one if you do that.

Related

How do I validate_uniqueness_of when scoped to two attributes only when one attribute on matching existing records does not equal a specific value?

I have an Invitation model that represents an invitation to join a subscription. There should only be one Invitation at any given time with a specific email / subscription_id combination unless the other records with the matching email / subscription_id also have a state of 'declined'.
I can currently validate for uniqueness given that the email and subscription_id combination is unique:
My Invitation model:
validates :email, :uniqueness => { :scope => :subscription_id }
Rspec (passes):
it { should validate_uniqueness_of(:email).scoped_to(:subscription_id) }
However, I want to skip the uniqueness check if the matching model(s) in the database have a state that is equal to 'declined'.
If the existing model's state is 'declined', the validation should pass.
The first thing that comes to mind is:
validates :email, :uniqueness => { :scope => :subscription_id },
:unless => lambda { |asset| asset.state == 'declined' }
But this is wrong because it checks if the newly created model has a state of 'declined', I want to check if the previously existing records have a state of 'declined'.
I also tried this:
validates :email, :uniqueness => { :scope => :subscription_id, :message => 'subscriptionery do' },
:if => lambda { |asset| asset.state == 'declined' }
But that fails for what I assume is the same reason.
How would I write a validation that checks an additional scope?
I feel like writing something like the following, but this is just made up syntax to help explain my idea:
it { should validate_uniqueness_of(:email).scoped_to(:subscription_id) }
unless MyModel.where(:email == new_object.email,
:subscription_id == new_object.subscription_id,
:state == 'declined')
Update:
I did this and it worked:
validates :email, uniqueness: { scope: :subscription_id, message: 'The email address %{value} is already associated with this subscription.' }, if: :state_of_others_are_not_declined?, on: :create
def state_of_others_are_not_declined?
Invitation.where(email: email).where(subscription_id: subscription_id).where.not(state: 'declined').any?
end
How does this work for you;
validate :unique_email_with_subscription_and_state
def unique_email_with_subscription_and_state
errors.add(:email,"YOUR MESSAGE") if Invitation.where(email: self.email, subscription_id: self.subscription_id).where.not(state: 'declined').any?
end
This will select all Invitiations where the the email matches, subscription_id matches and the state is not declined. If it finds any it will add an error to :email. Something like this
"SELECT invitations.* FROM invitatations WHERE email = 'me#example.com' AND subscription_id = 2 AND state <> 'declined'"
Is that the desired result?

Validate User email to match one of two regex's?

In my Rails app college students with either #berkeley.edu or #uw.edu email addresses can register. I have the regex for validating both ready but since I need to check the email address the user enters to see which one it matches I think I need to create one regex, but I don't know how. Here are my two regex's:
berkeley_regex = /\A[\w+\-.]+#berkeley\.edu\z/i
uw_regex = /\A[\w+\-.]+#uw\.edu\z/i
And my validate:
validates :email, :presence => true, :uniqueness => true, :format => {:with => berkeley_regex}
Now, what would the regex to check against both but only match against one look like?
Can't you just validate against something like /\A[\w+\-.]+#(berkeley|uw)\.edu\z/i and be done with it? If you really need to later determine which it is, make a method that just checks the back part, or returns the match, or whatever...
First I think your regex should be changed from [\w+\-.] to [\w+\-\.]
The validation could be
validates :email, format: { with: "\A#{berkely_regex}|#{uw_regex}\z/i" }
but you'll need to remove the flags ( \A, \z, /i ) from the vartiables

Model validation, validate a string is an integer

Hi I have a model called users, and they have a cell phone attribute that must be entered as a 12 digit string leading with "+". I want to validate that the attribute they entered from a form is in fact in the correct format. Example "+11234567890" should save to the model, but "1232h" will not.
This is what I have in my model, but it does not work:
validates :cell, :format => { :with => /^[-+]?[0-9]+$/,
:message => "Only numbers allowed" }, :length => { :is => 10 }
Try this:
validates :cell, :format => { :with => /\A(\+1)?[0-9]{10}\z/, :message => "Not a valid 10-digit telephone number" }
I think the problem is that you're trying to do two different kinds of validations on a single validates line. The above combines the length constraint with the number constraint, and allows the variable +/- without messing up the length constraint.

Simple rails format validation not firing

I'm building an app where users can create url slugs for their profile. To make sure the slugs are valid I've added a validation in the User model for slugs:
validates :slug, :uniqueness => true, :format => { :with => /[a-z]+/ }, :allow_nil => true, :allow_blank => true
However, validation seems to pass, regardless of what format the slug string is, for example:
u.slug = 'jlskdf .jc oi/slkdjfie\*asdf&(*&*ss%&'
=> "jlskdf .jc oi/slkdjfie\\*asdf&(*&*ss%&"
u.save
=> true
Apparently it doesn't matter what I change the regex to either, everything passes. I've tried this format as well:
validates_format_of :slug, :with => /[a-z]+/
which gives the same results. Anyone have any ideas of what could be happening?
Your regular expression isn't anchored, so the pattern matches as long as it contains at least one letter a-z. Anything else is valid. Add \A and \z to the beginning and end to prevent matching any substring within the larger input.
:with => /\A[a-z]+\z/

Rails custom validation based on a regex?

I have the following regex that I use in my routes.rb for /type-in-something-here
# A-Z, a-z, 0-9, _ in the middle but never starting or ending in a _
# At least 5, no more than 500 characters
In the routes this works well as:
match ':uuid' => 'room#show', :constraints => { :uuid => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/ }
I want to have this also as a validation so invalid records aren't created. So I added the following to room.rb:
validates_format_of :uuid, :with => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/i, :message => "Invalid! Alphanumerics only."
But this validates_format_of isn't working, and instead of adding an error it's allow the record to save.
Any ideas what's wrong?
Thanks
For validation purposes, remember to add the beginning and end of string markers \A and \Z:
validates_format_of :uuid, :with => /\A[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?\Z/i
Otherwise your regex will happily match any string that contains at least a letter or a digit. For some reason Rails implicitly adds the boundaries in the routes. (Probably because it embeds the regex inside a larger one to match the entire URL, with explicit checks for / and the end of the URL.)
using something like this
validates :uuid, :format => {:with => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/i},
:message => "your message"
For more check this
validates :name, format: { with: /\A[a-zA-Z]+\z/,
message: "Only letters are allowed" }

Resources