validates_formatting_of 0.6.2 → 0.6.9
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/.rspec +1 -0
- data/lib/validates_formatting_of.rb +1 -2
- data/lib/validates_formatting_of/method.rb +60 -0
- data/lib/validates_formatting_of/model_additions.rb +14 -23
- data/lib/validates_formatting_of/validation.rb +24 -0
- data/lib/validates_formatting_of/validation_addition.rb +25 -0
- data/lib/validates_formatting_of/version.rb +1 -1
- data/spec/spec_helper.rb +24 -3
- data/spec/validates_formatting_of/model_additions_spec.rb +74 -54
- data/spec/validates_formatting_of/validation_addition_spec.rb +35 -0
- data/spec/validates_formatting_of/validation_spec.rb +36 -0
- data/validates_formatting_of.gemspec +1 -1
- metadata +10 -5
- data/lib/validates_formatting_of/validating_methods.rb +0 -82
- data/lib/validates_formatting_of/validation_messages.rb +0 -24
data/.rspec
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require "validates_formatting_of/version"
|
2
|
-
require "validates_formatting_of/
|
3
|
-
require "validates_formatting_of/validating_methods"
|
2
|
+
require "validates_formatting_of/method"
|
4
3
|
require "validates_formatting_of/model_additions"
|
5
4
|
require "validates_formatting_of/railtie" if defined? Rails
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'validates_formatting_of/validation'
|
2
|
+
require 'validates_formatting_of/validation_addition'
|
3
|
+
|
4
|
+
module ValidatesFormattingOf
|
5
|
+
|
6
|
+
class MissingValidation < StandardError; end
|
7
|
+
|
8
|
+
module Method
|
9
|
+
|
10
|
+
extend ValidationAddition
|
11
|
+
|
12
|
+
# This method is very close to allowing what is specified in RFC 5322 and RFC 5321
|
13
|
+
add :email, %r{\A([^@\s]+)@((?:(?!-)[-a-z0-9]+(?<!-)\.)+[a-z]{2,})\Z}i, "is not a valid email"
|
14
|
+
|
15
|
+
# Thie version of the email exists to support common validation libraries (such
|
16
|
+
# as client_side_validations) that require access to Rails' validation. Look-behinds
|
17
|
+
# are not supported in javascript.
|
18
|
+
add :simple_email, %r{\A([^@\s]+)@((?:(?!-)[-a-z0-9]+(?<!-)\.)+[a-z]{2,})\Z}i, "is not a valid email"
|
19
|
+
|
20
|
+
# Taken from Ryan Bates' screencast on extracting gems. Works extremely well. Thanks Ryan!
|
21
|
+
# (slightly revised to work on MRI 1.8.7 and ree)
|
22
|
+
add :url, %r{\Ahttps?:\/\/([^\s:@]+:[^\s:@]*@)?[A-Za-z\d\-]+(\.[A-Za-z\d\-]+)+\.?(:\d{1,5})?([\/?]\S*)?\Z}i, "is not a valid URL"
|
23
|
+
|
24
|
+
# No numbers of symbols. allows "-"
|
25
|
+
add :alpha, %r{\A([^\d\W]|[-])*\Z}, "must be only letters or dashes"
|
26
|
+
|
27
|
+
# Letters, numbers, and spaces
|
28
|
+
add :alphanum, %r{\A[A-Z0-9\s]+\Z}i, "must be letters, numbers"
|
29
|
+
|
30
|
+
# Visa, Mastercard, Discver, and American Express
|
31
|
+
add :credit_card, %r{\A((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}\Z}, "is not a valid credit card number"
|
32
|
+
|
33
|
+
# US Zip code. ##### or #####-####
|
34
|
+
add :us_zip, %r{\A\d{5}(-\d{4})?\Z}, "is not a valid zipcode"
|
35
|
+
|
36
|
+
# US Phone numbers.
|
37
|
+
# Examples of valid formats:
|
38
|
+
# * ###.###.####
|
39
|
+
# * ###-###-####
|
40
|
+
# * (###) ###-####
|
41
|
+
# * (###)###-####
|
42
|
+
# * #########
|
43
|
+
# * ### ###-####
|
44
|
+
add :us_phone, %r{\A(\((\d{3})\)|\d{3})[ |\.|\-]?(\d{3})[ |\.|\-]?(\d{4})\Z}, "is not a valid phone number"
|
45
|
+
|
46
|
+
# 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
|
+
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
|
+
|
50
|
+
# Social Security Number pattern
|
51
|
+
add :ssn, %r{\A\d{3}([-.]){1}\d{2}([-.]){1}\d{4}\Z}, "is not a valid social security number"
|
52
|
+
|
53
|
+
# Matches CSS colors either in three or six digit formats
|
54
|
+
add :hex_color, %r{\A([A-F0-9]{6}|[A-F0-9]{3})\Z}i, "is not a valid hex color"
|
55
|
+
|
56
|
+
# Ensures that what value is passed is a dollar amount of some kind
|
57
|
+
add :dollars, %r{\A\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\Z}, "is not a valid dollar amount"
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/hash/reverse_merge'
|
2
|
+
|
1
3
|
module ValidatesFormattingOf
|
2
4
|
module ModelAdditions
|
3
5
|
|
@@ -8,36 +10,25 @@ module ValidatesFormattingOf
|
|
8
10
|
# validates_formatting_of :email, :using => :email
|
9
11
|
# end
|
10
12
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
13
|
+
# If your column name is idencital to any of the built-in methods, you
|
14
|
+
# may leave off the `:using` option and validates_formatting_of will
|
15
|
+
# automatically use the validation with the matching name.
|
16
|
+
#
|
17
|
+
# class User < ActiveRecord::Base
|
18
|
+
# validates_formatting_of :email
|
19
|
+
# end
|
20
|
+
#
|
15
21
|
# You can also pass conditions and options for Rails to use
|
16
22
|
# * :if
|
17
23
|
# * :unless
|
18
24
|
# * :allow_nil
|
19
25
|
# * :allow_blank
|
20
26
|
# * :on
|
21
|
-
def validates_formatting_of(attribute,
|
22
|
-
|
23
|
-
|
24
|
-
options
|
25
|
-
:format => {
|
26
|
-
:with => regex_for_validation,
|
27
|
-
:message => validation_message,
|
28
|
-
}
|
29
|
-
}
|
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
|
33
|
-
validates(attribute, options)
|
27
|
+
def validates_formatting_of(attribute, options = {})
|
28
|
+
validation = Method.find(attribute, options)
|
29
|
+
options.reverse_merge!(:with => validation.regex, :message => validation.message)
|
30
|
+
self.validates_format_of(attribute, options)
|
34
31
|
end
|
35
32
|
|
36
|
-
private
|
37
|
-
|
38
|
-
def validate_with(method)
|
39
|
-
# Actually retrieve the regex to check against
|
40
|
-
ValidatingMethods.send(method)
|
41
|
-
end
|
42
33
|
end
|
43
34
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ValidatesFormattingOf
|
2
|
+
|
3
|
+
class InvalidRegularExpression < StandardError; end
|
4
|
+
|
5
|
+
class Validation
|
6
|
+
attr_reader :name, :regex, :message
|
7
|
+
|
8
|
+
def initialize(name, regexp, message = "is not correctly formatted")
|
9
|
+
callable = regexp.respond_to? :call
|
10
|
+
if !callable && regexp.class.to_s != "Regexp"
|
11
|
+
raise InvalidRegularExpression, "You must specify a Regexp, a proc, or a lambda for the #{name.inspect} validation."
|
12
|
+
end
|
13
|
+
@name, @regex, @message = name, regexp, message
|
14
|
+
end
|
15
|
+
|
16
|
+
def inspect
|
17
|
+
"#<Validation name: #{name.inspect}, regex: #{regex.inspect}, message: #{message}>"
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
"<Validation::#{name.to_s}>"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ValidatesFormattingOf
|
2
|
+
module ValidationAddition
|
3
|
+
attr_reader :validations
|
4
|
+
|
5
|
+
def add(name, regex, message = nil)
|
6
|
+
@validations ||= {}
|
7
|
+
@validations[name.to_sym] = Validation.new(name.to_sym, regex, message)
|
8
|
+
end
|
9
|
+
|
10
|
+
def find(attribute, opts = {})
|
11
|
+
method = opts[:using].nil? ? attribute : opts[:using]
|
12
|
+
if !exists? method
|
13
|
+
raise MissingValidation, "The validation method #{method.to_sym.inspect} has not been defined."
|
14
|
+
end
|
15
|
+
if method.to_sym == :ip_address
|
16
|
+
warn "[DEPRECATION] The :ip_address validation for `validates_formatting_of` is DEPRECATED. Please update your model validations to use :ip_address_v4. This method will be removed by version 0.7.0."
|
17
|
+
end
|
18
|
+
@validations[method.to_sym]
|
19
|
+
end
|
20
|
+
|
21
|
+
def exists?(name)
|
22
|
+
!@validations[name.to_sym].nil?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,28 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
1
4
|
require 'validates_formatting_of'
|
2
|
-
require '
|
5
|
+
require 'active_model'
|
6
|
+
|
7
|
+
class TestActiveRecord
|
8
|
+
include ActiveModel::Validations
|
9
|
+
include ActiveModel::Conversion
|
3
10
|
|
4
|
-
class SuperModel::Base
|
5
|
-
include ActiveModel::Validations::Callbacks
|
6
11
|
extend ValidatesFormattingOf::ModelAdditions
|
12
|
+
|
13
|
+
def initialize(attributes = {})
|
14
|
+
attributes.each do |name, value|
|
15
|
+
send("#{name}=", value)
|
16
|
+
end
|
17
|
+
@persisted = false
|
18
|
+
end
|
19
|
+
|
20
|
+
def save
|
21
|
+
@persisted = true
|
22
|
+
end
|
23
|
+
|
24
|
+
def persisted?
|
25
|
+
@persisted ||= false
|
26
|
+
end
|
27
|
+
|
7
28
|
end
|
@@ -3,8 +3,9 @@ require 'spec_helper'
|
|
3
3
|
describe ValidatesFormattingOf::ModelAdditions do
|
4
4
|
|
5
5
|
describe "email" do
|
6
|
-
class Email <
|
7
|
-
|
6
|
+
class Email < TestActiveRecord
|
7
|
+
attr_accessor :email
|
8
|
+
validates_formatting_of :email
|
8
9
|
end
|
9
10
|
it "validates that the email provided is valid" do
|
10
11
|
Email.new(:email => "example@example.com").should be_valid
|
@@ -21,7 +22,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
21
22
|
end
|
22
23
|
|
23
24
|
describe "simple email for 1.8.7 and javascript validations (such as with client_side_validations)" do
|
24
|
-
class SimpleEmail <
|
25
|
+
class SimpleEmail < TestActiveRecord
|
26
|
+
attr_accessor :email
|
25
27
|
validates_formatting_of :email, :using => :simple_email
|
26
28
|
end
|
27
29
|
it "validates that the email provided is valid" do
|
@@ -38,8 +40,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
38
40
|
end
|
39
41
|
end
|
40
42
|
describe "url" do
|
41
|
-
class Webpage <
|
42
|
-
|
43
|
+
class Webpage < TestActiveRecord
|
44
|
+
attr_accessor :url
|
45
|
+
validates_formatting_of :url
|
43
46
|
end
|
44
47
|
it "validates that the url provided is valid" do
|
45
48
|
Webpage.new(:url => 'http://something.com').should be_valid
|
@@ -51,7 +54,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
51
54
|
end
|
52
55
|
|
53
56
|
describe "us_zip" do
|
54
|
-
class USZip <
|
57
|
+
class USZip < TestActiveRecord
|
58
|
+
attr_accessor :zipcode
|
55
59
|
validates_formatting_of :zipcode, :using => :us_zip
|
56
60
|
end
|
57
61
|
it "validates that the zipcode provided is valid" do
|
@@ -64,19 +68,21 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
64
68
|
end
|
65
69
|
|
66
70
|
describe "alpha" do
|
67
|
-
class Alpha <
|
68
|
-
|
71
|
+
class Alpha < TestActiveRecord
|
72
|
+
attr_accessor :alpha
|
73
|
+
validates_formatting_of :alpha
|
69
74
|
end
|
70
75
|
it "validates that the letters provided is valid" do
|
71
|
-
Alpha.new(:
|
72
|
-
Alpha.new(:
|
73
|
-
Alpha.new(:
|
74
|
-
Alpha.new(:
|
76
|
+
Alpha.new(:alpha => 'abscdsofjsdpfahdsofkajlsdfaspdhjfads').should be_valid
|
77
|
+
Alpha.new(:alpha => 'asdfalskdfjhas-dlfhasdksdfaldhfadsfasdfa').should be_valid
|
78
|
+
Alpha.new(:alpha => 'adsufasodfksadjfskjdfha98').should_not be_valid
|
79
|
+
Alpha.new(:alpha => 'asdf ausdpf98hasdfo alsdf ja8 sd').should_not be_valid
|
75
80
|
end
|
76
81
|
end
|
77
82
|
|
78
83
|
describe "alphanum" do
|
79
|
-
class Alphanum <
|
84
|
+
class Alphanum < TestActiveRecord
|
85
|
+
attr_accessor :letters_and_numbers
|
80
86
|
validates_formatting_of :letters_and_numbers, :using => :alphanum
|
81
87
|
end
|
82
88
|
it "validates that the letters provided is valid" do
|
@@ -88,7 +94,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
88
94
|
end
|
89
95
|
|
90
96
|
describe "us_phone" do
|
91
|
-
class USPhone <
|
97
|
+
class USPhone < TestActiveRecord
|
98
|
+
attr_accessor :phone_number
|
92
99
|
validates_formatting_of :phone_number, :using => :us_phone
|
93
100
|
end
|
94
101
|
it "validates that the phone number provided is valid" do
|
@@ -103,7 +110,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
103
110
|
end
|
104
111
|
|
105
112
|
describe "ip_address" do
|
106
|
-
class IPAddress <
|
113
|
+
class IPAddress < TestActiveRecord
|
114
|
+
attr_accessor :ip
|
107
115
|
validates_formatting_of :ip, :using => :ip_address
|
108
116
|
end
|
109
117
|
it "validates that the IP address provided is valid" do
|
@@ -117,23 +125,25 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
117
125
|
end
|
118
126
|
|
119
127
|
describe "ip_address_v4" do
|
120
|
-
class
|
121
|
-
|
128
|
+
class IPAddress2 < TestActiveRecord
|
129
|
+
attr_accessor :ipv4
|
130
|
+
validates_formatting_of :ipv4, :using => :ip_address_v4
|
122
131
|
end
|
123
132
|
it "validates that the IP address provided is valid" do
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
133
|
+
IPAddress2.new(:ipv4 => '10.10.10').should_not be_valid
|
134
|
+
IPAddress2.new(:ipv4 => '999.10.10.20').should_not be_valid
|
135
|
+
IPAddress2.new(:ipv4 => '2222.22.22.22').should_not be_valid
|
136
|
+
IPAddress2.new(:ipv4 => '22.2222.22.2').should_not be_valid
|
137
|
+
IPAddress2.new(:ipv4 => '127.0.0.1').should be_valid
|
138
|
+
IPAddress2.new(:ipv4 => '132.254.111.10').should be_valid
|
130
139
|
end
|
131
140
|
end
|
132
141
|
|
133
142
|
# For clarification, NONE of the following numbers are real credit card numbers.
|
134
143
|
# They only match the pattern. These were randomly made for testing.
|
135
144
|
describe "credit_card" do
|
136
|
-
class Client <
|
145
|
+
class Client < TestActiveRecord
|
146
|
+
attr_accessor :cc
|
137
147
|
validates_formatting_of :cc, :using => :credit_card
|
138
148
|
end
|
139
149
|
it "validates that the credit card number provided is valid" do
|
@@ -145,8 +155,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
145
155
|
end
|
146
156
|
|
147
157
|
describe "ssn" do
|
148
|
-
class AnotherPerson <
|
149
|
-
|
158
|
+
class AnotherPerson < TestActiveRecord
|
159
|
+
attr_accessor :ssn
|
160
|
+
validates_formatting_of :ssn
|
150
161
|
end
|
151
162
|
it "validates that the social security number provided is valid" do
|
152
163
|
AnotherPerson.new(:ssn => "145.47.0191").should be_valid
|
@@ -159,7 +170,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
159
170
|
end
|
160
171
|
|
161
172
|
describe "hex_color" do
|
162
|
-
class Color <
|
173
|
+
class Color < TestActiveRecord
|
174
|
+
attr_accessor :color
|
163
175
|
validates_formatting_of :color, :using => :hex_color
|
164
176
|
end
|
165
177
|
it "validates that the hex color value provided is valid" do
|
@@ -174,18 +186,22 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
174
186
|
Color.new(:color => "sdfsdfsf").should_not be_valid
|
175
187
|
end
|
176
188
|
end
|
177
|
-
|
189
|
+
|
178
190
|
describe "validation options" do
|
179
|
-
class Phony <
|
180
|
-
|
191
|
+
class Phony < TestActiveRecord
|
192
|
+
attr_accessor :phone, :phone2
|
193
|
+
validates_formatting_of :phone, :using => :us_phone, :on => :create
|
194
|
+
validates_formatting_of :phone2, :using => :us_phone, :on => :update
|
181
195
|
end
|
182
196
|
it "validates the phone formatting only on creation" do
|
183
|
-
option = Phony.
|
197
|
+
option = Phony.new(:phone => "(123) 234-4567")
|
198
|
+
option.should be_valid
|
184
199
|
option.phone = "123123123"
|
185
200
|
option.should be_valid
|
186
201
|
end
|
187
|
-
|
188
|
-
class Iffy <
|
202
|
+
|
203
|
+
class Iffy < TestActiveRecord
|
204
|
+
attr_accessor :name, :phone
|
189
205
|
validates_presence_of :name
|
190
206
|
validates_formatting_of :phone, :using => :us_phone, :if => lambda { |iffy| iffy.name == "Matthew" }
|
191
207
|
end
|
@@ -193,8 +209,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
193
209
|
Iffy.new(:phone => "(123 345-4567", :name => "Bill").should be_valid
|
194
210
|
Iffy.new(:phone => "(123 345-4567", :name => "Matthew").should_not be_valid
|
195
211
|
end
|
196
|
-
|
197
|
-
class Unlessy <
|
212
|
+
|
213
|
+
class Unlessy < TestActiveRecord
|
214
|
+
attr_accessor :name, :phone
|
198
215
|
validates_presence_of :name
|
199
216
|
validates_formatting_of :phone, :using => :us_phone, :unless => lambda { |unlessy| unlessy.name == "Matthew" }
|
200
217
|
end
|
@@ -204,7 +221,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
204
221
|
end
|
205
222
|
end
|
206
223
|
describe "dollars" do
|
207
|
-
class Money <
|
224
|
+
class Money < TestActiveRecord
|
225
|
+
attr_accessor :amount
|
208
226
|
validates_formatting_of :amount, :using => :dollars
|
209
227
|
end
|
210
228
|
it "validates that the dollars amount provided is valid" do
|
@@ -220,7 +238,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
220
238
|
end
|
221
239
|
end
|
222
240
|
describe "custom messages" do
|
223
|
-
class Message <
|
241
|
+
class Message < TestActiveRecord
|
242
|
+
attr_accessor :first_name
|
224
243
|
validates_formatting_of :first_name, :using => :alpha, :message => "is not a valid first name"
|
225
244
|
end
|
226
245
|
it "are allowed and can be used in displaying error messages" do
|
@@ -232,7 +251,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
232
251
|
end
|
233
252
|
|
234
253
|
describe "default error messages" do
|
235
|
-
class Problems <
|
254
|
+
class Problems < TestActiveRecord
|
255
|
+
attr_accessor :name
|
236
256
|
validates_formatting_of :name, :using => :alpha
|
237
257
|
end
|
238
258
|
it "set a default error" do
|
@@ -245,24 +265,24 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
245
265
|
end
|
246
266
|
end
|
247
267
|
|
248
|
-
=begin
|
249
|
-
# Currently, SuperModel's validations do not detect allow_blank or allow_nil
|
250
|
-
# This functionality has been tested separately in an empty Rails app with perfect
|
251
|
-
# results.
|
252
|
-
|
253
268
|
describe "nil and blank values" do
|
254
|
-
class
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
269
|
+
class PeopleTest < TestActiveRecord
|
270
|
+
attr_accessor :email, :email2, :email3
|
271
|
+
validates_formatting_of :email, :allow_nil => true
|
272
|
+
validates_formatting_of :email2, :using => :email, :allow_blank => true
|
273
|
+
validates_formatting_of :email3, :using => :email
|
274
|
+
end
|
275
|
+
let(:people) { PeopleTest.new(:email => "mbridges.91@gmail.com", :email2 => "mbridges.91@gmail.com", :email3 => "mbridges.91@gmail.com") }
|
276
|
+
it "should test nil and blank values correctly" do
|
277
|
+
people.email = nil
|
278
|
+
people.should be_valid
|
279
|
+
people.email = "mbridges.91@gmail.com"
|
280
|
+
people.email2 = ""
|
281
|
+
people.should be_valid
|
282
|
+
people.email2 = "mbridges.91@gmail.com"
|
283
|
+
people.email3 = nil
|
284
|
+
people.should_not be_valid
|
264
285
|
end
|
265
286
|
end
|
266
|
-
=end
|
267
287
|
|
268
288
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'validates_formatting_of/validation_addition'
|
2
|
+
|
3
|
+
module ValidatesFormattingOf
|
4
|
+
|
5
|
+
class TestAdding
|
6
|
+
extend ValidationAddition
|
7
|
+
end
|
8
|
+
|
9
|
+
describe ValidationAddition do
|
10
|
+
before do
|
11
|
+
TestAdding.add :email, /email/i
|
12
|
+
end
|
13
|
+
it "should be able to add new validations" do
|
14
|
+
TestAdding.add :another, /another/i
|
15
|
+
TestAdding.validations.count.should == 2
|
16
|
+
TestAdding.validations[:email].should be_instance_of Validation
|
17
|
+
TestAdding.validations[:another].should be_instance_of Validation
|
18
|
+
end
|
19
|
+
it "should be able to smartly determine the method to use" do
|
20
|
+
validation = TestAdding.find(:email)
|
21
|
+
validation.name.should == :email
|
22
|
+
validation.regex.should == /email/i
|
23
|
+
validation = TestAdding.find(:non_existent_validation, :using => :email)
|
24
|
+
validation.name.should == :email
|
25
|
+
validation.regex.should == /email/i
|
26
|
+
end
|
27
|
+
it "should raise an error if the method does not exist" do
|
28
|
+
expect { TestAdding.find(:fake) }.to raise_error MissingValidation
|
29
|
+
end
|
30
|
+
it "should be able to determine if the method exists" do
|
31
|
+
TestAdding.exists?(:email).should be_true
|
32
|
+
TestAdding.exists?(:non_existent).should be_false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'validates_formatting_of/validation'
|
2
|
+
|
3
|
+
module ValidatesFormattingOf
|
4
|
+
|
5
|
+
describe Validation do
|
6
|
+
context "valid validation creation" do
|
7
|
+
let(:validation) { Validation.new(:name, /something/i, "is an invalid value") }
|
8
|
+
it "should receive the right parameters" do
|
9
|
+
validation.name.should == :name
|
10
|
+
validation.regex.should == %r{something}i
|
11
|
+
validation.message.should == 'is an invalid value'
|
12
|
+
end
|
13
|
+
it "should print properly" do
|
14
|
+
validation.to_s.should == "<Validation::name>"
|
15
|
+
end
|
16
|
+
it "should be inspected properly" do
|
17
|
+
validation.inspect.should =~ /Validation/
|
18
|
+
validation.inspect.should =~ /\/something\/i/
|
19
|
+
validation.inspect.should =~ /\:name/
|
20
|
+
end
|
21
|
+
end
|
22
|
+
context "invalid validation creation" do
|
23
|
+
it "should raise an error if the specified regex is not a Regexp objct" do
|
24
|
+
expect { Validation.new(:name, 123, "is an invalid value") }.to raise_error InvalidRegularExpression
|
25
|
+
end
|
26
|
+
it "should not raise an error if the specified regex is a proc or a lambda" do
|
27
|
+
expect { Validation.new(:name, lambda { "my record" }, "is an invalid value") }.not_to raise_error InvalidRegularExpression
|
28
|
+
expect { Validation.new(:name, proc { "my record" }, "is an invalid value") }.not_to raise_error InvalidRegularExpression
|
29
|
+
end
|
30
|
+
it "should not raise an error if the regex if valid" do
|
31
|
+
expect { Validation.new(:name, /something/i, "is an invalid value") }.not_to raise_error InvalidRegularExpression
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
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.6.
|
4
|
+
version: 0.6.9
|
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-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -60,7 +60,7 @@ dependencies:
|
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
|
-
name:
|
63
|
+
name: simplecov
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
65
65
|
none: false
|
66
66
|
requirements:
|
@@ -91,13 +91,16 @@ files:
|
|
91
91
|
- README.markdown
|
92
92
|
- Rakefile
|
93
93
|
- lib/validates_formatting_of.rb
|
94
|
+
- lib/validates_formatting_of/method.rb
|
94
95
|
- lib/validates_formatting_of/model_additions.rb
|
95
96
|
- lib/validates_formatting_of/railtie.rb
|
96
|
-
- lib/validates_formatting_of/
|
97
|
-
- lib/validates_formatting_of/
|
97
|
+
- lib/validates_formatting_of/validation.rb
|
98
|
+
- lib/validates_formatting_of/validation_addition.rb
|
98
99
|
- lib/validates_formatting_of/version.rb
|
99
100
|
- spec/spec_helper.rb
|
100
101
|
- spec/validates_formatting_of/model_additions_spec.rb
|
102
|
+
- spec/validates_formatting_of/validation_addition_spec.rb
|
103
|
+
- spec/validates_formatting_of/validation_spec.rb
|
101
104
|
- validates_formatting_of.gemspec
|
102
105
|
homepage: https://github.com/mattdbridges/validates_formatting_of
|
103
106
|
licenses: []
|
@@ -127,3 +130,5 @@ summary: Adds several convenient methods to validate things such as emails, urls
|
|
127
130
|
test_files:
|
128
131
|
- spec/spec_helper.rb
|
129
132
|
- spec/validates_formatting_of/model_additions_spec.rb
|
133
|
+
- spec/validates_formatting_of/validation_addition_spec.rb
|
134
|
+
- spec/validates_formatting_of/validation_spec.rb
|
@@ -1,82 +0,0 @@
|
|
1
|
-
module ValidatesFormattingOf
|
2
|
-
class ValidatingMethods
|
3
|
-
class << self
|
4
|
-
|
5
|
-
# This method is very close to allowing what is specified in RFC 5322 and RFC 5321
|
6
|
-
def email
|
7
|
-
%r{\A([^@\s]+)@((?:(?!-)[-a-z0-9]+(?<!-)\.)+[a-z]{2,})\Z}i
|
8
|
-
end
|
9
|
-
|
10
|
-
# Thie version of the email exists to support common validation libraries (such
|
11
|
-
# as client_side_validations) that require access to Rails' validation. Look-behinds
|
12
|
-
# are not supported in javascript.
|
13
|
-
def simple_email
|
14
|
-
%r{\A([^@\s]+)@((?:(?!-)[-a-z0-9]+(?<!-)\.)+[a-z]{2,})\Z}i
|
15
|
-
end
|
16
|
-
|
17
|
-
# Taken from Ryan Bates' screencast on extracting gems. Works extremely well. Thanks Ryan!
|
18
|
-
# (slightly revised to work on MRI 1.8.7 and ree)
|
19
|
-
def url
|
20
|
-
%r{\Ahttps?:\/\/([^\s:@]+:[^\s:@]*@)?[A-Za-z\d\-]+(\.[A-Za-z\d\-]+)+\.?(:\d{1,5})?([\/?]\S*)?\Z}i
|
21
|
-
end
|
22
|
-
|
23
|
-
# No numbers of symbols. allows "-"
|
24
|
-
def alpha
|
25
|
-
%r{\A([^\d\W]|[-])*\Z}
|
26
|
-
end
|
27
|
-
|
28
|
-
# Letters, numbers, and spaces
|
29
|
-
def alphanum
|
30
|
-
%r{\A[A-Z0-9\s]+\Z}i
|
31
|
-
end
|
32
|
-
|
33
|
-
# Visa, Mastercard, Discver, and American Express
|
34
|
-
def credit_card
|
35
|
-
%r{\A((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}\Z}
|
36
|
-
end
|
37
|
-
|
38
|
-
# US Zip code. ##### or #####-####
|
39
|
-
def us_zip
|
40
|
-
%r{\A\d{5}(-\d{4})?\Z}
|
41
|
-
end
|
42
|
-
|
43
|
-
# US Phone numbers.
|
44
|
-
# Examples of valid formats:
|
45
|
-
# * ###.###.####
|
46
|
-
# * ###-###-####
|
47
|
-
# * (###) ###-####
|
48
|
-
# * (###)###-####
|
49
|
-
# * #########
|
50
|
-
# * ### ###-####
|
51
|
-
def us_phone
|
52
|
-
%r{\A(\((\d{3})\)|\d{3})[ |\.|\-]?(\d{3})[ |\.|\-]?(\d{4})\Z}
|
53
|
-
end
|
54
|
-
|
55
|
-
# IP Address validation
|
56
|
-
def ip_address
|
57
|
-
warn "[DEPRECATION] The :ip_address validation for `valdiates_formatting_of` is DEPRECATED. Please update your model validations to use :ip_address_v4. This method will be removed by version 0.7.0."
|
58
|
-
ip_address_v4
|
59
|
-
end
|
60
|
-
|
61
|
-
def ip_address_v4
|
62
|
-
%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}
|
63
|
-
end
|
64
|
-
|
65
|
-
# Social Security Number pattern
|
66
|
-
def ssn
|
67
|
-
%r{\A\d{3}([-.]){1}\d{2}([-.]){1}\d{4}\Z}
|
68
|
-
end
|
69
|
-
|
70
|
-
# Matches CSS colors either in three or six digit formats
|
71
|
-
def hex_color
|
72
|
-
%r{\A([A-F0-9]{6}|[A-F0-9]{3})\Z}i
|
73
|
-
end
|
74
|
-
|
75
|
-
# Ensures that what value is passed is a dollar amount of some kind
|
76
|
-
def dollars
|
77
|
-
%r{\A\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\Z}
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module ValidatesFormattingOf
|
2
|
-
class ValidationMessages
|
3
|
-
|
4
|
-
Messages = {
|
5
|
-
:email => "is not a valid email",
|
6
|
-
:simple_email => "is not a valid email",
|
7
|
-
:url => "is not a valid URL",
|
8
|
-
:alpha => "must be only letters or dashes",
|
9
|
-
:alphanum => "must be letters, numbers",
|
10
|
-
:credit_card => "is not a valid credit card number",
|
11
|
-
:us_zip => "is not a valid zipcode",
|
12
|
-
:us_phone => "is not a valid phone number",
|
13
|
-
:ip_address => "is not a valid IP address",
|
14
|
-
:ssn => "is not a valid social security number",
|
15
|
-
:hex_color => "is not a valid hex color",
|
16
|
-
:dollars => "is not a valid dollar amount"
|
17
|
-
}
|
18
|
-
|
19
|
-
def self.message(message)
|
20
|
-
Messages.fetch(message.to_s.downcase.to_sym, "is not valid")
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
end
|