Skip to content

Instantly share code, notes, and snippets.

@ajbeach2
Created June 19, 2015 20:48
Show Gist options
  • Save ajbeach2/8d3e884be0e0923fefa2 to your computer and use it in GitHub Desktop.
Save ajbeach2/8d3e884be0e0923fefa2 to your computer and use it in GitHub Desktop.
class DomainValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless domain_check(value)
record.errors[attribute] << (options[:message] || "is not a valid domain")
end
end
def domain_check(name)
input = PublicSuffix.parse(name)
domain = input.domain
PublicSuffix.valid?(domain)
end
end
class Domain < ActiveRecord::Base
validates :name, uniqueness: true, domain: true, presence: true, length: { maximum: 50 }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment