Agile Web Development

Build it. Launch it. Love it.

Validates as hostname label

Checks for valid hostname labels by looking for:

  • Length between 1 and 63 characters long
  • Alphanumeric, hyphen, and (optionally) underscore characters
  • 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 validates_as_hostname_label
  OR
  script/plugin install git://github.com/shuber/validates_as_hostname_label.git

Usage

  class Account < ActiveRecord::Base
    validates_as_hostname_label :subdomain
  end

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

You may optionally pass a :reserved option which should be an array of hostname labels to exclude, otherwise ValidatesAsHostnameLabel.default_options[:reserved] 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 standard validation options :if, :unless, and :allow_blank work as well.

Internationalization

Uses the following I18n keys:

  • validates_as_hostname_label.invalid_format
  • validates_as_hostname_label.invalid_length
  • validates_as_hostname_label.invalid_prefix_or_suffix
  • validates_as_hostname_label.reserved

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but
     bump version in a commit by itself I can ignore when I pull)
    
  • Send me a pull request. Bonus points for topic branches.

Contact

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

Vitals

Home http://rdoc.info/projects/shuber/validates_as_hostname_label
Repository git://github.com/shuber/validates_as_hostname_label.git
License Rails' (MIT)
Tags Tag_red
Rating (0 votes)
Created 16 March 2010

Comments

Add a comment