Skip to content

Commit 4fda047

Browse files
Align host validation with Profile
1 parent cd750e6 commit 4fda047

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

app/models/school_email_domain.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,25 @@ def validate_domain
1818
value = "http://#{value}" unless %r{\A[a-z][a-z0-9+\-.]*://}i.match?(value)
1919
uri = URI.parse(value)
2020
host = uri.host&.delete_suffix('.')
21-
return if host.blank?
21+
22+
validate_host(host)
23+
rescue URI::InvalidURIError
24+
errors.add(:domain, :invalid)
25+
end
26+
27+
def validate_host(host)
28+
accounts_host_format =
29+
/\A\s*(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}\s*\z/i
30+
31+
unless host&.match?(accounts_host_format)
32+
errors.add(:domain, :invalid)
33+
return
34+
end
2235

2336
if PublicSuffix.valid?(host)
2437
self.domain = host
2538
else
2639
errors.add(:domain, :invalid)
2740
end
28-
rescue URI::InvalidURIError
29-
errors.add(:domain, :invalid)
3041
end
3142
end

spec/models/school_email_domain_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@
143143
context 'with an invalid domain' do
144144
it { is_expected.not_to allow_value('').for(:domain) }
145145
it { is_expected.not_to allow_value(' ').for(:domain) }
146+
it { is_expected.not_to allow_value('http://').for(:domain) }
146147
it { is_expected.not_to allow_value('edu').for(:domain) }
147148
it { is_expected.not_to allow_value('com').for(:domain) }
148149
it { is_expected.not_to allow_value('co.uk').for(:domain) }
149150
it { is_expected.not_to allow_value('http://invalid uri').for(:domain) }
151+
it { is_expected.not_to allow_value('-wrong.edu').for(:domain) }
150152
end
151153
end

0 commit comments

Comments
 (0)