Created
May 10, 2012 19:19
-
-
Save adamcrown/2655247 to your computer and use it in GitHub Desktop.
Relative 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 RelativeUriValidator < ActiveModel::EachValidator | |
def validate_each(record, attribute, value) | |
begin | |
unless URI.parse(value).relative? | |
record.errors[attribute] << 'is not a relative 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