GithubHelp home page GithubHelp logo

alphanodes / valid_hostname Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 269 KB

Extension to ActiveModel for validating hostnames

License: MIT License

Ruby 100.00%
gem hostname validation validator ruby

valid_hostname's Introduction

Valid Hostname

Run Rubocop Tests Gem Version

Description

Extension to ActiveModel for validating hostnames and domain names.

Features

  • Adds validation for hostnames to ActiveModel
  • Supports I18n for the error messages

Installation

in Gemfile

gem 'valid_hostname', '~> 2.0'

Run bundler

  bundle install

Validations performed

  • maximum length of hostname is 255 characters
  • maximum length of each hostname label is 63 characters
  • characters allowed in hostname labels are a-z, A-Z, 0-9 and hyphen
  • labels do not begin or end with a hyphen
  • labels do not consist of numeric values only

Options

  • option to allow for underscores in hostname labels
  • option to require that the last label is a valid TLD (ie. require that the name is a FQDN)
  • option to allow numeric values in the first label of the hostname (exception: the hostname cannot consist of a single numeric label)
  • option to specify a list of valid TLDs
  • options to allow for wildcard hostname in first label (for use with DNS)
  • option to configure error message. verbose: true creates detailed error messages. (only used by )

See also https://www.zytrax.com/books/dns/apa/names.html

How to use

Simple usage

  class Record < ActiveRecord::Base
    validates :name, hostname: true
  end

With options

  class Record < ActiveRecord::Base
    validates :name, hostname: { OPTIONS }
  end

or static usage

  ValidateHostname.valid?('localhost')

or static usage with options

  ValidateHostname.valid?('localhost', OPTIONS)

Options and their defaults

  • allow_underscore: false
  • require_valid_tld: false
  • valid_tlds: Array of allowed TLDs (can only be used with require_valid_tld: true)
  • allow_numeric_hostname: false
  • allow_wildcard_hostname: false
  • allow_root_label: false
  • verbose: true

Examples

Without options

  class Record < ActiveRecord::Base
    validates :host, hostname: true
  end
  >> @record = Record.new :name => 'horse'
  >> @record.save
  => true
  >> @record2 = Record.new :name => '_horse'
  >> @record2.save
  => false

With :allow_underscore

  class Record < ActiveRecord::Base
    validates :name, hostname: { allow_underscore: true }
  end
  >> @record3 = Record.new :name => '_horse'
  >> @record3.save
  => true

With :require_valid_tld

  class Record < ActiveRecord::Base
    validates :name, hostname: { require_valid_tld: true }
  end
  >> @record4 = Record.new :name => 'horse'
  >> @record4.save
  => false
  >> @record5 = Record.new :name => 'horse.com'
  >> @record5.save
  => true

With :valid_tlds

  class Record < ActiveRecord::Base
    validates :name, hostname: { :require_valid_tld, valid_tlds: %w[com org net] }
  end
  >> @record6 = Record.new :name => 'horse.info'
  >> @record6.save
  => false

With :allow_numeric_hostname

  class Record < ActiveRecord::Base
    validates :name, hostname: { allow_numeric_hostname: false }
  end
  >> @record7 = Record.new :name => '123.info'
  >> @record7.save
  => false

With :allow_wildcard_hostname

  class Record < ActiveRecord::Base
    validates :name, hostname: { allow_wildcard_hostname: true }
  end
  >> @record8 = Record.new :name => '*.123.info'
  >> @record8.save
  => true

Extra validators

domainname

  • sets require_valid_tld: true
  • sets allow_numeric_hostname: true
  • returns error if there is only one label and this label is numeric

Credits

This gem is a fork of https://github.com/KimNorgaard.

Copyright

Copyright (c) 2009-2022 Kim Norgaard. See LICENSE for details Copyright (c) 2022 Alexander Meindl. See LICENSE for details

valid_hostname's People

Contributors

alexandermeindl avatar kimnorgaard avatar krtschmr avatar lowjoel avatar nadnoslen avatar sshaw avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.