artists: validate that urls are well-formed (fix #2346).

This commit is contained in:
evazion
2017-07-29 01:37:30 -05:00
parent 10614d2152
commit 624444d51d
2 changed files with 15 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ class ArtistUrl < ApplicationRecord
before_save :initialize_normalized_url, on: [ :create ]
before_save :normalize
validates_presence_of :url
validate :validate_url_format
belongs_to :artist, :touch => true
attr_accessible :url, :artist_id, :normalized_url
@@ -65,4 +66,11 @@ class ArtistUrl < ApplicationRecord
def to_s
url
end
def validate_url_format
uri = Addressable::URI.parse(url)
errors[:base] << "'#{url}' must begin with http:// or https://" if !uri.scheme.in?(%w[http https])
rescue Addressable::URI::InvalidURIError => error
errors[:base] << "'#{url}' is malformed: #{error}"
end
end