Created
May 10, 2012 19:17
-
-
Save adamcrown/2655237 to your computer and use it in GitHub Desktop.
Absolute URI validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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