validator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -3,10 +3,14 @@ Active Model Validator
3
3
 
4
4
  This is a ActiveModel validators for domains and ip addresses.
5
5
 
6
- Example
6
+ Installation
7
+ ------------
8
+ gem install validator
9
+
10
+ Usage
7
11
  -------
8
12
 
9
- The following model uses `ActiveModel::Validations::PresenceValidator` and `ActiveRecord::Validations::UniquenessValidator` to ensure the presence and uniqueness of the user’s email attribute. The third line uses `EmailValidator` to check that the email address is valid.
13
+ In your models, the gem provides new validators like :domain or :ip_address
10
14
 
11
15
  class Model < ActiveRecord::Base
12
16
  validates :domain_name, :domain => true
@@ -21,6 +25,9 @@ Domain Validator
21
25
 
22
26
  validates :domain_name, :domain => {:message => 'custom message'}
23
27
 
28
+ # custom full domain length and label length
29
+ validates :domain_name, :domain => { :length => 200, :label_length => 60 }
30
+
24
31
 
25
32
  Ip Address Validator
26
33
  --------------------
@@ -39,7 +46,31 @@ Ip Address Validator
39
46
 
40
47
  validates :ip, :ip_address => { :message => "custom message" }
41
48
 
49
+
50
+ Localization Tricks
51
+ -------------------
52
+ To customize error message, you can use { :message => "your custom message" } or simple use Rails localization en.yml file, for instance:
53
+
54
+ en:
55
+ errors:
56
+ messages:
57
+ domain:
58
+ length: "your custom length error message"
59
+ ip_address:
60
+ invalid:
61
+ general: "your custom invalid ip address error message"
62
+ activemodel:
63
+ errors:
64
+ messages:
65
+ domain:
66
+ invalid: "custom error message only for activemodel"
67
+ models:
68
+ your_model:
69
+ domain:
70
+ invalid: "custom error message for YourDomain model"
71
+
72
+
42
73
  Copyright
43
74
  ---------
44
75
 
45
- Copyright (c) 2011 Vitaliy Nahaylo. See LICENSE for details.
76
+ Copyright (c) 2011 Vitaliy Nahaylo. See LICENSE for details.
@@ -1,30 +1,27 @@
1
1
  module ActiveModel
2
2
  module Validations
3
3
  class DomainValidator < ActiveModel::EachValidator
4
- # Call `#initialize` on the superclass, adding a default
5
- # `:allow_nil => false` option.
6
4
  def initialize(options)
7
- super(options.reverse_merge(:allow_nil => false))
5
+ options[:length] ||= ::Validator::Domain::LENGTH
6
+ options[:label_length] ||= ::Validator::Domain::LABEL_LENGTH
7
+
8
+ super(options)
8
9
  end
9
10
 
10
11
  def validate_each(record, attr_name, value)
11
- return if options[:allow_nil] && value.nil?
12
-
13
12
  # do not validate if value is empty
14
13
  return if value.nil?
15
14
 
16
15
  @validator = ::Validator::Domain.new(value)
17
16
 
18
17
  # max domain length
19
- unless @validator.valid_by_length?
18
+ unless @validator.valid_by_length?(options[:length])
20
19
  record.errors.add(attr_name, :'domain.length', options)
21
- #return
22
20
  end
23
21
 
24
22
  # label is limited to between 1 and 63 octets
25
- unless @validator.valid_by_label_length?
23
+ unless @validator.valid_by_label_length?(options[:label_length])
26
24
  record.errors.add(attr_name, :'domain.label_length', options)
27
- #return
28
25
  end
29
26
 
30
27
  # skip proceeding validation if errors
@@ -1,15 +1,7 @@
1
1
  module ActiveModel
2
2
  module Validations
3
3
  class IpAddressValidator < ActiveModel::EachValidator
4
- # Call `#initialize` on the superclass, adding a default
5
- # `:allow_nil => false` option.
6
- def initialize(options)
7
- super(options.reverse_merge(:allow_nil => false))
8
- end
9
-
10
4
  def validate_each(record, attr_name, value)
11
- return if options[:allow_nil] && value.nil?
12
-
13
5
  # do not validate if value is empty
14
6
  return if value.nil?
15
7
 
@@ -44,7 +36,7 @@ module ActiveModel
44
36
  end
45
37
 
46
38
  module HelperMethods
47
- # class Dns < ActiveRecord::Base
39
+ # class Ip < ActiveRecord::Base
48
40
  # validates_ip_address_of :ip
49
41
  # end
50
42
  #
@@ -7,12 +7,12 @@ module Validator
7
7
  @value = value
8
8
  end
9
9
 
10
- def valid_by_length?(length = LENGTH)
11
- @value.length <= length
10
+ def valid_by_length?(length = nil)
11
+ @value.length <= (length || LENGTH)
12
12
  end
13
13
 
14
- def valid_by_label_length?(label_length = LABEL_LENGTH)
15
- !(@value.split(".").find{|f| f.length > label_length and f.length > 1 })
14
+ def valid_by_label_length?(label_length = nil)
15
+ !(@value.split(".").find{|f| f.length > (label_length || LABEL_LENGTH) and f.length > 1 })
16
16
  end
17
17
 
18
18
  def valid_by_regexp?
@@ -3,8 +3,8 @@ en:
3
3
  messages:
4
4
  domain:
5
5
  invalid: "invalid domain name"
6
- length: "full domain name is limited to 255 octets"
7
- label_length: "length of any one label is limited to between 1 and 63 octets"
6
+ length: "domain name length is limited to %{length} characters"
7
+ label_length: "each label length is limited to %{label_length} characters"
8
8
  ip_address:
9
9
  invalid:
10
10
  general: "invalid ip address"
@@ -1,3 +1,3 @@
1
1
  module Validator
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -6,6 +6,7 @@ module ActiveModel
6
6
  describe DomainValidator do
7
7
  let(:domain) { TestDomain.new }
8
8
  let(:domain_with_message) { TestDomainWithMessage.new }
9
+ let(:domain_with_length) { TestDomainWithLength.new }
9
10
 
10
11
  describe "validations" do
11
12
  # for blank domain
@@ -52,22 +53,27 @@ module ActiveModel
52
53
  end
53
54
  end
54
55
 
56
+ it "should be valid for custom length and label length" do
57
+ domain_with_length.domain_name = "valid-domain.com"
58
+ domain_with_length.should be_valid
59
+ end
60
+
55
61
  end
56
62
 
57
63
  describe 'invalid' do
58
64
  it {
59
65
  domain.domain_name = "not_valid_because_of_length_#{'w'*230}.com"
60
- domain.should have_errors_on(:domain_name, 2).with_message(I18n.t(:'errors.messages.domain.length'))
66
+ domain.should have_errors_on(:domain_name, 2).with_message(I18n.t(:'errors.messages.domain.length', :length => 255))
61
67
  }
62
68
 
63
69
  it {
64
70
  domain.domain_name = "not_valid_because_of_length_of_label#{'w'*230}.com"
65
- domain.should have_errors_on(:domain_name, 2).with_message(I18n.t(:'errors.messages.domain.label_length'))
71
+ domain.should have_errors_on(:domain_name, 2).with_message(I18n.t(:'errors.messages.domain.label_length', :label_length => 63))
66
72
  }
67
73
 
68
74
  it {
69
75
  domain.domain_name = "#{'w'*64}.com"
70
- domain.should have_errors_on(:domain_name).with_message(I18n.t(:'errors.messages.domain.label_length'))
76
+ domain.should have_errors_on(:domain_name).with_message(I18n.t(:'errors.messages.domain.label_length', :label_length => 63))
71
77
  }
72
78
 
73
79
  it "should be invalid if consists of special symbols (&, _, {, ], *, etc)" do
@@ -97,6 +103,18 @@ module ActiveModel
97
103
  end
98
104
  end
99
105
 
106
+ context 'custom full domain length and label length' do
107
+ it {
108
+ domain_with_length.domain_name = "#{'w'*61}.com"
109
+ domain_with_length.should have_errors_on(:domain_name).with_message(I18n.t(:'errors.messages.domain.label_length', :label_length => 60))
110
+ }
111
+
112
+ it {
113
+ domain_with_length.domain_name = "not_valid_because_of_length_#{'w'*190}.com"
114
+ domain_with_length.should have_errors_on(:domain_name, 2).with_message(I18n.t(:'errors.messages.domain.length', :length => 200))
115
+ }
116
+ end
117
+
100
118
  end
101
119
  end
102
120
  end
@@ -8,6 +8,10 @@ class TestDomain < BaseTestDomain
8
8
  validates :domain_name, :domain => true
9
9
  end
10
10
 
11
+ class TestDomainWithLength < BaseTestDomain
12
+ validates :domain_name, :domain => { :length => 200, :label_length => 60 }
13
+ end
14
+
11
15
  class TestDomainWithMessage < BaseTestDomain
12
16
  validates :domain_name, :domain => { :message => 'invalid' }
13
- end
17
+ end
metadata CHANGED
@@ -1,73 +1,109 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: validator
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Vitaliy Nahaylo
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-01-17 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2012-01-24 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
15
22
  name: ipaddress
16
- requirement: &4227880 !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
17
25
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
22
33
  type: :runtime
23
- prerelease: false
24
- version_requirements: *4227880
25
- - !ruby/object:Gem::Dependency
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
26
36
  name: activemodel
27
- requirement: &3846480 !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
28
39
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 3
46
+ - 0
47
+ - 0
32
48
  version: 3.0.0
33
49
  - - <
34
- - !ruby/object:Gem::Version
50
+ - !ruby/object:Gem::Version
51
+ hash: 15
52
+ segments:
53
+ - 3
54
+ - 2
55
+ - 0
35
56
  version: 3.2.0
36
57
  type: :runtime
37
- prerelease: false
38
- version_requirements: *3846480
39
- - !ruby/object:Gem::Dependency
58
+ version_requirements: *id002
59
+ - !ruby/object:Gem::Dependency
40
60
  name: activesupport
41
- requirement: &3841640 !ruby/object:Gem::Requirement
61
+ prerelease: false
62
+ requirement: &id003 !ruby/object:Gem::Requirement
42
63
  none: false
43
- requirements:
44
- - - ! '>='
45
- - !ruby/object:Gem::Version
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 7
68
+ segments:
69
+ - 3
70
+ - 0
71
+ - 0
46
72
  version: 3.0.0
47
73
  - - <
48
- - !ruby/object:Gem::Version
74
+ - !ruby/object:Gem::Version
75
+ hash: 15
76
+ segments:
77
+ - 3
78
+ - 2
79
+ - 0
49
80
  version: 3.2.0
50
81
  type: :development
51
- prerelease: false
52
- version_requirements: *3841640
53
- - !ruby/object:Gem::Dependency
82
+ version_requirements: *id003
83
+ - !ruby/object:Gem::Dependency
54
84
  name: rspec
55
- requirement: &3889840 !ruby/object:Gem::Requirement
85
+ prerelease: false
86
+ requirement: &id004 !ruby/object:Gem::Requirement
56
87
  none: false
57
- requirements:
58
- - - ! '>='
59
- - !ruby/object:Gem::Version
60
- version: '0'
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
61
95
  type: :development
62
- prerelease: false
63
- version_requirements: *3889840
96
+ version_requirements: *id004
64
97
  description: Validators for domains and ip addresses
65
- email:
98
+ email:
66
99
  - nahaylo@gmail.com
67
100
  executables: []
101
+
68
102
  extensions: []
103
+
69
104
  extra_rdoc_files: []
70
- files:
105
+
106
+ files:
71
107
  - .gitignore
72
108
  - Gemfile
73
109
  - LICENSE
@@ -86,31 +122,41 @@ files:
86
122
  - spec/test_classes/domain.rb
87
123
  - spec/test_classes/ip_address.rb
88
124
  - validator.gemspec
89
- homepage: ''
125
+ has_rdoc: true
126
+ homepage: ""
90
127
  licenses: []
128
+
91
129
  post_install_message:
92
130
  rdoc_options: []
93
- require_paths:
131
+
132
+ require_paths:
94
133
  - lib
95
- required_ruby_version: !ruby/object:Gem::Requirement
134
+ required_ruby_version: !ruby/object:Gem::Requirement
96
135
  none: false
97
- requirements:
98
- - - ! '>='
99
- - !ruby/object:Gem::Version
100
- version: '0'
101
- required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 3
140
+ segments:
141
+ - 0
142
+ version: "0"
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
144
  none: false
103
- requirements:
104
- - - ! '>='
105
- - !ruby/object:Gem::Version
106
- version: '0'
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 3
149
+ segments:
150
+ - 0
151
+ version: "0"
107
152
  requirements: []
153
+
108
154
  rubyforge_project: validator
109
- rubygems_version: 1.8.10
155
+ rubygems_version: 1.5.0
110
156
  signing_key:
111
157
  specification_version: 3
112
158
  summary: Validator
113
- test_files:
159
+ test_files:
114
160
  - spec/domain_validator_spec.rb
115
161
  - spec/ip_address_validator_spec.rb
116
162
  - spec/spec_helper.rb