https://github.com/alphanodes/valid_hostname
Extension to ActiveModel for validating hostnames
https://github.com/alphanodes/valid_hostname
gem hostname ruby validation validator
Last synced: about 1 year ago
JSON representation
Extension to ActiveModel for validating hostnames
- Host: GitHub
- URL: https://github.com/alphanodes/valid_hostname
- Owner: alphanodes
- License: mit
- Created: 2022-10-26T15:10:23.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-11T15:40:51.000Z (over 3 years ago)
- Last Synced: 2024-11-14T10:50:31.339Z (over 1 year ago)
- Topics: gem, hostname, ruby, validation, validator
- Language: Ruby
- Homepage:
- Size: 263 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Valid Hostname
* Source:
* Bugs:
[](https://github.com/AlphaNodes/valid_hostname/actions/workflows/rubocop.yml) [](https://github.com/AlphaNodes/valid_hostname/actions/workflows/tests.yml) [](http://badge.fury.io/rb/valid_hostname)
## 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
```Gemfile
gem 'valid_hostname', '~> 2.0'
```
### Run bundler
```shell
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
## How to use
Simple usage
```ruby
class Record < ActiveRecord::Base
validates :name, hostname: true
end
```
With options
```ruby
class Record < ActiveRecord::Base
validates :name, hostname: { OPTIONS }
end
```
or static usage
```ruby
ValidateHostname.valid?('localhost')
```
or static usage with options
```ruby
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
```ruby
class Record < ActiveRecord::Base
validates :host, hostname: true
end
```
```result
>> @record = Record.new :name => 'horse'
>> @record.save
=> true
```
```result
>> @record2 = Record.new :name => '_horse'
>> @record2.save
=> false
```
With :allow_underscore
```ruby
class Record < ActiveRecord::Base
validates :name, hostname: { allow_underscore: true }
end
```
```result
>> @record3 = Record.new :name => '_horse'
>> @record3.save
=> true
```
With :require_valid_tld
```ruby
class Record < ActiveRecord::Base
validates :name, hostname: { require_valid_tld: true }
end
```
```result
>> @record4 = Record.new :name => 'horse'
>> @record4.save
=> false
```
```result
>> @record5 = Record.new :name => 'horse.com'
>> @record5.save
=> true
```
With :valid_tlds
```ruby
class Record < ActiveRecord::Base
validates :name, hostname: { :require_valid_tld, valid_tlds: %w[com org net] }
end
```
```result
>> @record6 = Record.new :name => 'horse.info'
>> @record6.save
=> false
```
With :allow_numeric_hostname
```ruby
class Record < ActiveRecord::Base
validates :name, hostname: { allow_numeric_hostname: false }
end
```
```result
>> @record7 = Record.new :name => '123.info'
>> @record7.save
=> false
```
With :allow_wildcard_hostname
```ruby
class Record < ActiveRecord::Base
validates :name, hostname: { allow_wildcard_hostname: true }
end
```
```result
>> @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 .
* [KimNorgaard](https://github.com/KimNorgaard)
* [alexandermeindl](https://github.com/alexandermeindl/awesome-redmine)
## Copyright
Copyright (c) 2009-2022 Kim Norgaard. See LICENSE for details
Copyright (c) 2022 Alexander Meindl. See LICENSE for details