2019-08-08 21:04:19 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class DomainValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
2022-10-18 04:56:29 +00:00
|
|
|
return unless value.is_a?(String)
|
2019-08-08 21:04:19 +00:00
|
|
|
|
2019-08-30 00:19:17 +00:00
|
|
|
domain = begin
|
|
|
|
if options[:acct]
|
2022-10-18 04:56:29 +00:00
|
|
|
value.split('@')[1]
|
2019-08-30 00:19:17 +00:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-18 04:56:29 +00:00
|
|
|
return if value.blank?
|
|
|
|
|
2019-08-30 00:19:17 +00:00
|
|
|
record.errors.add(attribute, I18n.t('domain_validator.invalid_domain')) unless compliant?(domain)
|
2019-08-08 21:04:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def compliant?(value)
|
2022-10-18 04:56:29 +00:00
|
|
|
value.match?(Twitter::TwitterText::Regex[:valid_domain])
|
2019-08-08 21:04:19 +00:00
|
|
|
end
|
|
|
|
end
|