activevalidators 1.2.4 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +25 -29
- data/README.md +2 -2
- data/activevalidators.gemspec +6 -6
- data/lib/active_model/validations/phone_validator.rb +24 -2
- data/spec/validations/phone_spec.rb +51 -28
- data/spec/validations/twitter_spec.rb +1 -0
- metadata +11 -51
data/Gemfile.lock
CHANGED
@@ -1,61 +1,57 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
activevalidators (1.
|
5
|
-
activemodel (
|
6
|
-
activerecord (
|
7
|
-
date_validator (
|
8
|
-
mail
|
4
|
+
activevalidators (1.3.0)
|
5
|
+
activemodel (~> 3.0.0)
|
6
|
+
activerecord (~> 3.0.0)
|
7
|
+
date_validator (~> 0.6.1)
|
8
|
+
mail (~> 2.2.15)
|
9
9
|
|
10
10
|
GEM
|
11
11
|
remote: http://rubygems.org/
|
12
12
|
specs:
|
13
|
-
activemodel (3.0.
|
14
|
-
activesupport (= 3.0.
|
13
|
+
activemodel (3.0.5)
|
14
|
+
activesupport (= 3.0.5)
|
15
15
|
builder (~> 2.1.2)
|
16
16
|
i18n (~> 0.4)
|
17
|
-
activerecord (3.0.
|
18
|
-
activemodel (= 3.0.
|
19
|
-
activesupport (= 3.0.
|
17
|
+
activerecord (3.0.5)
|
18
|
+
activemodel (= 3.0.5)
|
19
|
+
activesupport (= 3.0.5)
|
20
20
|
arel (~> 2.0.2)
|
21
21
|
tzinfo (~> 0.3.23)
|
22
|
-
activesupport (3.0.
|
23
|
-
arel (2.0.
|
22
|
+
activesupport (3.0.5)
|
23
|
+
arel (2.0.9)
|
24
24
|
builder (2.1.2)
|
25
|
-
date_validator (0.
|
26
|
-
activemodel (
|
25
|
+
date_validator (0.6.1)
|
26
|
+
activemodel (~> 3.0.0)
|
27
27
|
diff-lcs (1.1.2)
|
28
|
-
i18n (0.
|
29
|
-
mail (2.2.
|
28
|
+
i18n (0.5.0)
|
29
|
+
mail (2.2.15)
|
30
30
|
activesupport (>= 2.3.6)
|
31
|
-
i18n (
|
31
|
+
i18n (>= 0.4.0)
|
32
32
|
mime-types (~> 1.16)
|
33
33
|
treetop (~> 1.4.8)
|
34
34
|
mime-types (1.16)
|
35
35
|
polyglot (0.3.1)
|
36
|
-
rspec (2.
|
37
|
-
rspec-core (~> 2.
|
38
|
-
rspec-expectations (~> 2.
|
39
|
-
rspec-mocks (~> 2.
|
40
|
-
rspec-core (2.1
|
41
|
-
rspec-expectations (2.
|
36
|
+
rspec (2.5.0)
|
37
|
+
rspec-core (~> 2.5.0)
|
38
|
+
rspec-expectations (~> 2.5.0)
|
39
|
+
rspec-mocks (~> 2.5.0)
|
40
|
+
rspec-core (2.5.1)
|
41
|
+
rspec-expectations (2.5.0)
|
42
42
|
diff-lcs (~> 1.1.2)
|
43
|
-
rspec-mocks (2.
|
43
|
+
rspec-mocks (2.5.0)
|
44
44
|
treetop (1.4.9)
|
45
45
|
polyglot (>= 0.3.1)
|
46
|
-
tzinfo (0.3.
|
46
|
+
tzinfo (0.3.25)
|
47
47
|
|
48
48
|
PLATFORMS
|
49
49
|
java
|
50
50
|
ruby
|
51
51
|
|
52
52
|
DEPENDENCIES
|
53
|
-
activemodel (>= 3.0.0)
|
54
|
-
activerecord (>= 3.0.0)
|
55
53
|
activevalidators!
|
56
54
|
bundler
|
57
|
-
date_validator (= 0.5.9)
|
58
|
-
mail
|
59
55
|
rspec
|
60
56
|
rspec-core
|
61
57
|
rspec-expectations
|
data/README.md
CHANGED
@@ -38,7 +38,7 @@ In your models, the gem provides new validators like `email`, or `url`:
|
|
38
38
|
|
39
39
|
class Account
|
40
40
|
validates :visa_card, :credit_card => { :type => :visa }
|
41
|
-
validates :credit_card, :credit_card => { :type => :
|
41
|
+
validates :credit_card, :credit_card => { :type => :any }
|
42
42
|
end
|
43
43
|
|
44
44
|
|
@@ -46,7 +46,7 @@ Exhaustive list of supported validators and their implementation:
|
|
46
46
|
|
47
47
|
* `email` : based on the `mail` gem
|
48
48
|
* `url` : based on a regular expression
|
49
|
-
* `phone` : based on a
|
49
|
+
* `phone` : based on a set of predefined masks
|
50
50
|
* `twitter` : based on a regular expression
|
51
51
|
* `slug` : based on `ActiveSupport::String#parameterize`
|
52
52
|
* `ip` : based on `Resolv::IPv[4|6]::Regex`
|
data/activevalidators.gemspec
CHANGED
@@ -4,9 +4,9 @@ $:.unshift lib unless $:.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = "activevalidators"
|
7
|
-
s.version = '1.
|
7
|
+
s.version = '1.3.0'
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = ["Franck Verrot", "Paco Guzmán", "Oriol Gual", "Garrett Bjerkhoel"]
|
9
|
+
s.authors = ["Franck Verrot", "Paco Guzmán", "Oriol Gual", "Garrett Bjerkhoel", "Renato Riccieri Santos Zannon"]
|
10
10
|
s.email = ["franck@verrot.fr"]
|
11
11
|
s.homepage = "http://github.com/cesario/activevalidators"
|
12
12
|
s.summary = %q{Collection of ActiveModel/ActiveRecord validations}
|
@@ -16,10 +16,10 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.add_development_dependency "rspec"
|
17
17
|
s.add_development_dependency "rspec-core"
|
18
18
|
s.add_development_dependency "rspec-expectations"
|
19
|
-
s.add_dependency 'activerecord', '
|
20
|
-
s.add_dependency 'activemodel', '
|
21
|
-
s.add_dependency 'mail'
|
22
|
-
s.add_dependency 'date_validator', '0.
|
19
|
+
s.add_dependency 'activerecord' , '~> 3.0.0'
|
20
|
+
s.add_dependency 'activemodel' , '~> 3.0.0'
|
21
|
+
s.add_dependency 'mail' , '~> 2.2.15'
|
22
|
+
s.add_dependency 'date_validator', '~> 0.6.1'
|
23
23
|
|
24
24
|
s.files = `git ls-files`.split("\n")
|
25
25
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -2,10 +2,32 @@ module ActiveModel
|
|
2
2
|
module Validations
|
3
3
|
class PhoneValidator < EachValidator
|
4
4
|
def validate_each(record, attribute, value)
|
5
|
-
|
5
|
+
@value = value
|
6
|
+
@formats = PhoneValidator.known_formats[options[:country]] || PhoneValidator.known_formats[:us]
|
7
|
+
unless matches_any?
|
6
8
|
record.errors.add(attribute)
|
7
9
|
end
|
8
10
|
end
|
11
|
+
|
12
|
+
def self.known_formats
|
13
|
+
@@known_formats ||=
|
14
|
+
{:us => ["###-###-####", "##########", "###.###.####", "### ### ####", "(###) ###-####"],
|
15
|
+
:brazil => ["## ####-####", "(##) ####-####", "##########"],
|
16
|
+
:france => ["## ## ## ## ##"],
|
17
|
+
:uk => ["#### ### ####"]}
|
18
|
+
end
|
19
|
+
|
20
|
+
def matches_any?
|
21
|
+
false if @formats.nil? or not @formats.respond_to?(:detect)
|
22
|
+
@formats.detect { |format| @value.match(PhoneValidator.regexp_from format) }
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def self.regexp_from(format)
|
28
|
+
Regexp.new "^"+(Regexp.escape format).gsub('\#','\d')+"$"
|
29
|
+
end
|
30
|
+
|
9
31
|
end
|
10
32
|
end
|
11
|
-
end
|
33
|
+
end
|
@@ -2,45 +2,68 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
|
2
2
|
|
3
3
|
describe "Phone Validation" do
|
4
4
|
|
5
|
-
before(:each) do
|
6
|
-
TestRecord.reset_callbacks(:validate)
|
7
|
-
TestRecord.validates :phone, :phone => true
|
8
|
-
end
|
9
|
-
|
10
5
|
subject { TestRecord.new }
|
11
6
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
17
12
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
23
18
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
29
42
|
|
30
|
-
it 'should validate format of phone with ### ### ####' do
|
31
|
-
subject.phone = '999 999 9999'
|
32
|
-
subject.should be_valid
|
33
|
-
subject.should have(0).errors
|
34
43
|
end
|
35
44
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|
40
60
|
end
|
41
61
|
|
62
|
+
|
42
63
|
describe "for invalid formats" do
|
43
64
|
before :each do
|
65
|
+
TestRecord.reset_callbacks(:validate)
|
66
|
+
TestRecord.validates :phone, :phone => true
|
44
67
|
subject.phone = '999'
|
45
68
|
end
|
46
69
|
|
metadata
CHANGED
@@ -1,24 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activevalidators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 2
|
9
|
-
- 4
|
10
|
-
version: 1.2.4
|
4
|
+
prerelease:
|
5
|
+
version: 1.3.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Franck Verrot
|
14
9
|
- "Paco Guzm\xC3\xA1n"
|
15
10
|
- Oriol Gual
|
16
11
|
- Garrett Bjerkhoel
|
12
|
+
- Renato Riccieri Santos Zannon
|
17
13
|
autorequire:
|
18
14
|
bindir: bin
|
19
15
|
cert_chain: []
|
20
16
|
|
21
|
-
date:
|
17
|
+
date: 2011-03-20 00:00:00 -04:00
|
22
18
|
default_executable:
|
23
19
|
dependencies:
|
24
20
|
- !ruby/object:Gem::Dependency
|
@@ -29,9 +25,6 @@ dependencies:
|
|
29
25
|
requirements:
|
30
26
|
- - ">="
|
31
27
|
- !ruby/object:Gem::Version
|
32
|
-
hash: 3
|
33
|
-
segments:
|
34
|
-
- 0
|
35
28
|
version: "0"
|
36
29
|
type: :development
|
37
30
|
version_requirements: *id001
|
@@ -43,9 +36,6 @@ dependencies:
|
|
43
36
|
requirements:
|
44
37
|
- - ">="
|
45
38
|
- !ruby/object:Gem::Version
|
46
|
-
hash: 3
|
47
|
-
segments:
|
48
|
-
- 0
|
49
39
|
version: "0"
|
50
40
|
type: :development
|
51
41
|
version_requirements: *id002
|
@@ -57,9 +47,6 @@ dependencies:
|
|
57
47
|
requirements:
|
58
48
|
- - ">="
|
59
49
|
- !ruby/object:Gem::Version
|
60
|
-
hash: 3
|
61
|
-
segments:
|
62
|
-
- 0
|
63
50
|
version: "0"
|
64
51
|
type: :development
|
65
52
|
version_requirements: *id003
|
@@ -71,9 +58,6 @@ dependencies:
|
|
71
58
|
requirements:
|
72
59
|
- - ">="
|
73
60
|
- !ruby/object:Gem::Version
|
74
|
-
hash: 3
|
75
|
-
segments:
|
76
|
-
- 0
|
77
61
|
version: "0"
|
78
62
|
type: :development
|
79
63
|
version_requirements: *id004
|
@@ -83,13 +67,8 @@ dependencies:
|
|
83
67
|
requirement: &id005 !ruby/object:Gem::Requirement
|
84
68
|
none: false
|
85
69
|
requirements:
|
86
|
-
- -
|
70
|
+
- - ~>
|
87
71
|
- !ruby/object:Gem::Version
|
88
|
-
hash: 7
|
89
|
-
segments:
|
90
|
-
- 3
|
91
|
-
- 0
|
92
|
-
- 0
|
93
72
|
version: 3.0.0
|
94
73
|
type: :runtime
|
95
74
|
version_requirements: *id005
|
@@ -99,13 +78,8 @@ dependencies:
|
|
99
78
|
requirement: &id006 !ruby/object:Gem::Requirement
|
100
79
|
none: false
|
101
80
|
requirements:
|
102
|
-
- -
|
81
|
+
- - ~>
|
103
82
|
- !ruby/object:Gem::Version
|
104
|
-
hash: 7
|
105
|
-
segments:
|
106
|
-
- 3
|
107
|
-
- 0
|
108
|
-
- 0
|
109
83
|
version: 3.0.0
|
110
84
|
type: :runtime
|
111
85
|
version_requirements: *id006
|
@@ -115,12 +89,9 @@ dependencies:
|
|
115
89
|
requirement: &id007 !ruby/object:Gem::Requirement
|
116
90
|
none: false
|
117
91
|
requirements:
|
118
|
-
- -
|
92
|
+
- - ~>
|
119
93
|
- !ruby/object:Gem::Version
|
120
|
-
|
121
|
-
segments:
|
122
|
-
- 0
|
123
|
-
version: "0"
|
94
|
+
version: 2.2.15
|
124
95
|
type: :runtime
|
125
96
|
version_requirements: *id007
|
126
97
|
- !ruby/object:Gem::Dependency
|
@@ -129,14 +100,9 @@ dependencies:
|
|
129
100
|
requirement: &id008 !ruby/object:Gem::Requirement
|
130
101
|
none: false
|
131
102
|
requirements:
|
132
|
-
- -
|
103
|
+
- - ~>
|
133
104
|
- !ruby/object:Gem::Version
|
134
|
-
|
135
|
-
segments:
|
136
|
-
- 0
|
137
|
-
- 5
|
138
|
-
- 9
|
139
|
-
version: 0.5.9
|
105
|
+
version: 0.6.1
|
140
106
|
type: :runtime
|
141
107
|
version_requirements: *id008
|
142
108
|
description: ActiveValidators is a collection of ActiveModel/ActiveRecord validations
|
@@ -192,23 +158,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
192
158
|
requirements:
|
193
159
|
- - ">="
|
194
160
|
- !ruby/object:Gem::Version
|
195
|
-
hash: 3
|
196
|
-
segments:
|
197
|
-
- 0
|
198
161
|
version: "0"
|
199
162
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
200
163
|
none: false
|
201
164
|
requirements:
|
202
165
|
- - ">="
|
203
166
|
- !ruby/object:Gem::Version
|
204
|
-
hash: 3
|
205
|
-
segments:
|
206
|
-
- 0
|
207
167
|
version: "0"
|
208
168
|
requirements: []
|
209
169
|
|
210
170
|
rubyforge_project:
|
211
|
-
rubygems_version: 1.
|
171
|
+
rubygems_version: 1.6.0
|
212
172
|
signing_key:
|
213
173
|
specification_version: 3
|
214
174
|
summary: Collection of ActiveModel/ActiveRecord validations
|