diff options
author | Reverite <github@reverite.sh> | 2019-08-13 15:45:40 -0700 |
---|---|---|
committer | Reverite <github@reverite.sh> | 2019-08-13 15:45:40 -0700 |
commit | 2e221bd5b6472d1eecb654b3518af7886d3dadaf (patch) | |
tree | afb9e3a86ba3a93b6b5f04cdec6070351992fed4 /app/validators/domain_validator.rb | |
parent | 9528d3eda280ffac20a18c6f21bfc51f594e2c86 (diff) | |
parent | b859eb001717dfc62aebb8eba47b84c75aebe4ef (diff) |
Merge branch 'glitch' into production
Diffstat (limited to 'app/validators/domain_validator.rb')
-rw-r--r-- | app/validators/domain_validator.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/app/validators/domain_validator.rb b/app/validators/domain_validator.rb new file mode 100644 index 000000000..ae07f1798 --- /dev/null +++ b/app/validators/domain_validator.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class DomainValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + return if value.blank? + + record.errors.add(attribute, I18n.t('domain_validator.invalid_domain')) unless compliant?(value) + end + + private + + def compliant?(value) + Addressable::URI.new.tap { |uri| uri.host = value } + rescue Addressable::URI::InvalidURIError + false + end +end |