validates_formatting_of 0.3.7 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown
CHANGED
@@ -30,7 +30,10 @@ You can still add the following options when using `validates_formatting_of`:
|
|
30
30
|
|
31
31
|
* `:allow_nil`
|
32
32
|
* `:allow_blank`
|
33
|
-
* `:
|
33
|
+
* `:on`
|
34
|
+
* `:if`
|
35
|
+
* `:unless`
|
36
|
+
* You also specify the `:message` option to use a custom validation message.
|
34
37
|
|
35
38
|
# Available Formatting Validations
|
36
39
|
|
@@ -11,6 +11,13 @@ module ValidatesFormattingOf
|
|
11
11
|
# This call will ensure that the user-provided email is a valid email. This way,
|
12
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
|
+
#
|
15
|
+
# You can also pass conditions and options for Rails to use
|
16
|
+
# * :if
|
17
|
+
# * :unless
|
18
|
+
# * :allow_nil
|
19
|
+
# * :allow_blank
|
20
|
+
# * :on
|
14
21
|
def validates_formatting_of(attribute, opts={})
|
15
22
|
regex_for_validation = opts[:regex] || validate_with(opts[:using])
|
16
23
|
validation_message = opts[:message] || ValidationMessages.message(opts[:using])
|
@@ -20,8 +27,9 @@ module ValidatesFormattingOf
|
|
20
27
|
:message => validation_message,
|
21
28
|
}
|
22
29
|
}
|
23
|
-
|
24
|
-
|
30
|
+
%w(allow_nil allow_blank if unless on).each do |opt|
|
31
|
+
options.merge!(opt.to_sym => opts[opt.to_sym]) if opts[opt.to_sym].present?
|
32
|
+
end
|
25
33
|
validates(attribute, options)
|
26
34
|
end
|
27
35
|
|
@@ -3,7 +3,7 @@ module ValidatesFormattingOf
|
|
3
3
|
|
4
4
|
# This method is very close to allowing what is specified in RFC 5322 and RFC 5321
|
5
5
|
def email
|
6
|
-
|
6
|
+
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
|
7
7
|
end
|
8
8
|
|
9
9
|
# Taken from Ryan Bates' screencast on extracting gems. Works extremely well. Thanks Ryan!
|
@@ -32,9 +32,16 @@ module ValidatesFormattingOf
|
|
32
32
|
/^\d{5}(-\d{4})?$/
|
33
33
|
end
|
34
34
|
|
35
|
-
# US Phone numbers.
|
35
|
+
# US Phone numbers.
|
36
|
+
# Examples of valid formats:
|
37
|
+
# * ###.###.####
|
38
|
+
# * ###-###-####
|
39
|
+
# * (###) ###-####
|
40
|
+
# * (###)###-####
|
41
|
+
# * #########
|
42
|
+
# * ### ###-####
|
36
43
|
def us_phone
|
37
|
-
/^(
|
44
|
+
/^(\((\d{3})\)|\d{3})[ |\.|\-]?(\d{3})[ |\.|\-]?(\d{4})$/
|
38
45
|
end
|
39
46
|
|
40
47
|
# IP Address validation
|
@@ -12,6 +12,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
12
12
|
Email.new(:email => "mbridges.91@gmail.com").should be_valid
|
13
13
|
Email.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").should be_valid
|
14
14
|
Email.new(:email => "this__???{}|__should@be-valid.com").should be_valid
|
15
|
+
Email.new(:email => "visitorservices@vmfa.museum").should be_valid
|
16
|
+
Email.new(:email => "info@samoa.travel").should be_valid
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
@@ -72,8 +74,10 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
72
74
|
it "validates that the phone number provided is valid" do
|
73
75
|
USPhone.new(:phone_number => '(234) 234-3456').should be_valid
|
74
76
|
USPhone.new(:phone_number => '123 123 3456').should be_valid
|
77
|
+
USPhone.new(:phone_number => '1231233456').should be_valid
|
78
|
+
USPhone.new(:phone_number => '123.123.3456').should be_valid
|
75
79
|
USPhone.new(:phone_number => '(223)123-2347').should be_valid
|
76
|
-
USPhone.new(:phone_number => '
|
80
|
+
USPhone.new(:phone_number => '(223 123-2347').should_not be_valid
|
77
81
|
USPhone.new(:phone_number => '12349870238').should_not be_valid
|
78
82
|
end
|
79
83
|
end
|
@@ -98,7 +102,7 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
98
102
|
class Client < SuperModel::Base
|
99
103
|
validates_formatting_of :cc, :using => :credit_card
|
100
104
|
end
|
101
|
-
it "validates that the
|
105
|
+
it "validates that the credit card number provided is valid" do
|
102
106
|
Client.new(:cc => '4264-2879-1230-0000').should be_valid # Visa style
|
103
107
|
Client.new(:cc => '6011-1111-0000-2391').should be_valid # Discover style
|
104
108
|
Client.new(:cc => '5422434400828888').should be_valid # Mastercard style
|
@@ -136,6 +140,35 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
136
140
|
Color.new(:color => "sdfsdfsf").should_not be_valid
|
137
141
|
end
|
138
142
|
end
|
143
|
+
|
144
|
+
describe "validation options" do
|
145
|
+
class Phony < SuperModel::Base
|
146
|
+
validates_formatting_of :phone, :using => :us_phone, :on => :create
|
147
|
+
end
|
148
|
+
it "validates the phone formatting only on creation" do
|
149
|
+
option = Phony.create!(:phone => "(123) 234-4567")
|
150
|
+
option.phone = "123123123"
|
151
|
+
option.should be_valid
|
152
|
+
end
|
153
|
+
|
154
|
+
class Iffy < SuperModel::Base
|
155
|
+
validates_presence_of :name
|
156
|
+
validates_formatting_of :phone, :using => :us_phone, :if => lambda { |iffy| iffy.name == "Matthew" }
|
157
|
+
end
|
158
|
+
it "validates the phone formatting only if a name is specified" do
|
159
|
+
Iffy.new(:phone => "(123 345-4567", :name => "Bill").should be_valid
|
160
|
+
Iffy.new(:phone => "(123 345-4567", :name => "Matthew").should_not be_valid
|
161
|
+
end
|
162
|
+
|
163
|
+
class Unlessy < SuperModel::Base
|
164
|
+
validates_presence_of :name
|
165
|
+
validates_formatting_of :phone, :using => :us_phone, :unless => lambda { |unlessy| unlessy.name == "Matthew" }
|
166
|
+
end
|
167
|
+
it "validates the phone formatting only if a name is specified" do
|
168
|
+
Unlessy.new(:phone => "(123 345-4567", :name => "Bill").should_not be_valid
|
169
|
+
Unlessy.new(:phone => "(123 345-4567", :name => "Matthew").should be_valid
|
170
|
+
end
|
171
|
+
end
|
139
172
|
describe "dollars" do
|
140
173
|
class Money < SuperModel::Base
|
141
174
|
validates_formatting_of :amount, :using => :dollars
|
@@ -172,7 +205,7 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
172
205
|
problems = Problems.new(:name => "sdfs12312dfsd")
|
173
206
|
problems.should_not be_valid
|
174
207
|
problems.errors.full_messages.first.should =~ /letters/i
|
175
|
-
email = Email.new(:email => "
|
208
|
+
email = Email.new(:email => "not.an.email.address")
|
176
209
|
email.should_not be_valid
|
177
210
|
email.errors.full_messages.first.should =~ /email/i
|
178
211
|
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.
|
4
|
+
version: 0.4.0
|
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:
|
12
|
+
date: 2012-01-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70103698133420 !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: *70103698133420
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &70103698132140 !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: *70103698132140
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70103698131500 !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: *70103698131500
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: supermodel
|
49
|
-
requirement: &
|
49
|
+
requirement: &70103698130720 !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: *70103698130720
|
58
58
|
description: Common Rails validations wrapped in a gem.
|
59
59
|
email:
|
60
60
|
- mbridges.91@gmail.com
|