diff options
author | ThibG <thib@sitedethib.com> | 2019-08-12 23:01:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-12 23:01:19 +0200 |
commit | cf421bafdf644f8c8a44b4f06f82841cbd568cdf (patch) | |
tree | 4d1f3b2c9bdcadcf0fe53372a75ba54c0c06875a /app/validators | |
parent | aa485d6f055b93fd7a9df8b47ed96122b38af39e (diff) | |
parent | cfca28bc874125e63e7e1a3c96d94bd5a7eca108 (diff) |
Merge pull request #1196 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/validators')
-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 |