Skip to content

Instantly share code, notes, and snippets.

@adamcrown
Created May 10, 2012 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrown/2655237 to your computer and use it in GitHub Desktop.
Save adamcrown/2655237 to your computer and use it in GitHub Desktop.
Absolute URI validator
require 'uri'
class AbsoluteUriValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
begin
unless URI.parse(value).absolute?
record.errors[attribute] << 'is not an absolute URL'
end
rescue URI::InvalidURIError => error
record.errors[attribute] << 'is not a valid URL'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment