mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-23 00:06:21 +00:00
22 lines
616 B
Ruby
22 lines
616 B
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
# Valida URLs
|
||
|
#
|
||
|
# @see {https://storck.io/posts/better-http-url-validation-in-ruby-on-rails/}
|
||
|
class UrlValidator < ActiveModel::EachValidator
|
||
|
def validate_each(record, attribute, value)
|
||
|
if value.blank?
|
||
|
record.errors.add(attribute, :url_missing)
|
||
|
return
|
||
|
end
|
||
|
|
||
|
uri = URI.parse(value)
|
||
|
|
||
|
record.errors.add(attribute, :scheme_missing) if uri.scheme.blank?
|
||
|
record.errors.add(attribute, :host_missing) if uri.host.blank?
|
||
|
record.errors.add(attribute, :path_missing) if uri.path.blank?
|
||
|
rescue URI::Error
|
||
|
record.errors.add(attribute, :invalid)
|
||
|
end
|
||
|
end
|