Class: EnvValidator::Types::URL

Inherits:
Base
  • Object
show all
Defined in:
lib/env_validator/types.rb

Instance Method Summary collapse

Instance Method Details

#coerce(value) ⇒ Object



90
91
92
# File 'lib/env_validator/types.rb', line 90

def coerce(value)
  URI.parse(value.to_s)
end

#validate(value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/env_validator/types.rb', line 74

def validate(value)
  uri = URI.parse(value.to_s)

  # Must have a scheme and host for a valid URL
  raise TypeError, "Expected valid URL with scheme and host, got #{value.inspect}" unless uri.scheme && uri.host

  # Must be HTTP or HTTPS
  unless %w[http https].include?(uri.scheme.downcase)
    raise TypeError, "Expected HTTP or HTTPS URL, got #{value.inspect}"
  end

  true
rescue URI::InvalidURIError
  raise TypeError, "Expected valid URL, got #{value.inspect}"
end