Skip to content

Instantly share code, notes, and snippets.

@adamcrown
Created May 10, 2012 19:19
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/2655247 to your computer and use it in GitHub Desktop.
Save adamcrown/2655247 to your computer and use it in GitHub Desktop.
Relative URI Validator
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