activevalidators 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +2 -2
- data/README.md +2 -2
- data/activevalidators.gemspec +1 -1
- data/lib/active_model/validations/email_validator.rb +1 -1
- data/lib/active_model/validations/phone_validator.rb +11 -0
- data/lib/active_model/validations/respond_to_validator.rb +1 -1
- data/lib/active_model/validations/url_validator.rb +1 -1
- data/lib/active_validators.rb +1 -0
- data/spec/models/phone_validator_model.rb +7 -0
- data/spec/specs/email_spec.rb +17 -6
- data/spec/specs/phone_spec.rb +57 -0
- data/spec/specs/respond_to_spec.rb +18 -8
- data/spec/specs/url_spec.rb +16 -5
- metadata +7 -2
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
|
4
|
+
activevalidators (1.0.2)
|
5
5
|
activemodel (>= 3.0.0)
|
6
6
|
activerecord (>= 3.0.0)
|
7
7
|
mail
|
@@ -46,9 +46,9 @@ PLATFORMS
|
|
46
46
|
ruby
|
47
47
|
|
48
48
|
DEPENDENCIES
|
49
|
-
ActiveValidators!
|
50
49
|
activemodel (>= 3.0.0)
|
51
50
|
activerecord (>= 3.0.0)
|
51
|
+
activevalidators!
|
52
52
|
bundler
|
53
53
|
mail
|
54
54
|
rspec
|
data/README.md
CHANGED
@@ -22,14 +22,14 @@ Exhaustive list of supported validators:
|
|
22
22
|
|
23
23
|
* `email` : checks the email based on the `mail` gem
|
24
24
|
* `url` : checks the url based on a regular expression
|
25
|
-
|
25
|
+
* `phone` : checks the phone number based on a regular expression
|
26
26
|
|
27
27
|
Todo
|
28
28
|
----
|
29
29
|
|
30
30
|
Lots of improvements can be made:
|
31
31
|
|
32
|
-
* Add I18n of error messages
|
32
|
+
* Add I18n specific types of error messages for each validator
|
33
33
|
* Implement new validators
|
34
34
|
* ...
|
35
35
|
|
data/activevalidators.gemspec
CHANGED
@@ -0,0 +1,11 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class PhoneValidator < EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
unless value =~ /^\d{3}-\d{3}-\d{4}|\d{3}\.\d{3}\.\d{4}|\d{10}|\d{3}\s\d{3}\s\d{4}|\(\d{3}\)\s\d{3}-\d{4}$/i
|
6
|
+
record.errors.add(attribute)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -6,7 +6,7 @@ module ActiveModel
|
|
6
6
|
responders = options.dup
|
7
7
|
RESERVED_OPTIONS.each do |opt,should_apply| responders.delete(opt) end
|
8
8
|
responders.each do |method,dummy|
|
9
|
-
record.errors
|
9
|
+
record.errors.add(attribute) unless value.respond_to? method
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
@@ -3,7 +3,7 @@ module ActiveModel
|
|
3
3
|
class UrlValidator < EachValidator
|
4
4
|
def validate_each(record, attribute, value)
|
5
5
|
unless value =~ /^https?:\/\/(?i)[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/
|
6
|
-
record.errors
|
6
|
+
record.errors.add(attribute)
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
data/lib/active_validators.rb
CHANGED
data/spec/specs/email_spec.rb
CHANGED
@@ -4,14 +4,25 @@ describe "Email Validation" do
|
|
4
4
|
it "accepts valid emails" do
|
5
5
|
model = Models::EmailValidatorModel.new
|
6
6
|
model.email = 'franck@verrot.fr'
|
7
|
-
model.valid?.should
|
7
|
+
model.valid?.should be(true)
|
8
8
|
model.should have(0).errors
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
model
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
describe "for invalid emails" do
|
12
|
+
let(:model) do
|
13
|
+
Models::EmailValidatorModel.new.tap do |m|
|
14
|
+
m.email = 'franck.fr'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "rejects invalid emails" do
|
19
|
+
model.valid?.should be(false)
|
20
|
+
model.should have(1).errors
|
21
|
+
end
|
22
|
+
|
23
|
+
it "generates an error message of type invalid" do
|
24
|
+
model.valid?.should be(false)
|
25
|
+
model.errors[:email].should == [model.errors.generate_message(:email, :invalid)]
|
26
|
+
end
|
16
27
|
end
|
17
28
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
2
|
+
|
3
|
+
describe "Phone Validation" do
|
4
|
+
|
5
|
+
it 'should validate format of phone with ###-###-####' do
|
6
|
+
model = Models::PhoneValidatorModel.new
|
7
|
+
model.phone = '999-999-9999'
|
8
|
+
model.valid?.should be(true)
|
9
|
+
model.should have(0).errors
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should validate format of phone with ##########' do
|
13
|
+
model = Models::PhoneValidatorModel.new
|
14
|
+
model.phone = '9999999999'
|
15
|
+
model.valid?.should be(true)
|
16
|
+
model.should have(0).errors
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should validate format of phone with ###.###.####' do
|
20
|
+
model = Models::PhoneValidatorModel.new
|
21
|
+
model.phone = '999.999.9999'
|
22
|
+
model.valid?.should be(true)
|
23
|
+
model.should have(0).errors
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should validate format of phone with ### ### ####' do
|
27
|
+
model = Models::PhoneValidatorModel.new
|
28
|
+
model.phone = '999 999 9999'
|
29
|
+
model.valid?.should be(true)
|
30
|
+
model.should have(0).errors
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should validate format of phone with (###) ###-####' do
|
34
|
+
model = Models::PhoneValidatorModel.new
|
35
|
+
model.phone = '(999) 999-9999'
|
36
|
+
model.valid?.should be(true)
|
37
|
+
model.should have(0).errors
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "for invalid formats" do
|
41
|
+
let(:model) do
|
42
|
+
Models::PhoneValidatorModel.new.tap do |m|
|
43
|
+
m.phone = '999'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "rejects invalid formats" do
|
48
|
+
model.valid?.should be(false)
|
49
|
+
model.should have(1).errors
|
50
|
+
end
|
51
|
+
|
52
|
+
it "generates an error message of type invalid" do
|
53
|
+
model.valid?.should be(false)
|
54
|
+
model.errors[:phone].should == [model.errors.generate_message(:phone, :invalid)]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -7,17 +7,27 @@ describe "Respond To Validation" do
|
|
7
7
|
model.global_condition = true
|
8
8
|
model.local_condition = true
|
9
9
|
|
10
|
-
model.valid?.should
|
10
|
+
model.valid?.should be(true)
|
11
11
|
model.should have(0).errors
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
model
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
describe "when does not respond_to?" do
|
15
|
+
let(:model) do
|
16
|
+
Models::RespondToValidatorModel.new.tap do |m|
|
17
|
+
m.responder = 42
|
18
|
+
m.global_condition = true
|
19
|
+
m.local_condition = true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "rejects the responder" do
|
24
|
+
model.valid?.should be(false)
|
25
|
+
model.should have(1).errors
|
26
|
+
end
|
19
27
|
|
20
|
-
|
21
|
-
|
28
|
+
it "generates an error message of type invalid" do
|
29
|
+
model.valid?.should be(false)
|
30
|
+
model.errors[:responder].should == [model.errors.generate_message(:responder, :invalid)]
|
31
|
+
end
|
22
32
|
end
|
23
33
|
end
|
data/spec/specs/url_spec.rb
CHANGED
@@ -8,10 +8,21 @@ describe "Url Validation" do
|
|
8
8
|
model.should have(0).errors
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
model
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
describe "for invalid emails" do
|
12
|
+
let(:model) do
|
13
|
+
Models::UrlValidatorModel.new.tap do |m|
|
14
|
+
m.url = 'http://^^^^.fr'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "rejects invalid emails" do
|
19
|
+
model.valid?.should be(false)
|
20
|
+
model.should have(1).errors
|
21
|
+
end
|
22
|
+
|
23
|
+
it "generates an error message of type invalid" do
|
24
|
+
model.valid?.should be(false)
|
25
|
+
model.errors[:url].should == [model.errors.generate_message(:url, :invalid)]
|
26
|
+
end
|
16
27
|
end
|
17
28
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 1.0.
|
8
|
+
- 2
|
9
|
+
version: 1.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Franck Verrot
|
@@ -132,14 +132,17 @@ files:
|
|
132
132
|
- activevalidators.gemspec
|
133
133
|
- autotest/discover.rb
|
134
134
|
- lib/active_model/validations/email_validator.rb
|
135
|
+
- lib/active_model/validations/phone_validator.rb
|
135
136
|
- lib/active_model/validations/respond_to_validator.rb
|
136
137
|
- lib/active_model/validations/url_validator.rb
|
137
138
|
- lib/active_validators.rb
|
138
139
|
- spec/models/email_validator_model.rb
|
140
|
+
- spec/models/phone_validator_model.rb
|
139
141
|
- spec/models/respond_to_validator_model.rb
|
140
142
|
- spec/models/url_validator_model.rb
|
141
143
|
- spec/spec_helper.rb
|
142
144
|
- spec/specs/email_spec.rb
|
145
|
+
- spec/specs/phone_spec.rb
|
143
146
|
- spec/specs/respond_to_spec.rb
|
144
147
|
- spec/specs/url_spec.rb
|
145
148
|
has_rdoc: true
|
@@ -176,9 +179,11 @@ specification_version: 3
|
|
176
179
|
summary: Collection of ActiveModel/ActiveRecord validations
|
177
180
|
test_files:
|
178
181
|
- spec/models/email_validator_model.rb
|
182
|
+
- spec/models/phone_validator_model.rb
|
179
183
|
- spec/models/respond_to_validator_model.rb
|
180
184
|
- spec/models/url_validator_model.rb
|
181
185
|
- spec/spec_helper.rb
|
182
186
|
- spec/specs/email_spec.rb
|
187
|
+
- spec/specs/phone_spec.rb
|
183
188
|
- spec/specs/respond_to_spec.rb
|
184
189
|
- spec/specs/url_spec.rb
|