validates_formatting_of 0.7.2 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +11 -1
- data/README.markdown +2 -3
- data/lib/validates_formatting_of.rb +2 -1
- data/lib/validates_formatting_of/railtie.rb +7 -1
- data/lib/validates_formatting_of/validator.rb +26 -0
- data/lib/validates_formatting_of/version.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/validates_formatting_of/validator_spec.rb +284 -0
- metadata +5 -2
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,16 @@
|
|
1
|
+
## v0.8.0
|
2
|
+
|
3
|
+
* Added Rails 'sexy' validation. *hbakhiyor*
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
class User < ActiveRecord::Base
|
7
|
+
validates :email, :email => true
|
8
|
+
end
|
9
|
+
```
|
10
|
+
|
1
11
|
## v0.7.2
|
2
12
|
|
3
|
-
* Bugfix: Changed \Z to \z anchor and added some test assertions. Not valid with newline in some formats
|
13
|
+
* Bugfix: Changed \Z to \z anchor and added some test assertions. Not valid with newline in some formats *hbakhiyor*
|
4
14
|
|
5
15
|
## v0.7.1
|
6
16
|
|
data/README.markdown
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
# Validates Formatting Of
|
2
|
-
|
3
1
|
[![Build Status](https://secure.travis-ci.org/mattdbridges/validates_formatting_of.png)](http://travis-ci.org/mattdbridges/validates_formatting_of)
|
4
2
|
[![Dependency Status](https://gemnasium.com/mattdbridges/validates_formatting_of.png?travis)](https://gemnasium.com/mattdbridges/validates_formatting_of)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/mattdbridges/validates_formatting_of.png)](https://codeclimate.com/github/mattdbridges/validates_formatting_of)
|
5
4
|
|
6
|
-
|
5
|
+
# Validates Formatting Of
|
7
6
|
|
8
7
|
The `validates_formatting_of` gem adds several convenient methods to validate things such as emails, urls, and phone numbers in a Rails application.
|
9
8
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require "validates_formatting_of/version"
|
2
2
|
require "validates_formatting_of/method"
|
3
|
+
require "validates_formatting_of/validator"
|
3
4
|
require "validates_formatting_of/model_additions"
|
4
|
-
require "validates_formatting_of/railtie" if defined? Rails
|
5
|
+
require "validates_formatting_of/railtie" if defined? Rails
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_support/core_ext/hash/reverse_merge'
|
2
|
+
require 'active_model/validations'
|
3
|
+
|
4
|
+
module ValidatesFormattingOf
|
5
|
+
module Validations
|
6
|
+
class Validator < ActiveModel::Validations::FormatValidator
|
7
|
+
def initialize(options = {}, format = nil)
|
8
|
+
return if format.blank?
|
9
|
+
|
10
|
+
validation = Method.find(nil, :using => format)
|
11
|
+
options.reverse_merge!(:with => validation.regex, :message => validation.message)
|
12
|
+
super options
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Method.validations.each do |key, value|
|
17
|
+
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
18
|
+
class #{key.to_s.camelize}Validator < Validator
|
19
|
+
def initialize(options = {})
|
20
|
+
super options, #{key.to_sym.inspect}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
RUBY_EVAL
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,284 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesFormattingOf::Validations do
|
4
|
+
|
5
|
+
describe "with 'sexy' validation style" do
|
6
|
+
describe "email" do
|
7
|
+
class Email < TestActiveRecord
|
8
|
+
attr_accessor :email
|
9
|
+
validates :email, :email => true
|
10
|
+
end
|
11
|
+
|
12
|
+
it "validates that the email provided is valid" do
|
13
|
+
Email.new(:email => "example@example.com").should be_valid
|
14
|
+
Email.new(:email => "badexample.com").should_not be_valid
|
15
|
+
Email.new(:email => "mbridges.91@gmail.com").should be_valid
|
16
|
+
Email.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").should be_valid
|
17
|
+
Email.new(:email => "this__???{}|__should@be-valid.com").should be_valid
|
18
|
+
Email.new(:email => "visitorservices@vmfa.museum").should be_valid
|
19
|
+
Email.new(:email => "info@samoa.travel").should be_valid
|
20
|
+
Email.new(:email => "info@-samoa.travel").should_not be_valid
|
21
|
+
Email.new(:email => "info@samoa-.travel").should_not be_valid
|
22
|
+
Email.new(:email => "info@123-samoa.travel").should be_valid
|
23
|
+
Email.new(:email => "info@123-samoa.travel\n").should_not be_valid
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "simple email for 1.8.7 and javascript validations (such as with client_side_validations)" do
|
28
|
+
class SimpleEmail < TestActiveRecord
|
29
|
+
attr_accessor :email
|
30
|
+
validates :email, :simple_email => true
|
31
|
+
end
|
32
|
+
it "validates that the email provided is valid" do
|
33
|
+
SimpleEmail.new(:email => "example@example.com").should be_valid
|
34
|
+
SimpleEmail.new(:email => "badexample.com").should_not be_valid
|
35
|
+
SimpleEmail.new(:email => "mbridges.91@gmail.com").should be_valid
|
36
|
+
SimpleEmail.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").should be_valid
|
37
|
+
SimpleEmail.new(:email => "this__???{}|__should@be-valid.com").should be_valid
|
38
|
+
SimpleEmail.new(:email => "visitorservices@vmfa.museum").should be_valid
|
39
|
+
SimpleEmail.new(:email => "info@samoa.travel").should be_valid
|
40
|
+
SimpleEmail.new(:email => "info@-samoa.travel").should be_valid
|
41
|
+
SimpleEmail.new(:email => "info@samoa-.travel").should be_valid
|
42
|
+
SimpleEmail.new(:email => "info@123-samoa.travel").should be_valid
|
43
|
+
SimpleEmail.new(:email => "info@123-samoa.travel\n").should_not be_valid
|
44
|
+
end
|
45
|
+
end
|
46
|
+
describe "url" do
|
47
|
+
class Webpage < TestActiveRecord
|
48
|
+
attr_accessor :url
|
49
|
+
validates :url, :url => true
|
50
|
+
end
|
51
|
+
it "validates that the url provided is valid" do
|
52
|
+
Webpage.new(:url => 'http://something.com').should be_valid
|
53
|
+
Webpage.new(:url => 'http://something-else.com').should be_valid
|
54
|
+
Webpage.new(:url => 'http://sub.domains.something-else.com').should be_valid
|
55
|
+
Webpage.new(:url => 'http://username:password@something-else.com').should be_valid
|
56
|
+
Webpage.new(:url => "http://username:password@something-else.com\n").should_not be_valid
|
57
|
+
Webpage.new(:url => "something else").should_not be_valid
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "us_zip" do
|
62
|
+
class USZip < TestActiveRecord
|
63
|
+
attr_accessor :zipcode
|
64
|
+
validates :zipcode, :us_zip => true
|
65
|
+
end
|
66
|
+
it "validates that the zipcode provided is valid" do
|
67
|
+
USZip.new(:zipcode => '92348').should be_valid
|
68
|
+
USZip.new(:zipcode => '23434-2348').should be_valid
|
69
|
+
USZip.new(:zipcode => "23434-2348\n").should_not be_valid
|
70
|
+
USZip.new(:zipcode => '234').should_not be_valid
|
71
|
+
USZip.new(:zipcode => '23408234').should_not be_valid
|
72
|
+
USZip.new(:zipcode => 'invalid').should_not be_valid
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "alpha" do
|
77
|
+
class Alpha < TestActiveRecord
|
78
|
+
attr_accessor :alpha
|
79
|
+
validates :alpha, :alpha => true
|
80
|
+
end
|
81
|
+
it "validates that the letters provided is valid" do
|
82
|
+
Alpha.new(:alpha => 'abscdsofjsdpfahdsofkajlsdfaspdhjfads').should be_valid
|
83
|
+
Alpha.new(:alpha => 'asdfalskdfjhas-dlfhasdksdfaldhfadsfasdfa').should be_valid
|
84
|
+
Alpha.new(:alpha => 'adsufasodfksadjfskjdfha98').should_not be_valid
|
85
|
+
Alpha.new(:alpha => 'asdf ausdpf98hasdfo alsdf ja8 sd').should_not be_valid
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "alphanum" do
|
90
|
+
class Alphanum < TestActiveRecord
|
91
|
+
attr_accessor :letters_and_numbers
|
92
|
+
validates :letters_and_numbers, :alphanum => true
|
93
|
+
end
|
94
|
+
it "validates that the letters provided is valid" do
|
95
|
+
Alphanum.new(:letters_and_numbers => 'numbersandlettersarevalid1234567890').should be_valid
|
96
|
+
Alphanum.new(:letters_and_numbers => 'justletters').should be_valid
|
97
|
+
Alphanum.new(:letters_and_numbers => 'letters and numbers 123 with spaces').should be_valid
|
98
|
+
Alphanum.new(:letters_and_numbers => 'adding ; some special ** chars').should_not be_valid
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "us_phone" do
|
103
|
+
class USPhone < TestActiveRecord
|
104
|
+
attr_accessor :phone_number
|
105
|
+
validates :phone_number, :us_phone => true
|
106
|
+
end
|
107
|
+
it "validates that the phone number provided is valid" do
|
108
|
+
USPhone.new(:phone_number => '(234) 234-3456').should be_valid
|
109
|
+
USPhone.new(:phone_number => '123 123 3456').should be_valid
|
110
|
+
USPhone.new(:phone_number => '1231233456').should be_valid
|
111
|
+
USPhone.new(:phone_number => '123.123.3456').should be_valid
|
112
|
+
USPhone.new(:phone_number => '(223)123-2347').should be_valid
|
113
|
+
USPhone.new(:phone_number => "(223)123-2347\n").should_not be_valid
|
114
|
+
USPhone.new(:phone_number => '(223 123-2347').should_not be_valid
|
115
|
+
USPhone.new(:phone_number => '12349870238').should_not be_valid
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "ip_address_v4" do
|
120
|
+
class IPAddress < TestActiveRecord
|
121
|
+
attr_accessor :ipv4
|
122
|
+
validates :ipv4, :ip_address_v4 => true
|
123
|
+
end
|
124
|
+
it "validates that the IP address provided is valid" do
|
125
|
+
IPAddress.new(:ipv4 => '10.10.10').should_not be_valid
|
126
|
+
IPAddress.new(:ipv4 => '999.10.10.20').should_not be_valid
|
127
|
+
IPAddress.new(:ipv4 => '2222.22.22.22').should_not be_valid
|
128
|
+
IPAddress.new(:ipv4 => '22.2222.22.2').should_not be_valid
|
129
|
+
IPAddress.new(:ipv4 => '127.0.0.1').should be_valid
|
130
|
+
IPAddress.new(:ipv4 => '132.254.111.10').should be_valid
|
131
|
+
IPAddress.new(:ipv4 => "132.254.111.10\n").should_not be_valid
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# For clarification, NONE of the following numbers are real credit card numbers.
|
136
|
+
# They only match the pattern. These were randomly made for testing.
|
137
|
+
describe "credit_card" do
|
138
|
+
class Client < TestActiveRecord
|
139
|
+
attr_accessor :cc
|
140
|
+
validates :cc, :credit_card => true
|
141
|
+
end
|
142
|
+
it "validates that the credit card number provided is valid" do
|
143
|
+
Client.new(:cc => '4264-2879-1230-0000').should be_valid # Visa style
|
144
|
+
Client.new(:cc => '6011-1111-0000-2391').should be_valid # Discover style
|
145
|
+
Client.new(:cc => '5422434400828888').should be_valid # Mastercard style
|
146
|
+
Client.new(:cc => "5422434400828889\n").should_not be_valid # Mastercard style
|
147
|
+
Client.new(:cc => '1233444444444444').should_not be_valid # fake
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
describe "ssn" do
|
152
|
+
class AnotherPerson < TestActiveRecord
|
153
|
+
attr_accessor :ssn
|
154
|
+
validates :ssn, :ssn => true
|
155
|
+
end
|
156
|
+
it "validates that the social security number provided is valid" do
|
157
|
+
AnotherPerson.new(:ssn => "145.47.0191").should be_valid
|
158
|
+
AnotherPerson.new(:ssn => "223-43-2343").should be_valid
|
159
|
+
AnotherPerson.new(:ssn => "999.55.8888").should be_valid
|
160
|
+
AnotherPerson.new(:ssn => "999.55.8888\n").should_not be_valid
|
161
|
+
AnotherPerson.new(:ssn => "28934").should_not be_valid
|
162
|
+
AnotherPerson.new(:ssn => "228934828934934").should_not be_valid
|
163
|
+
AnotherPerson.new(:ssn => "23498.7234").should_not be_valid
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
describe "hex_color" do
|
168
|
+
class Color < TestActiveRecord
|
169
|
+
attr_accessor :color
|
170
|
+
validates :color, :hex_color => true
|
171
|
+
end
|
172
|
+
it "validates that the hex color value provided is valid" do
|
173
|
+
Color.new(:color => "efefef").should be_valid
|
174
|
+
Color.new(:color => "98de89").should be_valid
|
175
|
+
Color.new(:color => "000011").should be_valid
|
176
|
+
Color.new(:color => "132").should be_valid
|
177
|
+
Color.new(:color => "eef").should be_valid
|
178
|
+
Color.new(:color => "eef\n").should_not be_valid
|
179
|
+
Color.new(:color => "efefe").should_not be_valid
|
180
|
+
Color.new(:color => "zsdfsd").should_not be_valid
|
181
|
+
Color.new(:color => "p98hul;").should_not be_valid
|
182
|
+
Color.new(:color => "sdfsdfsf").should_not be_valid
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe "validation options" do
|
187
|
+
class Phony < TestActiveRecord
|
188
|
+
attr_accessor :phone, :phone2
|
189
|
+
validates :phone, :us_phone => true, :on => :create
|
190
|
+
validates :phone2, :us_phone => true, :on => :update
|
191
|
+
end
|
192
|
+
it "validates the phone formatting only on creation" do
|
193
|
+
option = Phony.new(:phone => "(123) 234-4567")
|
194
|
+
option.should be_valid
|
195
|
+
option.phone = "123123123"
|
196
|
+
option.should be_valid
|
197
|
+
end
|
198
|
+
|
199
|
+
class Iffy < TestActiveRecord
|
200
|
+
attr_accessor :name, :phone
|
201
|
+
validates_presence_of :name
|
202
|
+
validates :phone, :us_phone => true, :if => lambda { |iffy| iffy.name == "Matthew" }
|
203
|
+
end
|
204
|
+
it "validates the phone formatting only if a name is specified" do
|
205
|
+
Iffy.new(:phone => "(123 345-4567", :name => "Bill").should be_valid
|
206
|
+
Iffy.new(:phone => "(123 345-4567", :name => "Matthew").should_not be_valid
|
207
|
+
end
|
208
|
+
|
209
|
+
class Unlessy < TestActiveRecord
|
210
|
+
attr_accessor :name, :phone
|
211
|
+
validates_presence_of :name
|
212
|
+
validates :phone, :us_phone => true, :unless => lambda { |unlessy| unlessy.name == "Matthew" }
|
213
|
+
end
|
214
|
+
it "validates the phone formatting only if a name is specified" do
|
215
|
+
Unlessy.new(:phone => "(123 345-4567", :name => "Bill").should_not be_valid
|
216
|
+
Unlessy.new(:phone => "(123 345-4567", :name => "Matthew").should be_valid
|
217
|
+
end
|
218
|
+
end
|
219
|
+
describe "dollars" do
|
220
|
+
class Money < TestActiveRecord
|
221
|
+
attr_accessor :amount
|
222
|
+
validates :amount, :dollars => true
|
223
|
+
end
|
224
|
+
it "validates that the dollars amount provided is valid" do
|
225
|
+
Money.new(:amount => "$100.00").should be_valid
|
226
|
+
Money.new(:amount => "100.00").should be_valid
|
227
|
+
Money.new(:amount => "12,234,343").should be_valid
|
228
|
+
Money.new(:amount => "$12.34").should be_valid
|
229
|
+
Money.new(:amount => "120,123,232.32").should be_valid
|
230
|
+
Money.new(:amount => "$$1111111100").should_not be_valid
|
231
|
+
Money.new(:amount => "100;00").should_not be_valid
|
232
|
+
Money.new(:amount => "238,3423,42..99").should_not be_valid
|
233
|
+
Money.new(:amount => "$-233").should_not be_valid
|
234
|
+
end
|
235
|
+
end
|
236
|
+
describe "custom messages" do
|
237
|
+
class Message < TestActiveRecord
|
238
|
+
attr_accessor :first_name
|
239
|
+
validates :first_name, :alpha => { :message => "is not a valid first name" }
|
240
|
+
end
|
241
|
+
it "are allowed and can be used in displaying error messages" do
|
242
|
+
message = Message.new(:first_name => "invalid-first-name-123")
|
243
|
+
message.should_not be_valid
|
244
|
+
message.errors.keys.class.should eq Array
|
245
|
+
message.errors.full_messages.first.should =~ /is not a valid first name/
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
describe "default error messages" do
|
250
|
+
class Problems < TestActiveRecord
|
251
|
+
attr_accessor :name
|
252
|
+
validates :name, :alpha => true
|
253
|
+
end
|
254
|
+
it "set a default error" do
|
255
|
+
problems = Problems.new(:name => "sdfs12312dfsd")
|
256
|
+
problems.should_not be_valid
|
257
|
+
problems.errors.full_messages.first.should =~ /letters/i
|
258
|
+
email = Email.new(:email => "not.an.email.address")
|
259
|
+
email.should_not be_valid
|
260
|
+
email.errors.full_messages.first.should =~ /email/i
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
describe "nil and blank values" do
|
265
|
+
class PeopleTest < TestActiveRecord
|
266
|
+
attr_accessor :email, :email2, :email3
|
267
|
+
validates :email, :email => true, :allow_nil => true
|
268
|
+
validates :email2, :email => true, :allow_blank => true
|
269
|
+
validates :email3, :email => true
|
270
|
+
end
|
271
|
+
let(:people) { PeopleTest.new(:email => "mbridges.91@gmail.com", :email2 => "mbridges.91@gmail.com", :email3 => "mbridges.91@gmail.com") }
|
272
|
+
it "should test nil and blank values correctly" do
|
273
|
+
people.email = nil
|
274
|
+
people.should be_valid
|
275
|
+
people.email = "mbridges.91@gmail.com"
|
276
|
+
people.email2 = ""
|
277
|
+
people.should be_valid
|
278
|
+
people.email2 = "mbridges.91@gmail.com"
|
279
|
+
people.email3 = nil
|
280
|
+
people.should_not be_valid
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
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.8.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: 2013-03-
|
12
|
+
date: 2013-03-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -96,11 +96,13 @@ files:
|
|
96
96
|
- lib/validates_formatting_of/railtie.rb
|
97
97
|
- lib/validates_formatting_of/validation.rb
|
98
98
|
- lib/validates_formatting_of/validation_addition.rb
|
99
|
+
- lib/validates_formatting_of/validator.rb
|
99
100
|
- lib/validates_formatting_of/version.rb
|
100
101
|
- spec/spec_helper.rb
|
101
102
|
- spec/validates_formatting_of/model_additions_spec.rb
|
102
103
|
- spec/validates_formatting_of/validation_addition_spec.rb
|
103
104
|
- spec/validates_formatting_of/validation_spec.rb
|
105
|
+
- spec/validates_formatting_of/validator_spec.rb
|
104
106
|
- validates_formatting_of.gemspec
|
105
107
|
homepage: https://github.com/mattdbridges/validates_formatting_of
|
106
108
|
licenses: []
|
@@ -132,3 +134,4 @@ test_files:
|
|
132
134
|
- spec/validates_formatting_of/model_additions_spec.rb
|
133
135
|
- spec/validates_formatting_of/validation_addition_spec.rb
|
134
136
|
- spec/validates_formatting_of/validation_spec.rb
|
137
|
+
- spec/validates_formatting_of/validator_spec.rb
|