Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Checks for valid hostname labels

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 CHANGELOG
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README.markdown
Octocat-spinner-32 Rakefile
Octocat-spinner-32 init.rb
Octocat-spinner-32 validates_as_hostname_label.gemspec
README.markdown

validates_as_hostname_label

Checks for valid hostname labels by looking for:

  • Length between 1 and 63 characters long
  • Letters 'a' through 'z' (case-insensitive), the digits '0' through '9', and the hyphen (and optionally the underscore)
  • Labels that don't begin or end with a hyphen or underscore
  • Reserved labels

See http://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names

Installation

gem install shuber-validates_as_hostname_label --source http://gems.github.com
OR
script/plugin install git://github.com/shuber/validates_as_hostname_label.git

Usage

class Account < ActiveRecord::Base
  validates_as_hostname_label :subdomain
end

You may optionally pass a :reserved option which should be an array of hostname labels to exclude, otherwise Huberry::ValidatesAsHostnameLabel::RESERVED_HOSTNAMES will be used.

class Account < ActiveRecord::Base
  validates_as_hostname_label :subdomain, :reserved => ['www', 'ftp', 'mail', 'pop']
end

@account = Account.new :subdomain => 'www'
@account.save # false
@account.errors # { :subdomain => 'is reserved' }

Also accepts an :allow_underscores option which defaults to false

class Account < ActiveRecord::Base
  validates_as_hostname_label :subdomain, :allow_underscores => true
end

@account = Account.new :subdomain => 'test_ing'
@account.save # true

The usual validation options (like :if, :unless, :allow_nil, etc) work as well

Contact

Problems, comments, and suggestions all welcome: shuber@huberry.com

Something went wrong with that request. Please try again.