validator 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,4 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
- .idea
5
+ .idea
6
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/README.markdown CHANGED
@@ -1,7 +1,7 @@
1
1
  Active Model Validator [![Travis](https://secure.travis-ci.org/nahaylo/validator.png)](http://travis-ci.org/nahaylo/validator)
2
2
  ============================
3
3
 
4
- This is a ActiveModel validators for domains, ip addresses and email addresses.
4
+ This is a ActiveModel validators for domains (including TLDs), ip addresses and email addresses.
5
5
 
6
6
  Installation
7
7
  ------------
@@ -22,6 +22,7 @@ In your models, the gem provides new validators like :domain, :ip_address or :em
22
22
  Domain Validator
23
23
  ----------------
24
24
 
25
+ # validate domain name by full length, label length, TLD existing
25
26
  validates :domain_name, :domain => true
26
27
 
27
28
  validates :domain_name, :domain => { :message => 'custom message' }
@@ -29,6 +30,9 @@ Domain Validator
29
30
  # custom full domain and label length
30
31
  validates :domain_name, :domain => { :length => 200, :label_length => 60 }
31
32
 
33
+ # skip TLD validation
34
+ validates :domain_name, :domain => { :check_tld => false }
35
+
32
36
 
33
37
  Ip Address Validator
34
38
  --------------------
@@ -2,7 +2,7 @@ module ActiveModel
2
2
  module Validations
3
3
  class DomainValidator < ActiveModel::EachValidator
4
4
  def initialize(options)
5
- options[:length] ||= ::Validator::Domain::LENGTH
5
+ options[:length] ||= ::Validator::Domain::LENGTH
6
6
  options[:label_length] ||= ::Validator::Domain::LABEL_LENGTH
7
7
 
8
8
  super(options)
@@ -29,6 +29,15 @@ module ActiveModel
29
29
 
30
30
  unless @validator.valid_by_regexp?
31
31
  record.errors.add(attr_name, :'domain.invalid', options)
32
+ return
33
+ end
34
+
35
+ # if check_tld = true - check if TLD exists
36
+ if options[:check_tld] != false
37
+ tld = value.split('.').last.upcase
38
+ unless ::Validator::Domain::Tld.exists?(tld)
39
+ record.errors.add(attr_name, :'domain.unknown_tld', options)
40
+ end
32
41
  end
33
42
  end
34
43
  end
@@ -1,5 +1,7 @@
1
1
  module Validator
2
2
  class Domain
3
+ autoload :Tld, 'validator/domain/tld'
4
+
3
5
  LENGTH = 255
4
6
  LABEL_LENGTH = 63
5
7
 
@@ -0,0 +1,20 @@
1
+ module Validator
2
+ class Domain
3
+ # The purpose of this class is to provide user with current list of
4
+ # available top level domain names including new ICANN additions and ccTLDs
5
+ # Currently TLD definitions have been acquired from the following sources:
6
+ #
7
+ # http://www.icann.org/tlds/
8
+ # http://www.dnso.org/constituency/gtld/gtld.html
9
+ # http://www.iana.org/cctld/cctld-whois.htm
10
+ # http://data.iana.org/TLD/tlds-alpha-by-domain.txt
11
+ #
12
+ class Tld
13
+ TLD = %w(AC AD AE AERO AF AG AI AL AM AN AO AQ AR ARPA AS ASIA AT AU AW AX AZ BA BB BD BE BF BG BH BI BIZ BJ BM BN BO BR BS BT BV BW BY BZ CA CAT CC CD CF CG CH CI CK CL CM CN CO COM COOP CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EDU EE EG ER ES ET EU FI FJ FK FM FO FR GA GB GD GE GF GG GH GI GL GM GN GOV GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN INFO INT IO IQ IR IS IT JE JM JO JOBS JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MG MH MIL MK ML MM MN MO MOBI MP MQ MR MS MT MU MUSEUM MV MW MX MY MZ NA NAME NC NE NET NF NG NI NL NO NP NR NU NZ OM ORG PA PE PF PG PH PK PL PM PN PR PRO PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR ST SU SV SX SY SZ TC TD TEL TF TG TH TJ TK TL TM TN TO TP TR TRAVEL TT TV TW TZ UA UG UK US UY UZ VA VC VE VG VI VN VU WF WS XN--0ZWM56D XN--11B5BS3A9AJ6G XN--3E0B707E XN--45BRJ9C XN--80AKHBYKNJ4F XN--90A3AC XN--9T4B11YI5A XN--CLCHC0EA0B2G2A9GCD XN--DEBA0AD XN--FIQS8S XN--FIQZ9S XN--FPCRJ9C3D XN--FZC2C9E2C XN--G6W251D XN--GECRJ9C XN--H2BRJ9C XN--HGBK6AJ7F53BBA XN--HLCJ6AYA9ESC7A XN--J6W193G XN--JXALPDLP XN--KGBECHTV XN--KPRW13D XN--KPRY57D XN--LGBBAT1AD8J XN--MGBAAM7A8H XN--MGBAYH7GPA XN--MGBBH1A71E XN--MGBC0A9AZCG XN--MGBERP4A5D4AR XN--O3CW4H XN--OGBPF8FL XN--P1AI XN--PGBS0DH XN--S9BRJ9C XN--WGBH1C XN--WGBL6A XN--XKC2AL3HYE2A XN--XKC2DL3A5EE0H XN--YFRO4I67O XN--YGBI2AMMX XN--ZCKZAH XXX YE YT ZA ZM ZW)
14
+
15
+ def self.exists?(tld)
16
+ TLD.include?(tld)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -3,6 +3,7 @@ en:
3
3
  messages:
4
4
  domain:
5
5
  invalid: "invalid domain name"
6
+ unknown_tld: "consists of unknown TLD"
6
7
  length: "domain name length is limited to %{length} characters"
7
8
  label_length: "each label length is limited to %{label_length} characters"
8
9
  ip_address:
@@ -1,3 +1,3 @@
1
1
  module Validator
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -7,6 +7,7 @@ module ActiveModel
7
7
  let(:domain) { TestDomain.new }
8
8
  let(:domain_with_message) { TestDomainWithMessage.new }
9
9
  let(:domain_with_length) { TestDomainWithLength.new }
10
+ let(:domain_without_tld) { TestDomainWithoutTld.new }
10
11
 
11
12
  describe "validations" do
12
13
  # for blank domain
@@ -47,7 +48,7 @@ module ActiveModel
47
48
  end
48
49
 
49
50
  it "should be valid if TLD length is between 2 and 6" do
50
- %w(ab abcabc).each do |tld|
51
+ %w(ua museum).each do |tld|
51
52
  domain.domain_name = "domain.#{tld}"
52
53
  domain.should_not have_errors_on(:domain_name)
53
54
  end
@@ -58,6 +59,11 @@ module ActiveModel
58
59
  domain_with_length.should be_valid
59
60
  end
60
61
 
62
+ it "should be valid with unknown TLD" do
63
+ domain_without_tld.domain_name = "valid-domain.abcabc"
64
+ domain_without_tld.should be_valid
65
+ end
66
+
61
67
  end
62
68
 
63
69
  describe 'invalid' do
@@ -92,8 +98,13 @@ module ActiveModel
92
98
  end
93
99
 
94
100
  it "should not be valid with TLD length more than 6" do
95
- domain.domain_name = "domain.abcabcd"
96
- domain.should have_errors_on(:domain_name)
101
+ domain_without_tld.domain_name = "domain.abcabcd"
102
+ domain_without_tld.should have_errors_on(:domain_name)
103
+ end
104
+
105
+ it "should not be valid with unknown TLD" do
106
+ domain.domain_name = "domain.abcabc"
107
+ domain.should have_errors_on(:domain_name).with_message(I18n.t(:'errors.messages.domain.unknown_tld'))
97
108
  end
98
109
 
99
110
  it "should not be valid if TLD consists of numbers or special symbols (&, _, {, ], *, etc)" do
@@ -15,3 +15,7 @@ end
15
15
  class TestDomainWithMessage < BaseTestDomain
16
16
  validates :domain_name, :domain => { :message => 'invalid' }
17
17
  end
18
+
19
+ class TestDomainWithoutTld < BaseTestDomain
20
+ validates :domain_name, :domain => { :check_tld => false }
21
+ end
data/validator.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Vitaliy Nahaylo"]
9
9
  s.email = ["nahaylo@gmail.com"]
10
10
  s.homepage = "https://github.com/nahaylo/validator"
11
- s.summary = "ActiveModel validations for domains, ip addresses and email addresses with fully localization support."
12
- s.description = "ActiveModel validations for domains, ip addresses and email addresses with fully localization support."
11
+ s.summary = "ActiveModel validations for domains (including TLDs), ip addresses and email addresses with fully localization support."
12
+ s.description = "ActiveModel validations for domains (including TLDs), ip addresses and email addresses with fully localization support."
13
13
 
14
14
  s.rubyforge_project = "validator"
15
15
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vitaliy Nahaylo
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-20 00:00:00 +02:00
18
+ date: 2012-02-28 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -78,7 +78,7 @@ dependencies:
78
78
  version: "0"
79
79
  type: :development
80
80
  version_requirements: *id004
81
- description: ActiveModel validations for domains, ip addresses and email addresses with fully localization support.
81
+ description: ActiveModel validations for domains (including TLDs), ip addresses and email addresses with fully localization support.
82
82
  email:
83
83
  - nahaylo@gmail.com
84
84
  executables: []
@@ -89,6 +89,7 @@ extra_rdoc_files: []
89
89
 
90
90
  files:
91
91
  - .gitignore
92
+ - .rspec
92
93
  - .travis.yml
93
94
  - Gemfile
94
95
  - LICENSE
@@ -99,6 +100,7 @@ files:
99
100
  - lib/active_model/validations/ip_address_validator.rb
100
101
  - lib/validator.rb
101
102
  - lib/validator/domain.rb
103
+ - lib/validator/domain/tld.rb
102
104
  - lib/validator/email.rb
103
105
  - lib/validator/ip_address.rb
104
106
  - lib/validator/locale/en.yml
@@ -144,7 +146,7 @@ rubyforge_project: validator
144
146
  rubygems_version: 1.5.0
145
147
  signing_key:
146
148
  specification_version: 3
147
- summary: ActiveModel validations for domains, ip addresses and email addresses with fully localization support.
149
+ summary: ActiveModel validations for domains (including TLDs), ip addresses and email addresses with fully localization support.
148
150
  test_files:
149
151
  - spec/domain_validator_spec.rb
150
152
  - spec/email_validator_spec.rb