activevalidators 1.5.1 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +10 -0
- data/README.md +10 -7
- data/Rakefile +24 -27
- data/activevalidators.gemspec +9 -9
- data/lib/active_model/validations/tracking_number_validator.rb +53 -16
- data/lib/active_validators/version.rb +4 -0
- data/test/test_helper.rb +29 -0
- data/{spec/validations/credit_card_spec.rb → test/validations/credit_card_test.rb} +14 -22
- data/test/validations/email_test.rb +42 -0
- data/test/validations/ip_test.rb +61 -0
- data/test/validations/password_test.rb +40 -0
- data/test/validations/phone_test.rb +76 -0
- data/test/validations/postal_code_test.rb +53 -0
- data/test/validations/respond_to_test.rb +33 -0
- data/test/validations/slug_test.rb +39 -0
- data/test/validations/tracking_number_test.rb +151 -0
- data/test/validations/twitter_test.rb +161 -0
- data/test/validations/url_test.rb +39 -0
- metadata +49 -71
- data/.bundle/config +0 -2
- data/autotest/discover.rb +0 -1
- data/spec/activevalidators_spec.rb +0 -39
- data/spec/spec_helper.rb +0 -15
- data/spec/validations/email_spec.rb +0 -44
- data/spec/validations/ip_spec.rb +0 -73
- data/spec/validations/password_spec.rb +0 -44
- data/spec/validations/phone_spec.rb +0 -80
- data/spec/validations/postal_code_spec.rb +0 -61
- data/spec/validations/respond_to_spec.rb +0 -37
- data/spec/validations/slug_spec.rb +0 -44
- data/spec/validations/tracking_number_spec.rb +0 -66
- data/spec/validations/twitter_spec.rb +0 -155
- data/spec/validations/url_spec.rb +0 -38
@@ -1,80 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
2
|
-
|
3
|
-
describe "Phone Validation" do
|
4
|
-
|
5
|
-
subject { TestRecord.new }
|
6
|
-
|
7
|
-
context "when no country is given" do
|
8
|
-
before(:each) do
|
9
|
-
TestRecord.reset_callbacks(:validate)
|
10
|
-
TestRecord.validates :phone, :phone => true
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should validate format of phone with ###-###-####' do
|
14
|
-
subject.phone = '999-999-9999'
|
15
|
-
subject.should be_valid
|
16
|
-
subject.should have(0).errors
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should validate format of phone with ##########' do
|
20
|
-
subject.phone = '9999999999'
|
21
|
-
subject.should be_valid
|
22
|
-
subject.should have(0).errors
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'should validate format of phone with ###.###.####' do
|
26
|
-
subject.phone = '999.999.9999'
|
27
|
-
subject.should be_valid
|
28
|
-
subject.should have(0).errors
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'should validate format of phone with ### ### ####' do
|
32
|
-
subject.phone = '999 999 9999'
|
33
|
-
subject.should be_valid
|
34
|
-
subject.should have(0).errors
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'should validate format of phone with (###) ###-####' do
|
38
|
-
subject.phone = '(999) 999-9999'
|
39
|
-
subject.should be_valid
|
40
|
-
subject.should have(0).errors
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
ActiveModel::Validations::PhoneValidator.known_formats.each do |country, formats|
|
46
|
-
context "when given a :#{country} country parameter" do
|
47
|
-
before(:each) do
|
48
|
-
TestRecord.reset_callbacks(:validate)
|
49
|
-
TestRecord.validates :phone, :phone => {:country => country}
|
50
|
-
end
|
51
|
-
|
52
|
-
formats.each do |format|
|
53
|
-
it "should validate format of phone with #{format}" do
|
54
|
-
subject.phone = format.gsub('#','9')
|
55
|
-
subject.should be_valid
|
56
|
-
subject.should have(0).errors
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
|
63
|
-
describe "for invalid formats" do
|
64
|
-
before :each do
|
65
|
-
TestRecord.reset_callbacks(:validate)
|
66
|
-
TestRecord.validates :phone, :phone => true
|
67
|
-
subject.phone = '999'
|
68
|
-
end
|
69
|
-
|
70
|
-
it "rejects invalid formats" do
|
71
|
-
subject.should_not be_valid
|
72
|
-
subject.should have(1).error
|
73
|
-
end
|
74
|
-
|
75
|
-
it "generates an error message of type invalid" do
|
76
|
-
subject.should_not be_valid
|
77
|
-
subject.errors[:phone].should include subject.errors.generate_message(:phone, :invalid)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
@@ -1,61 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
2
|
-
|
3
|
-
describe "Postal Code Validation" do
|
4
|
-
|
5
|
-
subject { TestRecord.new }
|
6
|
-
|
7
|
-
context "when no country is given" do
|
8
|
-
before(:each) do
|
9
|
-
TestRecord.reset_callbacks(:validate)
|
10
|
-
TestRecord.validates :postal_code, :postal_code => true
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'should validate format of postal code with #####' do
|
14
|
-
subject.postal_code = '11211'
|
15
|
-
subject.should be_valid
|
16
|
-
subject.should have(0).errors
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'should validate format of postal code with #####-#####' do
|
20
|
-
subject.postal_code = '94117-1234'
|
21
|
-
subject.should be_valid
|
22
|
-
subject.should have(0).errors
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
ActiveModel::Validations::PostalCodeValidator.known_formats.each do |country, formats|
|
27
|
-
context "when given a :#{country} country parameter" do
|
28
|
-
before(:each) do
|
29
|
-
TestRecord.reset_callbacks(:validate)
|
30
|
-
TestRecord.validates :postal_code, :postal_code => {:country => country}
|
31
|
-
end
|
32
|
-
|
33
|
-
formats.each do |format|
|
34
|
-
it "should validate format of postal code with #{format}" do
|
35
|
-
subject.postal_code = format.gsub('#','9')
|
36
|
-
subject.should be_valid
|
37
|
-
subject.should have(0).errors
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
describe "for invalid formats" do
|
45
|
-
before :each do
|
46
|
-
TestRecord.reset_callbacks(:validate)
|
47
|
-
TestRecord.validates :postal_code, :postal_code => true
|
48
|
-
subject.postal_code = '999'
|
49
|
-
end
|
50
|
-
|
51
|
-
it "rejects invalid formats" do
|
52
|
-
subject.should_not be_valid
|
53
|
-
subject.should have(1).error
|
54
|
-
end
|
55
|
-
|
56
|
-
it "generates an error message of type invalid" do
|
57
|
-
subject.should_not be_valid
|
58
|
-
subject.errors[:postal_code].should include subject.errors.generate_message(:postal_code, :invalid)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
2
|
-
|
3
|
-
describe "Respond To Validation" do
|
4
|
-
before(:each) do
|
5
|
-
TestRecord.reset_callbacks(:validate)
|
6
|
-
TestRecord.validates :responder, :respond_to => { :call => true, :if => :local_condition }, :if => :global_condition
|
7
|
-
end
|
8
|
-
|
9
|
-
subject { TestRecord.new }
|
10
|
-
|
11
|
-
it "respond_to?" do
|
12
|
-
subject.responder = lambda {}
|
13
|
-
subject.global_condition = true
|
14
|
-
subject.local_condition = true
|
15
|
-
|
16
|
-
subject.should be_valid
|
17
|
-
subject.should have(0).errors
|
18
|
-
end
|
19
|
-
|
20
|
-
describe "when does not respond_to?" do
|
21
|
-
before :each do
|
22
|
-
subject.responder = 42
|
23
|
-
subject.global_condition = true
|
24
|
-
subject.local_condition = true
|
25
|
-
end
|
26
|
-
|
27
|
-
it "rejects the responder" do
|
28
|
-
subject.should_not be_valid
|
29
|
-
subject.should have(1).error
|
30
|
-
end
|
31
|
-
|
32
|
-
it "generates an error message of type invalid" do
|
33
|
-
subject.should_not be_valid
|
34
|
-
subject.errors[:responder].should include subject.errors.generate_message(:responder, :invalid)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
2
|
-
|
3
|
-
describe "Slug Validation" do
|
4
|
-
before(:each) do
|
5
|
-
TestRecord.reset_callbacks(:validate)
|
6
|
-
TestRecord.validates :slug, :slug => true
|
7
|
-
end
|
8
|
-
|
9
|
-
subject { TestRecord.new }
|
10
|
-
|
11
|
-
it "accepts valid slugs" do
|
12
|
-
subject.slug = '1234567890-foo-bar-bar'
|
13
|
-
subject.should be_valid
|
14
|
-
subject.should have(0).errors
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "for invalid slugs" do
|
18
|
-
before :each do
|
19
|
-
subject.slug = '@#$%^'
|
20
|
-
end
|
21
|
-
|
22
|
-
it "rejects invalid slugs" do
|
23
|
-
subject.should_not be_valid
|
24
|
-
subject.should have(1).error
|
25
|
-
end
|
26
|
-
|
27
|
-
it "generates an error message of type invalid" do
|
28
|
-
subject.should_not be_valid
|
29
|
-
subject.errors[:slug].should include subject.errors.generate_message(:slug, :invalid)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "for empty slugs" do
|
34
|
-
before :each do
|
35
|
-
subject.slug = nil
|
36
|
-
end
|
37
|
-
|
38
|
-
it "generates an error message of type blank" do
|
39
|
-
subject.should_not be_valid
|
40
|
-
subject.errors[:slug].should include subject.errors.generate_message(:slug, :blank)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
@@ -1,66 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
2
|
-
|
3
|
-
describe "Tracking Number Validation" do
|
4
|
-
|
5
|
-
subject { TestRecord.new }
|
6
|
-
|
7
|
-
context "when no carrier parameter is given" do
|
8
|
-
before(:each) do
|
9
|
-
TestRecord.reset_callbacks(:validate)
|
10
|
-
TestRecord.validates :tracking_number, :tracking_number => true
|
11
|
-
end
|
12
|
-
|
13
|
-
it "raises an exception" do
|
14
|
-
lambda { subject.valid? }.should raise_error
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context "when given a ups carrier parameter" do
|
19
|
-
before(:each) do
|
20
|
-
TestRecord.reset_callbacks(:validate)
|
21
|
-
TestRecord.validates :tracking_number, :tracking_number => {:carrier => :ups}
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'should validate format of tracking number with 1Z................' do
|
25
|
-
subject.tracking_number = '1Z12345E0205271688'
|
26
|
-
subject.should be_valid
|
27
|
-
subject.should have(0).errors
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'should validate format of tracking number with ............' do
|
31
|
-
subject.tracking_number = '9999V999J999'
|
32
|
-
subject.should be_valid
|
33
|
-
subject.should have(0).errors
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'should validate format of tracking number with T..........' do
|
37
|
-
subject.tracking_number = 'T99F99E9999'
|
38
|
-
subject.should be_valid
|
39
|
-
subject.should have(0).errors
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should validate format of tracking number with .........' do
|
43
|
-
subject.tracking_number = '990728071'
|
44
|
-
subject.should be_valid
|
45
|
-
subject.should have(0).errors
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe "for invalid formats" do
|
50
|
-
before :each do
|
51
|
-
TestRecord.reset_callbacks(:validate)
|
52
|
-
TestRecord.validates :tracking_number, :tracking_number => {:carrier => :ups}
|
53
|
-
subject.tracking_number = '1Z12345E020_271688'
|
54
|
-
end
|
55
|
-
|
56
|
-
it "rejects invalid formats" do
|
57
|
-
subject.should_not be_valid
|
58
|
-
subject.should have(1).error
|
59
|
-
end
|
60
|
-
|
61
|
-
it "generates an error message of type invalid" do
|
62
|
-
subject.should_not be_valid
|
63
|
-
subject.errors[:tracking_number].should include subject.errors.generate_message(:tracking_number, :invalid)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
@@ -1,155 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
3
|
-
|
4
|
-
describe "Twitter Validation" do
|
5
|
-
before(:each) do
|
6
|
-
TestRecord.reset_callbacks(:validate)
|
7
|
-
TestRecord.validates :twitter_username, :twitter => true
|
8
|
-
end
|
9
|
-
|
10
|
-
subject { TestRecord.new }
|
11
|
-
|
12
|
-
it "rejects invalid urls" do
|
13
|
-
subject.should_not be_valid
|
14
|
-
subject.should have(1).error
|
15
|
-
end
|
16
|
-
|
17
|
-
it "generates an error message of type blank" do
|
18
|
-
subject.should_not be_valid
|
19
|
-
subject.errors[:twitter_username].should include subject.errors.generate_message(:twitter_username, :blank)
|
20
|
-
end
|
21
|
-
|
22
|
-
describe "for twitter url validator" do
|
23
|
-
before do
|
24
|
-
TestRecord.reset_callbacks(:validate)
|
25
|
-
TestRecord.validates :twitter_username, :twitter => { :format => :url }
|
26
|
-
end
|
27
|
-
|
28
|
-
it "validates with http" do
|
29
|
-
subject.twitter_username = 'http://twitter.com/garrettb'
|
30
|
-
subject.should be_valid
|
31
|
-
end
|
32
|
-
|
33
|
-
it "validates with https protocol" do
|
34
|
-
subject.twitter_username = 'https://twitter.com/garrettb'
|
35
|
-
subject.should be_valid
|
36
|
-
end
|
37
|
-
|
38
|
-
it "generate error with ftp protocol" do
|
39
|
-
subject.twitter_username = 'ftp://twitter.com/garrettb'
|
40
|
-
subject.should_not be_valid
|
41
|
-
subject.should have(1).error
|
42
|
-
end
|
43
|
-
|
44
|
-
it "validates with www and http" do
|
45
|
-
subject.twitter_username = 'http://www.twitter.com/garrettb'
|
46
|
-
subject.should be_valid
|
47
|
-
end
|
48
|
-
|
49
|
-
it "generate error without www dot" do
|
50
|
-
subject.twitter_username = 'http://wwwtwitter.com/garrettb'
|
51
|
-
subject.should_not be_valid
|
52
|
-
subject.should have(1).error
|
53
|
-
end
|
54
|
-
|
55
|
-
it "generate error without no username" do
|
56
|
-
subject.twitter_username = 'http://twitter.com'
|
57
|
-
subject.should_not be_valid
|
58
|
-
subject.should have(1).error
|
59
|
-
end
|
60
|
-
|
61
|
-
it "generate error without no username and trailing slash" do
|
62
|
-
subject.twitter_username = 'http://twitter.com/'
|
63
|
-
subject.should_not be_valid
|
64
|
-
subject.should have(1).error
|
65
|
-
end
|
66
|
-
|
67
|
-
it "generate error with too long of username" do
|
68
|
-
subject.twitter_username = 'http://twitter.com/garrettbjerkhoelwashere'
|
69
|
-
subject.should_not be_valid
|
70
|
-
subject.should have(1).error
|
71
|
-
end
|
72
|
-
|
73
|
-
it "generate error with invalid character" do
|
74
|
-
subject.twitter_username = 'http://twitter.com/garrettbjerkhoé'
|
75
|
-
subject.should_not be_valid
|
76
|
-
subject.should have(1).error
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
describe "for twitter at sign validator" do
|
81
|
-
before do
|
82
|
-
TestRecord.reset_callbacks(:validate)
|
83
|
-
TestRecord.validates :twitter_username, :twitter => { :format => :username_with_at }
|
84
|
-
end
|
85
|
-
|
86
|
-
it "validate with valid username" do
|
87
|
-
subject.twitter_username = '@garrettb'
|
88
|
-
subject.should be_valid
|
89
|
-
end
|
90
|
-
|
91
|
-
it "validate with one character" do
|
92
|
-
subject.twitter_username = '@a'
|
93
|
-
subject.should be_valid
|
94
|
-
end
|
95
|
-
|
96
|
-
it "generate error with too long of username" do
|
97
|
-
subject.twitter_username = '@garrettbjerkhoelwashere'
|
98
|
-
subject.should_not be_valid
|
99
|
-
subject.should have(1).error
|
100
|
-
end
|
101
|
-
|
102
|
-
it "generate error with no username" do
|
103
|
-
subject.twitter_username = '@'
|
104
|
-
subject.should_not be_valid
|
105
|
-
subject.should have(1).error
|
106
|
-
end
|
107
|
-
|
108
|
-
it "generate error with invalid character" do
|
109
|
-
subject.twitter_username = '@érik'
|
110
|
-
subject.should_not be_valid
|
111
|
-
subject.should have(1).error
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
describe "for twitter without at sign validator" do
|
116
|
-
before do
|
117
|
-
TestRecord.reset_callbacks(:validate)
|
118
|
-
TestRecord.validates :twitter_username, :twitter => true
|
119
|
-
end
|
120
|
-
|
121
|
-
it "validate with valid username" do
|
122
|
-
subject.twitter_username = 'garrettb'
|
123
|
-
subject.should be_valid
|
124
|
-
end
|
125
|
-
|
126
|
-
it "validate with one character" do
|
127
|
-
subject.twitter_username = 'a'
|
128
|
-
subject.should be_valid
|
129
|
-
end
|
130
|
-
|
131
|
-
it "generate error with too long of username" do
|
132
|
-
subject.twitter_username = 'garrettbjerkhoelwashere'
|
133
|
-
subject.should_not be_valid
|
134
|
-
subject.should have(1).error
|
135
|
-
end
|
136
|
-
|
137
|
-
it "generate error with no username" do
|
138
|
-
subject.twitter_username = ''
|
139
|
-
subject.should_not be_valid
|
140
|
-
subject.should have(1).error
|
141
|
-
end
|
142
|
-
|
143
|
-
it "generate error with invalid character" do
|
144
|
-
subject.twitter_username = 'érik'
|
145
|
-
subject.should_not be_valid
|
146
|
-
subject.should have(1).error
|
147
|
-
end
|
148
|
-
|
149
|
-
it "generate error with at sign character" do
|
150
|
-
subject.twitter_username = '@garrettb'
|
151
|
-
subject.should_not be_valid
|
152
|
-
subject.should have(1).error
|
153
|
-
end
|
154
|
-
end
|
155
|
-
end
|