validates_formatting_of 0.3.6 → 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -1
- data/.travis.yml +1 -1
- data/CHANGELOG.md +6 -2
- data/LICENSE.txt +1 -1
- data/README.markdown +9 -7
- data/Rakefile +1 -1
- data/lib/validates_formatting_of/model_additions.rb +16 -17
- data/lib/validates_formatting_of/railtie.rb +1 -1
- data/lib/validates_formatting_of/validating_methods.rb +2 -2
- data/lib/validates_formatting_of/validation_messages.rb +3 -3
- data/lib/validates_formatting_of/version.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/validates_formatting_of/model_additions_spec.rb +29 -37
- metadata +10 -10
data/.rspec
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--color
|
1
|
+
--color
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## v0.3.7
|
2
|
+
|
3
|
+
* Bugfix in `:allow_nil` and `:allow_blank`
|
4
|
+
|
1
5
|
## v0.3.6
|
2
6
|
|
3
7
|
* Added a small mention of how to suggest validation patterns
|
@@ -20,9 +24,9 @@
|
|
20
24
|
|
21
25
|
## v0.2.1 -- v0.2.3
|
22
26
|
|
23
|
-
* Added several new methods and tests for validations.
|
27
|
+
* Added several new methods and tests for validations.
|
24
28
|
* Fixed several spelling and English issues in the README.
|
25
29
|
|
26
30
|
## v0.2.0
|
27
31
|
|
28
|
-
* initial release
|
32
|
+
* initial release
|
data/LICENSE.txt
CHANGED
@@ -4,4 +4,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
4
|
|
5
5
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
6
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# validates_formatting_of
|
2
|
+
[![Build Status](https://secure.travis-ci.org/mattdbridges/validates_formatting_of.png)](http://travis-ci.org/mattdbridges/validates_formatting_of)
|
3
|
+
[![Dependency Status](https://gemnasium.com/mattdbridges/validates_formatting_of.png?travis)](https://gemnasium.com/mattdbridges/validates_formatting_of)
|
2
4
|
|
3
5
|
The `validates_formatting_of` gem adds several convenient methods to validate things such as emails, urls, and phone numbers in a Rails application.
|
4
6
|
|
@@ -63,19 +65,19 @@ You can still add the following options when using `validates_formatting_of`:
|
|
63
65
|
class Purchases < ActiveRecord::Base
|
64
66
|
validates_formatting_of :cc, :using => :credit_card
|
65
67
|
end
|
66
|
-
|
68
|
+
|
67
69
|
### US Zipcodes
|
68
70
|
|
69
71
|
class Location < ActiveRecord::Base
|
70
72
|
validates_formatting_of :zipcode, :using => :us_zip
|
71
73
|
end
|
72
|
-
|
74
|
+
|
73
75
|
### US Phone numbers
|
74
76
|
|
75
77
|
class Phones < ActiveRecord::Base
|
76
78
|
validates_formatting_of :phone, :using => :us_phone
|
77
79
|
end
|
78
|
-
|
80
|
+
|
79
81
|
### IP Address
|
80
82
|
|
81
83
|
class Location < ActiveRecord::Base
|
@@ -85,8 +87,8 @@ You can still add the following options when using `validates_formatting_of`:
|
|
85
87
|
|
86
88
|
class User < ActiveRecord::Base
|
87
89
|
validates_formatting_of :ssn, :using => :ssn
|
88
|
-
end
|
89
|
-
|
90
|
+
end
|
91
|
+
|
90
92
|
### Hex Colors
|
91
93
|
|
92
94
|
class Color < ActiveRecord::Base
|
@@ -115,4 +117,4 @@ It is very easy to contribute to this gem. Full documentation to do so will be a
|
|
115
117
|
# Have Ideas?
|
116
118
|
|
117
119
|
Do you use a particular pattern on a regular basis that isn't here or you would like to contribute? For now, [create a new issue](https://github.com/mattdbridges/validates_formatting_of/issues/new) in the issue tracker. I would be more than happy to consider adding it to the project.
|
118
|
-
|
120
|
+
|
data/Rakefile
CHANGED
@@ -1,29 +1,28 @@
|
|
1
1
|
module ValidatesFormattingOf
|
2
2
|
module ModelAdditions
|
3
3
|
|
4
|
-
# Using validates_formatting_of is as simple as using Rails' built-in
|
4
|
+
# Using validates_formatting_of is as simple as using Rails' built-in
|
5
5
|
# validation methods in models.
|
6
|
-
#
|
6
|
+
#
|
7
7
|
# class User < ActiveRecord::Base
|
8
8
|
# validates_formatting_of :email, :using => :email
|
9
9
|
# end
|
10
|
-
#
|
11
|
-
# This call will ensure that the user-provided email is a valid email. This way,
|
12
|
-
# you will not need to find or write your own regex to validate. All of that
|
10
|
+
#
|
11
|
+
# This call will ensure that the user-provided email is a valid email. This way,
|
12
|
+
# you will not need to find or write your own regex to validate. All of that
|
13
13
|
# logic is contained within `validates_formatting_of`
|
14
|
-
def validates_formatting_of(attribute, opts
|
15
|
-
|
14
|
+
def validates_formatting_of(attribute, opts={})
|
16
15
|
regex_for_validation = opts[:regex] || validate_with(opts[:using])
|
17
|
-
|
18
|
-
# Add :allow_nil and :allow_blank options. Both are false by default.
|
19
|
-
allow_nil = opts[:allow_nil] || false
|
20
|
-
allow_blank = opts[:allow_blank] || false
|
21
16
|
validation_message = opts[:message] || ValidationMessages.message(opts[:using])
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
17
|
+
options = {
|
18
|
+
:format => {
|
19
|
+
:with => regex_for_validation,
|
20
|
+
:message => validation_message,
|
21
|
+
}
|
22
|
+
}
|
23
|
+
options.merge!(:allow_nil => opts[:allow_nil]) unless opts[:allow_nil].nil?
|
24
|
+
options.merge!(:allow_blank => opts[:allow_blank]) unless opts[:allow_blank].nil?
|
25
|
+
validates(attribute, options)
|
27
26
|
end
|
28
27
|
|
29
28
|
private
|
@@ -38,4 +37,4 @@ module ValidatesFormattingOf
|
|
38
37
|
@formatting ||= ValidatingMethods.new
|
39
38
|
end
|
40
39
|
end
|
41
|
-
end
|
40
|
+
end
|
@@ -51,11 +51,11 @@ module ValidatesFormattingOf
|
|
51
51
|
def hex_color
|
52
52
|
/^([A-F0-9]{6}|[A-F0-9]{3})$/i
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
# Ensures that what value is passed is a dollar amount of some kind
|
56
56
|
def dollars
|
57
57
|
/^\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/
|
58
58
|
end
|
59
59
|
|
60
60
|
end
|
61
|
-
end
|
61
|
+
end
|
@@ -2,7 +2,7 @@ module ValidatesFormattingOf
|
|
2
2
|
module ValidationMessages
|
3
3
|
|
4
4
|
class Messages
|
5
|
-
|
5
|
+
|
6
6
|
def self.hash
|
7
7
|
{
|
8
8
|
:email => "is not a valid email",
|
@@ -18,7 +18,7 @@ module ValidatesFormattingOf
|
|
18
18
|
:dollars => "is not a valid dollar amount"
|
19
19
|
}
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
end
|
23
23
|
|
24
24
|
extend self
|
@@ -28,4 +28,4 @@ module ValidatesFormattingOf
|
|
28
28
|
end
|
29
29
|
|
30
30
|
end
|
31
|
-
end
|
31
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,7 +3,6 @@ require 'spec_helper'
|
|
3
3
|
describe ValidatesFormattingOf::ModelAdditions do
|
4
4
|
|
5
5
|
describe "email" do
|
6
|
-
|
7
6
|
class Email < SuperModel::Base
|
8
7
|
validates_formatting_of :email, :using => :email
|
9
8
|
end
|
@@ -14,10 +13,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
14
13
|
Email.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").should be_valid
|
15
14
|
Email.new(:email => "this__???{}|__should@be-valid.com").should be_valid
|
16
15
|
end
|
17
|
-
|
18
16
|
end
|
19
|
-
describe "url" do
|
20
17
|
|
18
|
+
describe "url" do
|
21
19
|
class Webpage < SuperModel::Base
|
22
20
|
validates_formatting_of :url, :using => :url
|
23
21
|
end
|
@@ -28,10 +26,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
28
26
|
Webpage.new(:url => 'http://username:password@something-else.com').should be_valid
|
29
27
|
Webpage.new(:url => "something else").should_not be_valid
|
30
28
|
end
|
31
|
-
|
32
29
|
end
|
30
|
+
|
33
31
|
describe "us_zip" do
|
34
|
-
|
35
32
|
class USZip < SuperModel::Base
|
36
33
|
validates_formatting_of :zipcode, :using => :us_zip
|
37
34
|
end
|
@@ -42,10 +39,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
42
39
|
USZip.new(:zipcode => '23408234').should_not be_valid
|
43
40
|
USZip.new(:zipcode => 'invalid').should_not be_valid
|
44
41
|
end
|
45
|
-
|
46
42
|
end
|
43
|
+
|
47
44
|
describe "alpha" do
|
48
|
-
|
49
45
|
class Alpha < SuperModel::Base
|
50
46
|
validates_formatting_of :letters, :using => :alpha
|
51
47
|
end
|
@@ -55,10 +51,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
55
51
|
Alpha.new(:letters => 'adsufasodfksadjfskjdfha98').should_not be_valid
|
56
52
|
Alpha.new(:letters => 'asdf ausdpf98hasdfo alsdf ja8 sd').should_not be_valid
|
57
53
|
end
|
58
|
-
|
59
54
|
end
|
55
|
+
|
60
56
|
describe "alphanum" do
|
61
|
-
|
62
57
|
class Alphanum < SuperModel::Base
|
63
58
|
validates_formatting_of :letters_and_numbers, :using => :alphanum
|
64
59
|
end
|
@@ -68,10 +63,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
68
63
|
Alphanum.new(:letters_and_numbers => 'letters and numbers 123 with spaces').should be_valid
|
69
64
|
Alphanum.new(:letters_and_numbers => 'adding ; some special ** chars').should_not be_valid
|
70
65
|
end
|
71
|
-
|
72
66
|
end
|
67
|
+
|
73
68
|
describe "us_phone" do
|
74
|
-
|
75
69
|
class USPhone < SuperModel::Base
|
76
70
|
validates_formatting_of :phone_number, :using => :us_phone
|
77
71
|
end
|
@@ -82,10 +76,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
82
76
|
USPhone.new(:phone_number => '2349870238').should_not be_valid
|
83
77
|
USPhone.new(:phone_number => '12349870238').should_not be_valid
|
84
78
|
end
|
85
|
-
|
86
79
|
end
|
80
|
+
|
87
81
|
describe "ip_address" do
|
88
|
-
|
89
82
|
class IPAddress < SuperModel::Base
|
90
83
|
validates_formatting_of :ip, :using => :ip_address
|
91
84
|
end
|
@@ -97,12 +90,11 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
97
90
|
IPAddress.new(:ip => '127.0.0.1').should be_valid
|
98
91
|
IPAddress.new(:ip => '132.254.111.10').should be_valid
|
99
92
|
end
|
100
|
-
|
101
93
|
end
|
94
|
+
|
102
95
|
# For clarification, NONE of the following numbers are real credit card numbers.
|
103
96
|
# They only match the pattern. These were randomly made for testing.
|
104
97
|
describe "credit_card" do
|
105
|
-
|
106
98
|
class Client < SuperModel::Base
|
107
99
|
validates_formatting_of :cc, :using => :credit_card
|
108
100
|
end
|
@@ -112,8 +104,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
112
104
|
Client.new(:cc => '5422434400828888').should be_valid # Mastercard style
|
113
105
|
Client.new(:cc => '1233444444444444').should_not be_valid # fake
|
114
106
|
end
|
115
|
-
|
116
107
|
end
|
108
|
+
|
117
109
|
describe "ssn" do
|
118
110
|
class AnotherPerson < SuperModel::Base
|
119
111
|
validates_formatting_of :ssn, :using => :ssn
|
@@ -127,6 +119,7 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
127
119
|
AnotherPerson.new(:ssn => "23498.7234").should_not be_valid
|
128
120
|
end
|
129
121
|
end
|
122
|
+
|
130
123
|
describe "hex_color" do
|
131
124
|
class Color < SuperModel::Base
|
132
125
|
validates_formatting_of :color, :using => :hex_color
|
@@ -160,50 +153,49 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
160
153
|
end
|
161
154
|
end
|
162
155
|
describe "custom messages" do
|
163
|
-
|
164
156
|
class Message < SuperModel::Base
|
165
157
|
validates_formatting_of :first_name, :using => :alpha, :message => "is not a valid first name"
|
166
158
|
end
|
167
|
-
|
168
159
|
it "are allowed and can be used in displaying error messages" do
|
169
160
|
message = Message.new(:first_name => "invalid-first-name-123")
|
170
161
|
message.should_not be_valid
|
171
162
|
message.errors.keys.class.should eq Array
|
172
163
|
message.errors.full_messages.first.should =~ /is not a valid first name/
|
173
164
|
end
|
174
|
-
|
175
165
|
end
|
176
|
-
describe "default error messages" do
|
177
166
|
|
167
|
+
describe "default error messages" do
|
178
168
|
class Problems < SuperModel::Base
|
179
169
|
validates_formatting_of :name, :using => :alpha
|
180
170
|
end
|
181
|
-
|
182
171
|
it "set a default error" do
|
183
172
|
problems = Problems.new(:name => "sdfs12312dfsd")
|
184
173
|
problems.should_not be_valid
|
185
174
|
problems.errors.full_messages.first.should =~ /letters/i
|
186
|
-
|
187
175
|
email = Email.new(:email => "fake@email.address")
|
188
176
|
email.should_not be_valid
|
189
177
|
email.errors.full_messages.first.should =~ /email/i
|
190
178
|
end
|
191
|
-
|
192
179
|
end
|
180
|
+
|
181
|
+
=begin
|
193
182
|
# Currently, SuperModel's validations do not detect allow_blank or allow_nil
|
194
183
|
# This functionality has been tested separately in an empty Rails app with perfect
|
195
184
|
# results.
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
end
|
185
|
+
|
186
|
+
describe "nil and blank values" do
|
187
|
+
class People < SuperModel::Base
|
188
|
+
validates_formatting_of :last_name, :using => :alpha, :allow_blank => true
|
189
|
+
end
|
190
|
+
it "are allowed" do
|
191
|
+
p = People.new(:last_name => "something")
|
192
|
+
p.should be_valid
|
193
|
+
p.save!
|
194
|
+
p.reload
|
195
|
+
p.last_name = ""
|
196
|
+
p.should_not be_valid
|
197
|
+
end
|
198
|
+
end
|
199
|
+
=end
|
200
|
+
|
201
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_formatting_of
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70125953379060 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70125953379060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70125953378380 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70125953378380
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70125953377680 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70125953377680
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: supermodel
|
49
|
-
requirement: &
|
49
|
+
requirement: &70125953393220 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70125953393220
|
58
58
|
description: Common Rails validations wrapped in a gem.
|
59
59
|
email:
|
60
60
|
- mbridges.91@gmail.com
|