docbrown/app/validators/valid_domain_csv_validator.rb
Jeremy Friesen 9687760056
Updating valid domain registration (#15436)
* Updating valid domain registration

Prior to this commit, our regular expression did not account for the `-`
character as valid within the domain.  The `-` character cannot be the
first nor last character of the domain (e.g. `-hello.com` nor
`hello-.com` are invalid but `hell-o.com` is valid).

In addition I tidied up the default value of the
`blocked_registration_email_domains` to match it's sibling `allowed_registration_email_domains`.

This relates to the spammer seo-hunt.com

* Adding validator domain validator spec

* Moving request spec to unit spec

This change does two things:

1) Allows for 2 character domains
2) Moves the spec for 2 character domains from an expensive spec to a
   cheaper to run spec (e.g., request-cycle to unit-test validator)

It preserves PR #12268
2021-11-19 10:05:00 -05:00

16 lines
697 B
Ruby

# @note While the validator implies a CSV, the implementation is that
# we have an array. Upstream implementors likely accept a CSV
# and coerce it into an array. See Authentication::Base for an
# example of coercing the CSV into an array.
class ValidDomainCsvValidator < ActiveModel::EachValidator
DEFAULT_MESSAGE = "must be a comma-separated list of valid domains".freeze
VALID_DOMAIN = /^[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/
def validate_each(record, attribute, value)
return unless value
return if value.all? { |domain| domain.match?(VALID_DOMAIN) }
record.errors.add(attribute, options[:message] || DEFAULT_MESSAGE)
end
end