validates_formatting_of 0.6.9 → 0.7.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 +1 -0
- data/CHANGELOG.md +10 -0
- data/README.markdown +106 -48
- data/lib/validates_formatting_of/method.rb +1 -5
- data/lib/validates_formatting_of/validation.rb +6 -2
- data/lib/validates_formatting_of/validation_addition.rb +17 -6
- data/lib/validates_formatting_of/version.rb +1 -1
- data/spec/spec_helper.rb +6 -1
- data/spec/validates_formatting_of/model_additions_spec.rb +7 -22
- data/spec/validates_formatting_of/validation_addition_spec.rb +12 -1
- metadata +2 -2
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
## v0.7.0
|
2
|
+
|
3
|
+
* Removed :ip_address validation method
|
4
|
+
* Bug fix for :simple_email validation. Identical to :email validation.
|
5
|
+
|
6
|
+
There were a few changes between v0.5.0 and v0.7.0, however I neglected to updated them here. They are as follows:
|
7
|
+
|
8
|
+
* Refactored entire gem for better testing
|
9
|
+
* Deprecating and then removing :ip_address validation in favor of :ip_address_v4 validation.
|
10
|
+
|
1
11
|
## v0.5.0
|
2
12
|
* Dropped support for Ruby 1.8.7
|
3
13
|
* Dropped support for REE
|
data/README.markdown
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# Validates Formatting Of
|
2
|
+
|
2
3
|
[](http://travis-ci.org/mattdbridges/validates_formatting_of)
|
3
4
|
[](https://gemnasium.com/mattdbridges/validates_formatting_of)
|
4
5
|
|
6
|
+
[](https://codeclimate.com/github/mattdbridges/validates_formatting_of)
|
7
|
+
|
5
8
|
The `validates_formatting_of` gem adds several convenient methods to validate things such as emails, urls, and phone numbers in a Rails application.
|
6
9
|
|
7
10
|
# Unsupported versions
|
@@ -23,13 +26,23 @@ Then bundle install:
|
|
23
26
|
|
24
27
|
# Usage
|
25
28
|
|
26
|
-
Using validates_formatting_of is as simple as using Rails' built-in validation methods in models.
|
29
|
+
Using `validates_formatting_of` is as simple as using Rails' built-in validation methods in models.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class User < ActiveRecord::Base
|
33
|
+
validates_formatting_of :email
|
34
|
+
end
|
35
|
+
```
|
27
36
|
|
28
|
-
|
29
|
-
validates_formatting_of :email, :using => :email
|
30
|
-
end
|
37
|
+
If the name of the column is identical to the validation method you would use, it auto-selects that validation method. Thus, this is a shortcut for the following:
|
31
38
|
|
32
|
-
|
39
|
+
```ruby
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
validates_formatting_of :email, :using => :email
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
This call will ensure that the user-provided email is a valid email. This way, you will not need to find or write your own regex to validate. All of that logic is contained within `validates_formatting_of`.
|
33
46
|
|
34
47
|
# Rails validation options still available
|
35
48
|
|
@@ -48,83 +61,128 @@ You can still add the following options when using `validates_formatting_of`:
|
|
48
61
|
|
49
62
|
### Email
|
50
63
|
|
51
|
-
|
52
|
-
|
53
|
-
|
64
|
+
```ruby
|
65
|
+
class User < ActiveRecord::Base
|
66
|
+
validates_formatting_of :email, :using => :email
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
**Note**: If you use `client_side_validations` then you need to use the following validation method:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class User < ActiveRecord::Base
|
74
|
+
validates_formatting_of :email, :using => :simple_email
|
75
|
+
end
|
76
|
+
```
|
54
77
|
|
55
78
|
### URL
|
56
79
|
|
57
|
-
|
58
|
-
|
59
|
-
|
80
|
+
```ruby
|
81
|
+
class Sites < ActiveRecord::Base
|
82
|
+
validates_formatting_of :website, :using => :url
|
83
|
+
end
|
84
|
+
```
|
60
85
|
|
61
86
|
### Alpha
|
62
87
|
|
63
|
-
|
64
|
-
|
65
|
-
|
88
|
+
```ruby
|
89
|
+
class Name < ActiveRecord::Base
|
90
|
+
validates_formatting_of :first_name, :using => :alpha
|
91
|
+
end
|
92
|
+
```
|
66
93
|
|
67
94
|
### Alphanumeric
|
68
95
|
|
69
|
-
|
70
|
-
|
71
|
-
|
96
|
+
```ruby
|
97
|
+
class Sites < ActiveRecord::Base
|
98
|
+
validates_formatting_of :text, :using => :alphanum
|
99
|
+
end
|
100
|
+
```
|
72
101
|
|
73
102
|
### Credit Card (Visa, Mastercard, Discover, and American Express)
|
74
103
|
|
75
|
-
|
76
|
-
|
77
|
-
|
104
|
+
```ruby
|
105
|
+
class Purchases < ActiveRecord::Base
|
106
|
+
validates_formatting_of :cc, :using => :credit_card
|
107
|
+
end
|
108
|
+
```
|
78
109
|
|
79
110
|
### US Zipcodes
|
80
111
|
|
81
|
-
|
82
|
-
|
83
|
-
|
112
|
+
```ruby
|
113
|
+
class Location < ActiveRecord::Base
|
114
|
+
validates_formatting_of :zipcode, :using => :us_zip
|
115
|
+
end
|
116
|
+
```
|
84
117
|
|
85
118
|
### US Phone numbers
|
86
119
|
|
87
|
-
|
88
|
-
|
89
|
-
|
120
|
+
```ruby
|
121
|
+
class Phones < ActiveRecord::Base
|
122
|
+
validates_formatting_of :phone, :using => :us_phone
|
123
|
+
end
|
124
|
+
```
|
90
125
|
|
91
|
-
###
|
126
|
+
### IPv4
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
class Location < ActiveRecord::Base
|
130
|
+
validates_formatting_of :ip, :using => :ip_address_v4
|
131
|
+
end
|
132
|
+
```
|
92
133
|
|
93
|
-
class Location < ActiveRecord::Base
|
94
|
-
validates_formatting_of :website, :using => :ip_address
|
95
|
-
end
|
96
134
|
### Social Security Number
|
97
135
|
|
98
|
-
|
99
|
-
|
100
|
-
|
136
|
+
```ruby
|
137
|
+
class User < ActiveRecord::Base
|
138
|
+
validates_formatting_of :ssn, :using => :ssn
|
139
|
+
end
|
140
|
+
```
|
101
141
|
|
102
142
|
### Hex Colors
|
103
143
|
|
104
|
-
|
105
|
-
|
106
|
-
|
144
|
+
```ruby
|
145
|
+
class Color < ActiveRecord::Base
|
146
|
+
validates_formatting_of :color, :using => :hex_color
|
147
|
+
end
|
148
|
+
```
|
107
149
|
|
108
150
|
### Dollar Amount
|
109
151
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
152
|
+
```ruby
|
153
|
+
class Invoice < ActiveRecord::Base
|
154
|
+
validates_formatting_of :amount, :using => :dollars
|
155
|
+
end
|
156
|
+
```
|
115
157
|
|
116
|
-
|
158
|
+
# Over-writable
|
117
159
|
|
160
|
+
If, for any reason, you want to overwrite the regex specified in the gem, you can using the `:with` option just like in ActiveModel's built-in `validates_format_of`.
|
118
161
|
|
119
|
-
|
120
|
-
|
121
|
-
|
162
|
+
```ruby
|
163
|
+
class Person < ActiveRecord::Base
|
164
|
+
validates_formatting_of :first_name, :with => /[A-Z]/i
|
165
|
+
end
|
166
|
+
```
|
122
167
|
|
123
168
|
# Development and Contribution
|
124
169
|
|
125
|
-
|
170
|
+
To add a simple regex validation, all that is necessary is to add a single line to the `Method` module in the `lib/method.rb` file (with a line or two of documentation explaining it's use).
|
171
|
+
|
172
|
+
For example:
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
module Method
|
176
|
+
# This :validation_name method is for example purposes only.
|
177
|
+
# You can use this in production, but it would require a value of 'example regex' to pass.
|
178
|
+
add :validation_name, %r{example regex}iux, "is a message for validation method :validation_name"
|
179
|
+
end
|
180
|
+
```
|
181
|
+
|
182
|
+
1. Fork it
|
183
|
+
2. Make it awesome (with passing tests)
|
184
|
+
3. Create a new Pull Request.
|
126
185
|
|
127
186
|
# Have Ideas?
|
128
187
|
|
129
188
|
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.
|
130
|
-
|
@@ -2,9 +2,6 @@ require 'validates_formatting_of/validation'
|
|
2
2
|
require 'validates_formatting_of/validation_addition'
|
3
3
|
|
4
4
|
module ValidatesFormattingOf
|
5
|
-
|
6
|
-
class MissingValidation < StandardError; end
|
7
|
-
|
8
5
|
module Method
|
9
6
|
|
10
7
|
extend ValidationAddition
|
@@ -15,7 +12,7 @@ module ValidatesFormattingOf
|
|
15
12
|
# Thie version of the email exists to support common validation libraries (such
|
16
13
|
# as client_side_validations) that require access to Rails' validation. Look-behinds
|
17
14
|
# are not supported in javascript.
|
18
|
-
add :simple_email, %r{\A([^@\s]+)@((?:
|
15
|
+
add :simple_email, %r{\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z}i, "is not a valid email"
|
19
16
|
|
20
17
|
# Taken from Ryan Bates' screencast on extracting gems. Works extremely well. Thanks Ryan!
|
21
18
|
# (slightly revised to work on MRI 1.8.7 and ree)
|
@@ -44,7 +41,6 @@ module ValidatesFormattingOf
|
|
44
41
|
add :us_phone, %r{\A(\((\d{3})\)|\d{3})[ |\.|\-]?(\d{3})[ |\.|\-]?(\d{4})\Z}, "is not a valid phone number"
|
45
42
|
|
46
43
|
# IP Address validation
|
47
|
-
add :ip_address, %r{\A(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\Z}, "is not a valid IPv4 address"
|
48
44
|
add :ip_address_v4, %r{\A(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\Z}, "is not a valid IPv4 address"
|
49
45
|
|
50
46
|
# Social Security Number pattern
|
@@ -1,6 +1,10 @@
|
|
1
1
|
module ValidatesFormattingOf
|
2
2
|
|
3
|
-
class InvalidRegularExpression < StandardError
|
3
|
+
class InvalidRegularExpression < StandardError
|
4
|
+
def initialize(name)
|
5
|
+
super("You must specify a Regexp, a proc, or a lambda for the #{name.inspect} validation.")
|
6
|
+
end
|
7
|
+
end
|
4
8
|
|
5
9
|
class Validation
|
6
10
|
attr_reader :name, :regex, :message
|
@@ -8,7 +12,7 @@ module ValidatesFormattingOf
|
|
8
12
|
def initialize(name, regexp, message = "is not correctly formatted")
|
9
13
|
callable = regexp.respond_to? :call
|
10
14
|
if !callable && regexp.class.to_s != "Regexp"
|
11
|
-
raise InvalidRegularExpression
|
15
|
+
raise InvalidRegularExpression.new(name)
|
12
16
|
end
|
13
17
|
@name, @regex, @message = name, regexp, message
|
14
18
|
end
|
@@ -1,4 +1,13 @@
|
|
1
|
+
require 'active_support/core_ext/object/blank'
|
2
|
+
|
1
3
|
module ValidatesFormattingOf
|
4
|
+
|
5
|
+
class MissingValidation < StandardError
|
6
|
+
def initialize(method)
|
7
|
+
super("The validation method #{method.to_sym.inspect} has not been defined.")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
2
11
|
module ValidationAddition
|
3
12
|
attr_reader :validations
|
4
13
|
|
@@ -8,18 +17,20 @@ module ValidatesFormattingOf
|
|
8
17
|
end
|
9
18
|
|
10
19
|
def find(attribute, opts = {})
|
11
|
-
method = opts
|
12
|
-
if
|
13
|
-
raise MissingValidation, "The validation method #{method.to_sym.inspect} has not been defined."
|
14
|
-
end
|
20
|
+
method = opts.fetch(:using, attribute)
|
21
|
+
raise MissingValidation.new(method) if missing? method
|
15
22
|
if method.to_sym == :ip_address
|
16
|
-
|
23
|
+
raise ArgumentError, "[REMOVED] The :ip_address validation for `validates_formatting_of` has been removed since version 0.7.0. Please update your model validations to use :ip_address_v4."
|
17
24
|
end
|
18
25
|
@validations[method.to_sym]
|
19
26
|
end
|
20
27
|
|
28
|
+
def missing?(name)
|
29
|
+
!exists?(name)
|
30
|
+
end
|
31
|
+
|
21
32
|
def exists?(name)
|
22
|
-
|
33
|
+
@validations[name.to_sym].present?
|
23
34
|
end
|
24
35
|
end
|
25
36
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -109,33 +109,18 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
109
109
|
end
|
110
110
|
end
|
111
111
|
|
112
|
-
describe "ip_address" do
|
113
|
-
class IPAddress < TestActiveRecord
|
114
|
-
attr_accessor :ip
|
115
|
-
validates_formatting_of :ip, :using => :ip_address
|
116
|
-
end
|
117
|
-
it "validates that the IP address provided is valid" do
|
118
|
-
IPAddress.new(:ip => '10.10.10').should_not be_valid
|
119
|
-
IPAddress.new(:ip => '999.10.10.20').should_not be_valid
|
120
|
-
IPAddress.new(:ip => '2222.22.22.22').should_not be_valid
|
121
|
-
IPAddress.new(:ip => '22.2222.22.2').should_not be_valid
|
122
|
-
IPAddress.new(:ip => '127.0.0.1').should be_valid
|
123
|
-
IPAddress.new(:ip => '132.254.111.10').should be_valid
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
112
|
describe "ip_address_v4" do
|
128
|
-
class
|
113
|
+
class IPAddress < TestActiveRecord
|
129
114
|
attr_accessor :ipv4
|
130
115
|
validates_formatting_of :ipv4, :using => :ip_address_v4
|
131
116
|
end
|
132
117
|
it "validates that the IP address provided is valid" do
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
118
|
+
IPAddress.new(:ipv4 => '10.10.10').should_not be_valid
|
119
|
+
IPAddress.new(:ipv4 => '999.10.10.20').should_not be_valid
|
120
|
+
IPAddress.new(:ipv4 => '2222.22.22.22').should_not be_valid
|
121
|
+
IPAddress.new(:ipv4 => '22.2222.22.2').should_not be_valid
|
122
|
+
IPAddress.new(:ipv4 => '127.0.0.1').should be_valid
|
123
|
+
IPAddress.new(:ipv4 => '132.254.111.10').should be_valid
|
139
124
|
end
|
140
125
|
end
|
141
126
|
|
@@ -10,6 +10,9 @@ module ValidatesFormattingOf
|
|
10
10
|
before do
|
11
11
|
TestAdding.add :email, /email/i
|
12
12
|
end
|
13
|
+
after do
|
14
|
+
TestAdding.instance_variable_set("@validations", nil)
|
15
|
+
end
|
13
16
|
it "should be able to add new validations" do
|
14
17
|
TestAdding.add :another, /another/i
|
15
18
|
TestAdding.validations.count.should == 2
|
@@ -27,9 +30,17 @@ module ValidatesFormattingOf
|
|
27
30
|
it "should raise an error if the method does not exist" do
|
28
31
|
expect { TestAdding.find(:fake) }.to raise_error MissingValidation
|
29
32
|
end
|
30
|
-
it "should be able to determine if the method
|
33
|
+
it "should be able to determine if the method does not exist" do
|
31
34
|
TestAdding.exists?(:email).should be_true
|
32
35
|
TestAdding.exists?(:non_existent).should be_false
|
33
36
|
end
|
37
|
+
it "should be able to accept strings for validation names (for namespacing)" do
|
38
|
+
TestAdding.add "namespace:phone", /namespace/i
|
39
|
+
TestAdding.exists?("namespace:phone").should be_true
|
40
|
+
end
|
41
|
+
it "should be able to determine if the method is missing" do
|
42
|
+
TestAdding.missing?(:non_existent).should be_true
|
43
|
+
TestAdding.missing?(:email).should be_false
|
44
|
+
end
|
34
45
|
end
|
35
46
|
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.7.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|