validates_formatting_of 0.8.1 → 0.9.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.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +2 -1
- data/Gemfile +1 -1
- data/README.markdown +27 -89
- data/lib/validates_formatting_of/method.rb +1 -1
- data/lib/validates_formatting_of/version.rb +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/validates_formatting_of/model_additions_spec.rb +105 -105
- data/spec/validates_formatting_of/validation_addition_spec.rb +14 -13
- data/spec/validates_formatting_of/validation_spec.rb +9 -8
- data/spec/validates_formatting_of/validator_spec.rb +105 -105
- data/validates_formatting_of.gemspec +2 -1
- metadata +32 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b287c47de58a743ad0e3bbcd926418f177d8a612
|
4
|
+
data.tar.gz: edab15de4ab01ee66ca38c98a18e22f87a0a4ca3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae394a30bb518a12b6f3ac1e3a72ef0cd5638d85cdd0e569b18c7f77e0c533235df5bb6766921be4d30de34b6b1390247548aaf065ef0fb0ea1bc005b6b024c0
|
7
|
+
data.tar.gz: 716d98e86d5a2782f180a571ef4a772f015d07cebbed209854edf7f7b8c2f9cbd61b9c7d579861252ff8a9ea2c82687b0e07c08c7d78918c39ef15b3d61b5a45
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -6,12 +6,12 @@
|
|
6
6
|
|
7
7
|
The `validates_formatting_of` gem adds several convenient methods to validate things such as emails, urls, and phone numbers in a Rails application.
|
8
8
|
|
9
|
-
#
|
9
|
+
# Supported Ruby Versions
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
* 1.
|
14
|
-
*
|
11
|
+
* 1.9.3
|
12
|
+
* 2.0.0
|
13
|
+
* 2.1.0
|
14
|
+
* 2.2.0
|
15
15
|
|
16
16
|
# Installation
|
17
17
|
|
@@ -61,7 +61,7 @@ Say, for example, you have identical plain-old regex validations for different c
|
|
61
61
|
While very unrealistic, these examples should serve their purpose in demonstrating this ability.
|
62
62
|
|
63
63
|
```ruby
|
64
|
-
# config/initializers/
|
64
|
+
# config/initializers/validates_formatting_of.rb
|
65
65
|
ValidatesFormattingOf::Method.add :loweralpha, /[a-z]/, "must be lowercase and no spaces"
|
66
66
|
ValidatesFormattingOf::Method.add :upperalpha, /[A-Z]/, "must be uppercase and no spaces"
|
67
67
|
ValidatesFormattingOf::Method.add :weak_password, /[a-zA-Z0-9]{8,}/, "must contain only letters and numbers and be at least 8 characters long".
|
@@ -71,20 +71,23 @@ Keep in mind, since this gem is only ActiveModel-dependent, you should be able t
|
|
71
71
|
|
72
72
|
# Built-in Validations
|
73
73
|
|
74
|
-
`validates_formatting_of` has the following
|
75
|
-
|
76
|
-
### Email
|
74
|
+
`validates_formatting_of` has the following built-in validations:
|
77
75
|
|
78
76
|
```ruby
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
77
|
+
class ExampleModel < ActiveRecord::Base
|
78
|
+
validates_formatting_of :email, :using => :email # Email
|
79
|
+
validates_formatting_of :first_name, :using => :alpha # Letters
|
80
|
+
validates_formatting_of :website, :using => :url # URLs
|
81
|
+
validates_formatting_of :text, :using => :alphanum # Alpha-numeric
|
82
|
+
validates_formatting_of :zipcode, :using => :us_zip # US Zip Code
|
83
|
+
validates_formatting_of :phone, :using => :us_phone # US Phone Numbers
|
84
|
+
validates_formatting_of :ip, :using => :ip_address_v4 # IPv4
|
85
|
+
validates_formatting_of :ssn, :using => :ssn # Social Security Numbers
|
86
|
+
validates_formatting_of :color, :using => :hex_color # Hexadecimal Colors
|
87
|
+
validates_formatting_of :amount, :using => :dollars # Dollars
|
86
88
|
|
87
|
-
|
89
|
+
# Credit Card (Visa, Mastercard, Discover, and American Express)
|
90
|
+
validates_formatting_of :cc, :using => :credit_card
|
88
91
|
end
|
89
92
|
```
|
90
93
|
|
@@ -96,83 +99,18 @@ class User < ActiveRecord::Base
|
|
96
99
|
end
|
97
100
|
```
|
98
101
|
|
99
|
-
###
|
100
|
-
|
101
|
-
```ruby
|
102
|
-
class Sites < ActiveRecord::Base
|
103
|
-
validates_formatting_of :website, :using => :url
|
104
|
-
end
|
105
|
-
```
|
106
|
-
|
107
|
-
### Alpha
|
108
|
-
|
109
|
-
```ruby
|
110
|
-
class Name < ActiveRecord::Base
|
111
|
-
validates_formatting_of :first_name, :using => :alpha
|
112
|
-
end
|
113
|
-
```
|
114
|
-
|
115
|
-
### Alphanumeric
|
116
|
-
|
117
|
-
```ruby
|
118
|
-
class Sites < ActiveRecord::Base
|
119
|
-
validates_formatting_of :text, :using => :alphanum
|
120
|
-
end
|
121
|
-
```
|
122
|
-
|
123
|
-
### Credit Card (Visa, Mastercard, Discover, and American Express)
|
124
|
-
|
125
|
-
```ruby
|
126
|
-
class Purchases < ActiveRecord::Base
|
127
|
-
validates_formatting_of :cc, :using => :credit_card
|
128
|
-
end
|
129
|
-
```
|
102
|
+
### Plain Old Ruby Objects
|
130
103
|
|
131
|
-
|
104
|
+
If you are not using ActiveRecord or want to use it on some other type of object, this is the least you need to get validations running.
|
132
105
|
|
133
106
|
```ruby
|
134
|
-
class
|
135
|
-
|
136
|
-
|
137
|
-
```
|
138
|
-
|
139
|
-
### US Phone numbers
|
140
|
-
|
141
|
-
```ruby
|
142
|
-
class Phones < ActiveRecord::Base
|
143
|
-
validates_formatting_of :phone, :using => :us_phone
|
144
|
-
end
|
145
|
-
```
|
146
|
-
|
147
|
-
### IPv4
|
148
|
-
|
149
|
-
```ruby
|
150
|
-
class Location < ActiveRecord::Base
|
151
|
-
validates_formatting_of :ip, :using => :ip_address_v4
|
152
|
-
end
|
153
|
-
```
|
154
|
-
|
155
|
-
### Social Security Number
|
156
|
-
|
157
|
-
```ruby
|
158
|
-
class User < ActiveRecord::Base
|
159
|
-
validates_formatting_of :ssn, :using => :ssn
|
160
|
-
end
|
161
|
-
```
|
162
|
-
|
163
|
-
### Hex Colors
|
164
|
-
|
165
|
-
```ruby
|
166
|
-
class Color < ActiveRecord::Base
|
167
|
-
validates_formatting_of :color, :using => :hex_color
|
168
|
-
end
|
169
|
-
```
|
107
|
+
class User
|
108
|
+
include ActiveModel::Validations
|
109
|
+
extend ValidatesFormattingOf::ModelAdditions
|
170
110
|
|
171
|
-
|
111
|
+
attr_accessor :email
|
172
112
|
|
173
|
-
|
174
|
-
class Invoice < ActiveRecord::Base
|
175
|
-
validates_formatting_of :amount, :using => :dollars
|
113
|
+
validates_formatting_of :email, :using => :email
|
176
114
|
end
|
177
115
|
```
|
178
116
|
|
@@ -18,7 +18,7 @@ module ValidatesFormattingOf
|
|
18
18
|
# (slightly revised to work on MRI 1.8.7 and ree)
|
19
19
|
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"
|
20
20
|
|
21
|
-
# No numbers
|
21
|
+
# No numbers or symbols. allows "-"
|
22
22
|
add :alpha, %r{\A([^\d\W]|[-])*\Z}, "must be only letters or dashes"
|
23
23
|
|
24
24
|
# Letters, numbers, and spaces
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ValidatesFormattingOf::ModelAdditions do
|
3
|
+
RSpec.describe ValidatesFormattingOf::ModelAdditions do
|
4
4
|
|
5
5
|
describe "email" do
|
6
6
|
class Email < TestActiveRecord
|
@@ -8,17 +8,17 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
8
8
|
validates_formatting_of :email
|
9
9
|
end
|
10
10
|
it "validates that the email provided is valid" do
|
11
|
-
Email.new(:email => "example@example.com").
|
12
|
-
Email.new(:email => "badexample.com").
|
13
|
-
Email.new(:email => "mbridges.91@gmail.com").
|
14
|
-
Email.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").
|
15
|
-
Email.new(:email => "this__???{}|__should@be-valid.com").
|
16
|
-
Email.new(:email => "visitorservices@vmfa.museum").
|
17
|
-
Email.new(:email => "info@samoa.travel").
|
18
|
-
Email.new(:email => "info@-samoa.travel").
|
19
|
-
Email.new(:email => "info@samoa-.travel").
|
20
|
-
Email.new(:email => "info@123-samoa.travel").
|
21
|
-
Email.new(:email => "info@123-samoa.travel\n").
|
11
|
+
expect(Email.new(:email => "example@example.com")).to be_valid
|
12
|
+
expect(Email.new(:email => "badexample.com")).not_to be_valid
|
13
|
+
expect(Email.new(:email => "mbridges.91@gmail.com")).to be_valid
|
14
|
+
expect(Email.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com")).to be_valid
|
15
|
+
expect(Email.new(:email => "this__???{}|__should@be-valid.com")).to be_valid
|
16
|
+
expect(Email.new(:email => "visitorservices@vmfa.museum")).to be_valid
|
17
|
+
expect(Email.new(:email => "info@samoa.travel")).to be_valid
|
18
|
+
expect(Email.new(:email => "info@-samoa.travel")).not_to be_valid
|
19
|
+
expect(Email.new(:email => "info@samoa-.travel")).not_to be_valid
|
20
|
+
expect(Email.new(:email => "info@123-samoa.travel")).to be_valid
|
21
|
+
expect(Email.new(:email => "info@123-samoa.travel\n")).not_to be_valid
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -28,17 +28,17 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
28
28
|
validates_formatting_of :email, :using => :simple_email
|
29
29
|
end
|
30
30
|
it "validates that the email provided is valid" do
|
31
|
-
SimpleEmail.new(:email => "example@example.com").
|
32
|
-
SimpleEmail.new(:email => "badexample.com").
|
33
|
-
SimpleEmail.new(:email => "mbridges.91@gmail.com").
|
34
|
-
SimpleEmail.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").
|
35
|
-
SimpleEmail.new(:email => "this__???{}|__should@be-valid.com").
|
36
|
-
SimpleEmail.new(:email => "visitorservices@vmfa.museum").
|
37
|
-
SimpleEmail.new(:email => "info@samoa.travel").
|
38
|
-
SimpleEmail.new(:email => "info@-samoa.travel").
|
39
|
-
SimpleEmail.new(:email => "info@samoa-.travel").
|
40
|
-
SimpleEmail.new(:email => "info@123-samoa.travel").
|
41
|
-
SimpleEmail.new(:email => "info@123-samoa.travel\n").
|
31
|
+
expect(SimpleEmail.new(:email => "example@example.com")).to be_valid
|
32
|
+
expect(SimpleEmail.new(:email => "badexample.com")).not_to be_valid
|
33
|
+
expect(SimpleEmail.new(:email => "mbridges.91@gmail.com")).to be_valid
|
34
|
+
expect(SimpleEmail.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com")).to be_valid
|
35
|
+
expect(SimpleEmail.new(:email => "this__???{}|__should@be-valid.com")).to be_valid
|
36
|
+
expect(SimpleEmail.new(:email => "visitorservices@vmfa.museum")).to be_valid
|
37
|
+
expect(SimpleEmail.new(:email => "info@samoa.travel")).to be_valid
|
38
|
+
expect(SimpleEmail.new(:email => "info@-samoa.travel")).to be_valid
|
39
|
+
expect(SimpleEmail.new(:email => "info@samoa-.travel")).to be_valid
|
40
|
+
expect(SimpleEmail.new(:email => "info@123-samoa.travel")).to be_valid
|
41
|
+
expect(SimpleEmail.new(:email => "info@123-samoa.travel\n")).not_to be_valid
|
42
42
|
end
|
43
43
|
end
|
44
44
|
describe "url" do
|
@@ -47,12 +47,12 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
47
47
|
validates_formatting_of :url
|
48
48
|
end
|
49
49
|
it "validates that the url provided is valid" do
|
50
|
-
Webpage.new(:url => 'http://something.com').
|
51
|
-
Webpage.new(:url => 'http://something-else.com').
|
52
|
-
Webpage.new(:url => 'http://sub.domains.something-else.com').
|
53
|
-
Webpage.new(:url => 'http://username:password@something-else.com').
|
54
|
-
Webpage.new(:url => "http://username:password@something-else.com\n").
|
55
|
-
Webpage.new(:url => "something else").
|
50
|
+
expect(Webpage.new(:url => 'http://something.com')).to be_valid
|
51
|
+
expect(Webpage.new(:url => 'http://something-else.com')).to be_valid
|
52
|
+
expect(Webpage.new(:url => 'http://sub.domains.something-else.com')).to be_valid
|
53
|
+
expect(Webpage.new(:url => 'http://username:password@something-else.com')).to be_valid
|
54
|
+
expect(Webpage.new(:url => "http://username:password@something-else.com\n")).not_to be_valid
|
55
|
+
expect(Webpage.new(:url => "something else")).not_to be_valid
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -62,12 +62,12 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
62
62
|
validates_formatting_of :zipcode, :using => :us_zip
|
63
63
|
end
|
64
64
|
it "validates that the zipcode provided is valid" do
|
65
|
-
USZip.new(:zipcode => '92348').
|
66
|
-
USZip.new(:zipcode => '23434-2348').
|
67
|
-
USZip.new(:zipcode => "23434-2348\n").
|
68
|
-
USZip.new(:zipcode => '234').
|
69
|
-
USZip.new(:zipcode => '23408234').
|
70
|
-
USZip.new(:zipcode => 'invalid').
|
65
|
+
expect(USZip.new(:zipcode => '92348')).to be_valid
|
66
|
+
expect(USZip.new(:zipcode => '23434-2348')).to be_valid
|
67
|
+
expect(USZip.new(:zipcode => "23434-2348\n")).not_to be_valid
|
68
|
+
expect(USZip.new(:zipcode => '234')).not_to be_valid
|
69
|
+
expect(USZip.new(:zipcode => '23408234')).not_to be_valid
|
70
|
+
expect(USZip.new(:zipcode => 'invalid')).not_to be_valid
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -77,10 +77,10 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
77
77
|
validates_formatting_of :alpha
|
78
78
|
end
|
79
79
|
it "validates that the letters provided is valid" do
|
80
|
-
Alpha.new(:alpha => 'abscdsofjsdpfahdsofkajlsdfaspdhjfads').
|
81
|
-
Alpha.new(:alpha => 'asdfalskdfjhas-dlfhasdksdfaldhfadsfasdfa').
|
82
|
-
Alpha.new(:alpha => 'adsufasodfksadjfskjdfha98').
|
83
|
-
Alpha.new(:alpha => 'asdf ausdpf98hasdfo alsdf ja8 sd').
|
80
|
+
expect(Alpha.new(:alpha => 'abscdsofjsdpfahdsofkajlsdfaspdhjfads')).to be_valid
|
81
|
+
expect(Alpha.new(:alpha => 'asdfalskdfjhas-dlfhasdksdfaldhfadsfasdfa')).to be_valid
|
82
|
+
expect(Alpha.new(:alpha => 'adsufasodfksadjfskjdfha98')).not_to be_valid
|
83
|
+
expect(Alpha.new(:alpha => 'asdf ausdpf98hasdfo alsdf ja8 sd')).not_to be_valid
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
@@ -90,10 +90,10 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
90
90
|
validates_formatting_of :letters_and_numbers, :using => :alphanum
|
91
91
|
end
|
92
92
|
it "validates that the letters provided is valid" do
|
93
|
-
Alphanum.new(:letters_and_numbers => 'numbersandlettersarevalid1234567890').
|
94
|
-
Alphanum.new(:letters_and_numbers => 'justletters').
|
95
|
-
Alphanum.new(:letters_and_numbers => 'letters and numbers 123 with spaces').
|
96
|
-
Alphanum.new(:letters_and_numbers => 'adding ; some special ** chars').
|
93
|
+
expect(Alphanum.new(:letters_and_numbers => 'numbersandlettersarevalid1234567890')).to be_valid
|
94
|
+
expect(Alphanum.new(:letters_and_numbers => 'justletters')).to be_valid
|
95
|
+
expect(Alphanum.new(:letters_and_numbers => 'letters and numbers 123 with spaces')).to be_valid
|
96
|
+
expect(Alphanum.new(:letters_and_numbers => 'adding ; some special ** chars')).not_to be_valid
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -103,14 +103,14 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
103
103
|
validates_formatting_of :phone_number, :using => :us_phone
|
104
104
|
end
|
105
105
|
it "validates that the phone number provided is valid" do
|
106
|
-
USPhone.new(:phone_number => '(234) 234-3456').
|
107
|
-
USPhone.new(:phone_number => '123 123 3456').
|
108
|
-
USPhone.new(:phone_number => '1231233456').
|
109
|
-
USPhone.new(:phone_number => '123.123.3456').
|
110
|
-
USPhone.new(:phone_number => '(223)123-2347').
|
111
|
-
USPhone.new(:phone_number => "(223)123-2347\n").
|
112
|
-
USPhone.new(:phone_number => '(223 123-2347').
|
113
|
-
USPhone.new(:phone_number => '12349870238').
|
106
|
+
expect(USPhone.new(:phone_number => '(234) 234-3456')).to be_valid
|
107
|
+
expect(USPhone.new(:phone_number => '123 123 3456')).to be_valid
|
108
|
+
expect(USPhone.new(:phone_number => '1231233456')).to be_valid
|
109
|
+
expect(USPhone.new(:phone_number => '123.123.3456')).to be_valid
|
110
|
+
expect(USPhone.new(:phone_number => '(223)123-2347')).to be_valid
|
111
|
+
expect(USPhone.new(:phone_number => "(223)123-2347\n")).not_to be_valid
|
112
|
+
expect(USPhone.new(:phone_number => '(223 123-2347')).not_to be_valid
|
113
|
+
expect(USPhone.new(:phone_number => '12349870238')).not_to be_valid
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
@@ -120,13 +120,13 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
120
120
|
validates_formatting_of :ipv4, :using => :ip_address_v4
|
121
121
|
end
|
122
122
|
it "validates that the IP address provided is valid" do
|
123
|
-
IPAddress.new(:ipv4 => '10.10.10').
|
124
|
-
IPAddress.new(:ipv4 => '999.10.10.20').
|
125
|
-
IPAddress.new(:ipv4 => '2222.22.22.22').
|
126
|
-
IPAddress.new(:ipv4 => '22.2222.22.2').
|
127
|
-
IPAddress.new(:ipv4 => '127.0.0.1').
|
128
|
-
IPAddress.new(:ipv4 => '132.254.111.10').
|
129
|
-
IPAddress.new(:ipv4 => "132.254.111.10\n").
|
123
|
+
expect(IPAddress.new(:ipv4 => '10.10.10')).not_to be_valid
|
124
|
+
expect(IPAddress.new(:ipv4 => '999.10.10.20')).not_to be_valid
|
125
|
+
expect(IPAddress.new(:ipv4 => '2222.22.22.22')).not_to be_valid
|
126
|
+
expect(IPAddress.new(:ipv4 => '22.2222.22.2')).not_to be_valid
|
127
|
+
expect(IPAddress.new(:ipv4 => '127.0.0.1')).to be_valid
|
128
|
+
expect(IPAddress.new(:ipv4 => '132.254.111.10')).to be_valid
|
129
|
+
expect(IPAddress.new(:ipv4 => "132.254.111.10\n")).not_to be_valid
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
@@ -138,11 +138,11 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
138
138
|
validates_formatting_of :cc, :using => :credit_card
|
139
139
|
end
|
140
140
|
it "validates that the credit card number provided is valid" do
|
141
|
-
Client.new(:cc => '4264-2879-1230-0000').
|
142
|
-
Client.new(:cc => '6011-1111-0000-2391').
|
143
|
-
Client.new(:cc => '5422434400828888').
|
144
|
-
Client.new(:cc => "5422434400828889\n").
|
145
|
-
Client.new(:cc => '1233444444444444').
|
141
|
+
expect(Client.new(:cc => '4264-2879-1230-0000')).to be_valid # Visa style
|
142
|
+
expect(Client.new(:cc => '6011-1111-0000-2391')).to be_valid # Discover style
|
143
|
+
expect(Client.new(:cc => '5422434400828888')).to be_valid # Mastercard style
|
144
|
+
expect(Client.new(:cc => "5422434400828889\n")).not_to be_valid # Mastercard style
|
145
|
+
expect(Client.new(:cc => '1233444444444444')).not_to be_valid # fake
|
146
146
|
end
|
147
147
|
end
|
148
148
|
|
@@ -152,13 +152,13 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
152
152
|
validates_formatting_of :ssn
|
153
153
|
end
|
154
154
|
it "validates that the social security number provided is valid" do
|
155
|
-
AnotherPerson.new(:ssn => "145.47.0191").
|
156
|
-
AnotherPerson.new(:ssn => "223-43-2343").
|
157
|
-
AnotherPerson.new(:ssn => "999.55.8888").
|
158
|
-
AnotherPerson.new(:ssn => "999.55.8888\n").
|
159
|
-
AnotherPerson.new(:ssn => "28934").
|
160
|
-
AnotherPerson.new(:ssn => "228934828934934").
|
161
|
-
AnotherPerson.new(:ssn => "23498.7234").
|
155
|
+
expect(AnotherPerson.new(:ssn => "145.47.0191")).to be_valid
|
156
|
+
expect(AnotherPerson.new(:ssn => "223-43-2343")).to be_valid
|
157
|
+
expect(AnotherPerson.new(:ssn => "999.55.8888")).to be_valid
|
158
|
+
expect(AnotherPerson.new(:ssn => "999.55.8888\n")).not_to be_valid
|
159
|
+
expect(AnotherPerson.new(:ssn => "28934")).not_to be_valid
|
160
|
+
expect(AnotherPerson.new(:ssn => "228934828934934")).not_to be_valid
|
161
|
+
expect(AnotherPerson.new(:ssn => "23498.7234")).not_to be_valid
|
162
162
|
end
|
163
163
|
end
|
164
164
|
|
@@ -168,16 +168,16 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
168
168
|
validates_formatting_of :color, :using => :hex_color
|
169
169
|
end
|
170
170
|
it "validates that the hex color value provided is valid" do
|
171
|
-
Color.new(:color => "efefef").
|
172
|
-
Color.new(:color => "98de89").
|
173
|
-
Color.new(:color => "000011").
|
174
|
-
Color.new(:color => "132").
|
175
|
-
Color.new(:color => "eef").
|
176
|
-
Color.new(:color => "eef\n").
|
177
|
-
Color.new(:color => "efefe").
|
178
|
-
Color.new(:color => "zsdfsd").
|
179
|
-
Color.new(:color => "p98hul;").
|
180
|
-
Color.new(:color => "sdfsdfsf").
|
171
|
+
expect(Color.new(:color => "efefef")).to be_valid
|
172
|
+
expect(Color.new(:color => "98de89")).to be_valid
|
173
|
+
expect(Color.new(:color => "000011")).to be_valid
|
174
|
+
expect(Color.new(:color => "132")).to be_valid
|
175
|
+
expect(Color.new(:color => "eef")).to be_valid
|
176
|
+
expect(Color.new(:color => "eef\n")).not_to be_valid
|
177
|
+
expect(Color.new(:color => "efefe")).not_to be_valid
|
178
|
+
expect(Color.new(:color => "zsdfsd")).not_to be_valid
|
179
|
+
expect(Color.new(:color => "p98hul;")).not_to be_valid
|
180
|
+
expect(Color.new(:color => "sdfsdfsf")).not_to be_valid
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
@@ -189,9 +189,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
189
189
|
end
|
190
190
|
it "validates the phone formatting only on creation" do
|
191
191
|
option = Phony.new(:phone => "(123) 234-4567")
|
192
|
-
option.
|
192
|
+
expect(option).to be_valid
|
193
193
|
option.phone = "123123123"
|
194
|
-
option.
|
194
|
+
expect(option).to be_valid
|
195
195
|
end
|
196
196
|
|
197
197
|
class Iffy < TestActiveRecord
|
@@ -200,8 +200,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
200
200
|
validates_formatting_of :phone, :using => :us_phone, :if => lambda { |iffy| iffy.name == "Matthew" }
|
201
201
|
end
|
202
202
|
it "validates the phone formatting only if a name is specified" do
|
203
|
-
Iffy.new(:phone => "(123 345-4567", :name => "Bill").
|
204
|
-
Iffy.new(:phone => "(123 345-4567", :name => "Matthew").
|
203
|
+
expect(Iffy.new(:phone => "(123 345-4567", :name => "Bill")).to be_valid
|
204
|
+
expect(Iffy.new(:phone => "(123 345-4567", :name => "Matthew")).not_to be_valid
|
205
205
|
end
|
206
206
|
|
207
207
|
class Unlessy < TestActiveRecord
|
@@ -210,8 +210,8 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
210
210
|
validates_formatting_of :phone, :using => :us_phone, :unless => lambda { |unlessy| unlessy.name == "Matthew" }
|
211
211
|
end
|
212
212
|
it "validates the phone formatting only if a name is specified" do
|
213
|
-
Unlessy.new(:phone => "(123 345-4567", :name => "Bill").
|
214
|
-
Unlessy.new(:phone => "(123 345-4567", :name => "Matthew").
|
213
|
+
expect(Unlessy.new(:phone => "(123 345-4567", :name => "Bill")).not_to be_valid
|
214
|
+
expect(Unlessy.new(:phone => "(123 345-4567", :name => "Matthew")).to be_valid
|
215
215
|
end
|
216
216
|
end
|
217
217
|
describe "dollars" do
|
@@ -220,15 +220,15 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
220
220
|
validates_formatting_of :amount, :using => :dollars
|
221
221
|
end
|
222
222
|
it "validates that the dollars amount provided is valid" do
|
223
|
-
Money.new(:amount => "$100.00").
|
224
|
-
Money.new(:amount => "100.00").
|
225
|
-
Money.new(:amount => "12,234,343").
|
226
|
-
Money.new(:amount => "$12.34").
|
227
|
-
Money.new(:amount => "120,123,232.32").
|
228
|
-
Money.new(:amount => "$$1111111100").
|
229
|
-
Money.new(:amount => "100;00").
|
230
|
-
Money.new(:amount => "238,3423,42..99").
|
231
|
-
Money.new(:amount => "$-233").
|
223
|
+
expect(Money.new(:amount => "$100.00")).to be_valid
|
224
|
+
expect(Money.new(:amount => "100.00")).to be_valid
|
225
|
+
expect(Money.new(:amount => "12,234,343")).to be_valid
|
226
|
+
expect(Money.new(:amount => "$12.34")).to be_valid
|
227
|
+
expect(Money.new(:amount => "120,123,232.32")).to be_valid
|
228
|
+
expect(Money.new(:amount => "$$1111111100")).not_to be_valid
|
229
|
+
expect(Money.new(:amount => "100;00")).not_to be_valid
|
230
|
+
expect(Money.new(:amount => "238,3423,42..99")).not_to be_valid
|
231
|
+
expect(Money.new(:amount => "$-233")).not_to be_valid
|
232
232
|
end
|
233
233
|
end
|
234
234
|
describe "custom messages" do
|
@@ -238,9 +238,9 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
238
238
|
end
|
239
239
|
it "are allowed and can be used in displaying error messages" do
|
240
240
|
message = Message.new(:first_name => "invalid-first-name-123")
|
241
|
-
message.
|
242
|
-
message.errors.keys.class.
|
243
|
-
message.errors.full_messages.first.
|
241
|
+
expect(message).not_to be_valid
|
242
|
+
expect(message.errors.keys.class).to eq Array
|
243
|
+
expect(message.errors.full_messages.first).to match(/is not a valid first name/)
|
244
244
|
end
|
245
245
|
end
|
246
246
|
|
@@ -251,11 +251,11 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
251
251
|
end
|
252
252
|
it "set a default error" do
|
253
253
|
problems = Problems.new(:name => "sdfs12312dfsd")
|
254
|
-
problems.
|
255
|
-
problems.errors.full_messages.first.
|
254
|
+
expect(problems).not_to be_valid
|
255
|
+
expect(problems.errors.full_messages.first).to match(/letters/i)
|
256
256
|
email = Email.new(:email => "not.an.email.address")
|
257
|
-
email.
|
258
|
-
email.errors.full_messages.first.
|
257
|
+
expect(email).not_to be_valid
|
258
|
+
expect(email.errors.full_messages.first).to match(/email/i)
|
259
259
|
end
|
260
260
|
end
|
261
261
|
|
@@ -269,13 +269,13 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
269
269
|
let(:people) { PeopleTest.new(:email => "mbridges.91@gmail.com", :email2 => "mbridges.91@gmail.com", :email3 => "mbridges.91@gmail.com") }
|
270
270
|
it "should test nil and blank values correctly" do
|
271
271
|
people.email = nil
|
272
|
-
people.
|
272
|
+
expect(people).to be_valid
|
273
273
|
people.email = "mbridges.91@gmail.com"
|
274
274
|
people.email2 = ""
|
275
|
-
people.
|
275
|
+
expect(people).to be_valid
|
276
276
|
people.email2 = "mbridges.91@gmail.com"
|
277
277
|
people.email3 = nil
|
278
|
-
people.
|
278
|
+
expect(people).not_to be_valid
|
279
279
|
end
|
280
280
|
end
|
281
281
|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'validates_formatting_of/validation_addition'
|
2
|
+
require 'its'
|
2
3
|
|
3
4
|
module ValidatesFormattingOf
|
4
5
|
|
@@ -9,43 +10,43 @@ module ValidatesFormattingOf
|
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
12
|
-
describe ValidationAddition do
|
13
|
+
RSpec.describe ValidationAddition do
|
13
14
|
before do
|
14
15
|
TestAdding.clear!
|
15
16
|
TestAdding.add :email, /email/i
|
16
17
|
end
|
17
18
|
it "should be able to add new validations" do
|
18
19
|
TestAdding.add :another, /another/i
|
19
|
-
TestAdding.validations.count.
|
20
|
-
TestAdding.validations['email'].
|
21
|
-
TestAdding.validations['another'].
|
20
|
+
expect(TestAdding.validations.count).to eq(2)
|
21
|
+
expect(TestAdding.validations['email']).to be_instance_of Validation
|
22
|
+
expect(TestAdding.validations['another']).to be_instance_of Validation
|
22
23
|
end
|
23
24
|
describe "find" do
|
24
25
|
context "implicit validation method" do
|
25
26
|
subject { TestAdding.find(:email) }
|
26
|
-
its(:name) {
|
27
|
-
its(:regex) {
|
27
|
+
its(:name) { is_expected.to eq(:email) }
|
28
|
+
its(:regex) { is_expected.to eq(/email/i) }
|
28
29
|
end
|
29
30
|
context "explicit validation method" do
|
30
31
|
subject { TestAdding.find(:non_existent_validation, :using => :email) }
|
31
|
-
its(:name) {
|
32
|
-
its(:regex) {
|
32
|
+
its(:name) { is_expected.to eq(:email) }
|
33
|
+
its(:regex) { is_expected.to eq(/email/i) }
|
33
34
|
end
|
34
35
|
end
|
35
36
|
it "should raise an error if the method does not exist" do
|
36
37
|
expect { TestAdding.find(:fake) }.to raise_error MissingValidation
|
37
38
|
end
|
38
39
|
it "should be able to determine if the method does not exist" do
|
39
|
-
TestAdding.exists?(:email).
|
40
|
-
TestAdding.exists?(:non_existent).
|
40
|
+
expect(TestAdding.exists?(:email)).to be_truthy
|
41
|
+
expect(TestAdding.exists?(:non_existent)).to be_falsey
|
41
42
|
end
|
42
43
|
it "should be able to accept strings for validation names (for namespacing)" do
|
43
44
|
TestAdding.add "namespace:phone", /namespace/i
|
44
|
-
TestAdding.exists?("namespace:phone").
|
45
|
+
expect(TestAdding.exists?("namespace:phone")).to be_truthy
|
45
46
|
end
|
46
47
|
it "should be able to determine if the method is missing" do
|
47
|
-
TestAdding.missing?(:non_existent).
|
48
|
-
TestAdding.missing?(:email).
|
48
|
+
expect(TestAdding.missing?(:non_existent)).to be_truthy
|
49
|
+
expect(TestAdding.missing?(:email)).to be_falsey
|
49
50
|
end
|
50
51
|
end
|
51
52
|
end
|
@@ -1,18 +1,19 @@
|
|
1
1
|
require 'validates_formatting_of/validation'
|
2
|
+
require 'its'
|
2
3
|
|
3
4
|
module ValidatesFormattingOf
|
4
5
|
|
5
|
-
describe Validation do
|
6
|
+
RSpec.describe Validation do
|
6
7
|
context "valid validation creation" do
|
7
8
|
let(:validation) { Validation.new(:name, /something/i, "is an invalid value") }
|
8
9
|
subject { validation }
|
9
|
-
its(:name) {
|
10
|
-
its(:regex) {
|
11
|
-
its(:message) {
|
12
|
-
its(:to_s) {
|
13
|
-
its(:inspect) {
|
14
|
-
its(:inspect) {
|
15
|
-
its(:inspect) {
|
10
|
+
its(:name) { is_expected.to eq(:name) }
|
11
|
+
its(:regex) { is_expected.to eq(%r{something}i) }
|
12
|
+
its(:message) { is_expected.to eq('is an invalid value') }
|
13
|
+
its(:to_s) { is_expected.to eq("<Validation::name>") }
|
14
|
+
its(:inspect) { is_expected.to match(/Validation/) }
|
15
|
+
its(:inspect) { is_expected.to match(/\/something\/i/) }
|
16
|
+
its(:inspect) { is_expected.to match(/\:name/) }
|
16
17
|
end
|
17
18
|
context "invalid validation creation" do
|
18
19
|
it "should raise an error if the specified regex is not a Regexp objct" do
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe ValidatesFormattingOf::Validations do
|
3
|
+
RSpec.describe ValidatesFormattingOf::Validations do
|
4
4
|
|
5
5
|
describe "with 'sexy' validation style" do
|
6
6
|
describe "email" do
|
@@ -10,17 +10,17 @@ describe ValidatesFormattingOf::Validations do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it "validates that the email provided is valid" do
|
13
|
-
Email.new(:email => "example@example.com").
|
14
|
-
Email.new(:email => "badexample.com").
|
15
|
-
Email.new(:email => "mbridges.91@gmail.com").
|
16
|
-
Email.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").
|
17
|
-
Email.new(:email => "this__???{}|__should@be-valid.com").
|
18
|
-
Email.new(:email => "visitorservices@vmfa.museum").
|
19
|
-
Email.new(:email => "info@samoa.travel").
|
20
|
-
Email.new(:email => "info@-samoa.travel").
|
21
|
-
Email.new(:email => "info@samoa-.travel").
|
22
|
-
Email.new(:email => "info@123-samoa.travel").
|
23
|
-
Email.new(:email => "info@123-samoa.travel\n").
|
13
|
+
expect(Email.new(:email => "example@example.com")).to be_valid
|
14
|
+
expect(Email.new(:email => "badexample.com")).not_to be_valid
|
15
|
+
expect(Email.new(:email => "mbridges.91@gmail.com")).to be_valid
|
16
|
+
expect(Email.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com")).to be_valid
|
17
|
+
expect(Email.new(:email => "this__???{}|__should@be-valid.com")).to be_valid
|
18
|
+
expect(Email.new(:email => "visitorservices@vmfa.museum")).to be_valid
|
19
|
+
expect(Email.new(:email => "info@samoa.travel")).to be_valid
|
20
|
+
expect(Email.new(:email => "info@-samoa.travel")).not_to be_valid
|
21
|
+
expect(Email.new(:email => "info@samoa-.travel")).not_to be_valid
|
22
|
+
expect(Email.new(:email => "info@123-samoa.travel")).to be_valid
|
23
|
+
expect(Email.new(:email => "info@123-samoa.travel\n")).not_to be_valid
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -30,17 +30,17 @@ describe ValidatesFormattingOf::Validations do
|
|
30
30
|
validates :email, :simple_email => true
|
31
31
|
end
|
32
32
|
it "validates that the email provided is valid" do
|
33
|
-
SimpleEmail.new(:email => "example@example.com").
|
34
|
-
SimpleEmail.new(:email => "badexample.com").
|
35
|
-
SimpleEmail.new(:email => "mbridges.91@gmail.com").
|
36
|
-
SimpleEmail.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").
|
37
|
-
SimpleEmail.new(:email => "this__???{}|__should@be-valid.com").
|
38
|
-
SimpleEmail.new(:email => "visitorservices@vmfa.museum").
|
39
|
-
SimpleEmail.new(:email => "info@samoa.travel").
|
40
|
-
SimpleEmail.new(:email => "info@-samoa.travel").
|
41
|
-
SimpleEmail.new(:email => "info@samoa-.travel").
|
42
|
-
SimpleEmail.new(:email => "info@123-samoa.travel").
|
43
|
-
SimpleEmail.new(:email => "info@123-samoa.travel\n").
|
33
|
+
expect(SimpleEmail.new(:email => "example@example.com")).to be_valid
|
34
|
+
expect(SimpleEmail.new(:email => "badexample.com")).not_to be_valid
|
35
|
+
expect(SimpleEmail.new(:email => "mbridges.91@gmail.com")).to be_valid
|
36
|
+
expect(SimpleEmail.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com")).to be_valid
|
37
|
+
expect(SimpleEmail.new(:email => "this__???{}|__should@be-valid.com")).to be_valid
|
38
|
+
expect(SimpleEmail.new(:email => "visitorservices@vmfa.museum")).to be_valid
|
39
|
+
expect(SimpleEmail.new(:email => "info@samoa.travel")).to be_valid
|
40
|
+
expect(SimpleEmail.new(:email => "info@-samoa.travel")).to be_valid
|
41
|
+
expect(SimpleEmail.new(:email => "info@samoa-.travel")).to be_valid
|
42
|
+
expect(SimpleEmail.new(:email => "info@123-samoa.travel")).to be_valid
|
43
|
+
expect(SimpleEmail.new(:email => "info@123-samoa.travel\n")).not_to be_valid
|
44
44
|
end
|
45
45
|
end
|
46
46
|
describe "url" do
|
@@ -49,12 +49,12 @@ describe ValidatesFormattingOf::Validations do
|
|
49
49
|
validates :url, :url => true
|
50
50
|
end
|
51
51
|
it "validates that the url provided is valid" do
|
52
|
-
Webpage.new(:url => 'http://something.com').
|
53
|
-
Webpage.new(:url => 'http://something-else.com').
|
54
|
-
Webpage.new(:url => 'http://sub.domains.something-else.com').
|
55
|
-
Webpage.new(:url => 'http://username:password@something-else.com').
|
56
|
-
Webpage.new(:url => "http://username:password@something-else.com\n").
|
57
|
-
Webpage.new(:url => "something else").
|
52
|
+
expect(Webpage.new(:url => 'http://something.com')).to be_valid
|
53
|
+
expect(Webpage.new(:url => 'http://something-else.com')).to be_valid
|
54
|
+
expect(Webpage.new(:url => 'http://sub.domains.something-else.com')).to be_valid
|
55
|
+
expect(Webpage.new(:url => 'http://username:password@something-else.com')).to be_valid
|
56
|
+
expect(Webpage.new(:url => "http://username:password@something-else.com\n")).not_to be_valid
|
57
|
+
expect(Webpage.new(:url => "something else")).not_to be_valid
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -64,12 +64,12 @@ describe ValidatesFormattingOf::Validations do
|
|
64
64
|
validates :zipcode, :us_zip => true
|
65
65
|
end
|
66
66
|
it "validates that the zipcode provided is valid" do
|
67
|
-
USZip.new(:zipcode => '92348').
|
68
|
-
USZip.new(:zipcode => '23434-2348').
|
69
|
-
USZip.new(:zipcode => "23434-2348\n").
|
70
|
-
USZip.new(:zipcode => '234').
|
71
|
-
USZip.new(:zipcode => '23408234').
|
72
|
-
USZip.new(:zipcode => 'invalid').
|
67
|
+
expect(USZip.new(:zipcode => '92348')).to be_valid
|
68
|
+
expect(USZip.new(:zipcode => '23434-2348')).to be_valid
|
69
|
+
expect(USZip.new(:zipcode => "23434-2348\n")).not_to be_valid
|
70
|
+
expect(USZip.new(:zipcode => '234')).not_to be_valid
|
71
|
+
expect(USZip.new(:zipcode => '23408234')).not_to be_valid
|
72
|
+
expect(USZip.new(:zipcode => 'invalid')).not_to be_valid
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
@@ -79,10 +79,10 @@ describe ValidatesFormattingOf::Validations do
|
|
79
79
|
validates :alpha, :alpha => true
|
80
80
|
end
|
81
81
|
it "validates that the letters provided is valid" do
|
82
|
-
Alpha.new(:alpha => 'abscdsofjsdpfahdsofkajlsdfaspdhjfads').
|
83
|
-
Alpha.new(:alpha => 'asdfalskdfjhas-dlfhasdksdfaldhfadsfasdfa').
|
84
|
-
Alpha.new(:alpha => 'adsufasodfksadjfskjdfha98').
|
85
|
-
Alpha.new(:alpha => 'asdf ausdpf98hasdfo alsdf ja8 sd').
|
82
|
+
expect(Alpha.new(:alpha => 'abscdsofjsdpfahdsofkajlsdfaspdhjfads')).to be_valid
|
83
|
+
expect(Alpha.new(:alpha => 'asdfalskdfjhas-dlfhasdksdfaldhfadsfasdfa')).to be_valid
|
84
|
+
expect(Alpha.new(:alpha => 'adsufasodfksadjfskjdfha98')).not_to be_valid
|
85
|
+
expect(Alpha.new(:alpha => 'asdf ausdpf98hasdfo alsdf ja8 sd')).not_to be_valid
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
@@ -92,10 +92,10 @@ describe ValidatesFormattingOf::Validations do
|
|
92
92
|
validates :letters_and_numbers, :alphanum => true
|
93
93
|
end
|
94
94
|
it "validates that the letters provided is valid" do
|
95
|
-
Alphanum.new(:letters_and_numbers => 'numbersandlettersarevalid1234567890').
|
96
|
-
Alphanum.new(:letters_and_numbers => 'justletters').
|
97
|
-
Alphanum.new(:letters_and_numbers => 'letters and numbers 123 with spaces').
|
98
|
-
Alphanum.new(:letters_and_numbers => 'adding ; some special ** chars').
|
95
|
+
expect(Alphanum.new(:letters_and_numbers => 'numbersandlettersarevalid1234567890')).to be_valid
|
96
|
+
expect(Alphanum.new(:letters_and_numbers => 'justletters')).to be_valid
|
97
|
+
expect(Alphanum.new(:letters_and_numbers => 'letters and numbers 123 with spaces')).to be_valid
|
98
|
+
expect(Alphanum.new(:letters_and_numbers => 'adding ; some special ** chars')).not_to be_valid
|
99
99
|
end
|
100
100
|
end
|
101
101
|
|
@@ -105,14 +105,14 @@ describe ValidatesFormattingOf::Validations do
|
|
105
105
|
validates :phone_number, :us_phone => true
|
106
106
|
end
|
107
107
|
it "validates that the phone number provided is valid" do
|
108
|
-
USPhone.new(:phone_number => '(234) 234-3456').
|
109
|
-
USPhone.new(:phone_number => '123 123 3456').
|
110
|
-
USPhone.new(:phone_number => '1231233456').
|
111
|
-
USPhone.new(:phone_number => '123.123.3456').
|
112
|
-
USPhone.new(:phone_number => '(223)123-2347').
|
113
|
-
USPhone.new(:phone_number => "(223)123-2347\n").
|
114
|
-
USPhone.new(:phone_number => '(223 123-2347').
|
115
|
-
USPhone.new(:phone_number => '12349870238').
|
108
|
+
expect(USPhone.new(:phone_number => '(234) 234-3456')).to be_valid
|
109
|
+
expect(USPhone.new(:phone_number => '123 123 3456')).to be_valid
|
110
|
+
expect(USPhone.new(:phone_number => '1231233456')).to be_valid
|
111
|
+
expect(USPhone.new(:phone_number => '123.123.3456')).to be_valid
|
112
|
+
expect(USPhone.new(:phone_number => '(223)123-2347')).to be_valid
|
113
|
+
expect(USPhone.new(:phone_number => "(223)123-2347\n")).not_to be_valid
|
114
|
+
expect(USPhone.new(:phone_number => '(223 123-2347')).not_to be_valid
|
115
|
+
expect(USPhone.new(:phone_number => '12349870238')).not_to be_valid
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
@@ -122,13 +122,13 @@ describe ValidatesFormattingOf::Validations do
|
|
122
122
|
validates :ipv4, :ip_address_v4 => true
|
123
123
|
end
|
124
124
|
it "validates that the IP address provided is valid" do
|
125
|
-
IPAddress.new(:ipv4 => '10.10.10').
|
126
|
-
IPAddress.new(:ipv4 => '999.10.10.20').
|
127
|
-
IPAddress.new(:ipv4 => '2222.22.22.22').
|
128
|
-
IPAddress.new(:ipv4 => '22.2222.22.2').
|
129
|
-
IPAddress.new(:ipv4 => '127.0.0.1').
|
130
|
-
IPAddress.new(:ipv4 => '132.254.111.10').
|
131
|
-
IPAddress.new(:ipv4 => "132.254.111.10\n").
|
125
|
+
expect(IPAddress.new(:ipv4 => '10.10.10')).not_to be_valid
|
126
|
+
expect(IPAddress.new(:ipv4 => '999.10.10.20')).not_to be_valid
|
127
|
+
expect(IPAddress.new(:ipv4 => '2222.22.22.22')).not_to be_valid
|
128
|
+
expect(IPAddress.new(:ipv4 => '22.2222.22.2')).not_to be_valid
|
129
|
+
expect(IPAddress.new(:ipv4 => '127.0.0.1')).to be_valid
|
130
|
+
expect(IPAddress.new(:ipv4 => '132.254.111.10')).to be_valid
|
131
|
+
expect(IPAddress.new(:ipv4 => "132.254.111.10\n")).not_to be_valid
|
132
132
|
end
|
133
133
|
end
|
134
134
|
|
@@ -140,11 +140,11 @@ describe ValidatesFormattingOf::Validations do
|
|
140
140
|
validates :cc, :credit_card => true
|
141
141
|
end
|
142
142
|
it "validates that the credit card number provided is valid" do
|
143
|
-
Client.new(:cc => '4264-2879-1230-0000').
|
144
|
-
Client.new(:cc => '6011-1111-0000-2391').
|
145
|
-
Client.new(:cc => '5422434400828888').
|
146
|
-
Client.new(:cc => "5422434400828889\n").
|
147
|
-
Client.new(:cc => '1233444444444444').
|
143
|
+
expect(Client.new(:cc => '4264-2879-1230-0000')).to be_valid # Visa style
|
144
|
+
expect(Client.new(:cc => '6011-1111-0000-2391')).to be_valid # Discover style
|
145
|
+
expect(Client.new(:cc => '5422434400828888')).to be_valid # Mastercard style
|
146
|
+
expect(Client.new(:cc => "5422434400828889\n")).not_to be_valid # Mastercard style
|
147
|
+
expect(Client.new(:cc => '1233444444444444')).not_to be_valid # fake
|
148
148
|
end
|
149
149
|
end
|
150
150
|
|
@@ -154,13 +154,13 @@ describe ValidatesFormattingOf::Validations do
|
|
154
154
|
validates :ssn, :ssn => true
|
155
155
|
end
|
156
156
|
it "validates that the social security number provided is valid" do
|
157
|
-
AnotherPerson.new(:ssn => "145.47.0191").
|
158
|
-
AnotherPerson.new(:ssn => "223-43-2343").
|
159
|
-
AnotherPerson.new(:ssn => "999.55.8888").
|
160
|
-
AnotherPerson.new(:ssn => "999.55.8888\n").
|
161
|
-
AnotherPerson.new(:ssn => "28934").
|
162
|
-
AnotherPerson.new(:ssn => "228934828934934").
|
163
|
-
AnotherPerson.new(:ssn => "23498.7234").
|
157
|
+
expect(AnotherPerson.new(:ssn => "145.47.0191")).to be_valid
|
158
|
+
expect(AnotherPerson.new(:ssn => "223-43-2343")).to be_valid
|
159
|
+
expect(AnotherPerson.new(:ssn => "999.55.8888")).to be_valid
|
160
|
+
expect(AnotherPerson.new(:ssn => "999.55.8888\n")).not_to be_valid
|
161
|
+
expect(AnotherPerson.new(:ssn => "28934")).not_to be_valid
|
162
|
+
expect(AnotherPerson.new(:ssn => "228934828934934")).not_to be_valid
|
163
|
+
expect(AnotherPerson.new(:ssn => "23498.7234")).not_to be_valid
|
164
164
|
end
|
165
165
|
end
|
166
166
|
|
@@ -170,16 +170,16 @@ describe ValidatesFormattingOf::Validations do
|
|
170
170
|
validates :color, :hex_color => true
|
171
171
|
end
|
172
172
|
it "validates that the hex color value provided is valid" do
|
173
|
-
Color.new(:color => "efefef").
|
174
|
-
Color.new(:color => "98de89").
|
175
|
-
Color.new(:color => "000011").
|
176
|
-
Color.new(:color => "132").
|
177
|
-
Color.new(:color => "eef").
|
178
|
-
Color.new(:color => "eef\n").
|
179
|
-
Color.new(:color => "efefe").
|
180
|
-
Color.new(:color => "zsdfsd").
|
181
|
-
Color.new(:color => "p98hul;").
|
182
|
-
Color.new(:color => "sdfsdfsf").
|
173
|
+
expect(Color.new(:color => "efefef")).to be_valid
|
174
|
+
expect(Color.new(:color => "98de89")).to be_valid
|
175
|
+
expect(Color.new(:color => "000011")).to be_valid
|
176
|
+
expect(Color.new(:color => "132")).to be_valid
|
177
|
+
expect(Color.new(:color => "eef")).to be_valid
|
178
|
+
expect(Color.new(:color => "eef\n")).not_to be_valid
|
179
|
+
expect(Color.new(:color => "efefe")).not_to be_valid
|
180
|
+
expect(Color.new(:color => "zsdfsd")).not_to be_valid
|
181
|
+
expect(Color.new(:color => "p98hul;")).not_to be_valid
|
182
|
+
expect(Color.new(:color => "sdfsdfsf")).not_to be_valid
|
183
183
|
end
|
184
184
|
end
|
185
185
|
|
@@ -191,9 +191,9 @@ describe ValidatesFormattingOf::Validations do
|
|
191
191
|
end
|
192
192
|
it "validates the phone formatting only on creation" do
|
193
193
|
option = Phony.new(:phone => "(123) 234-4567")
|
194
|
-
option.
|
194
|
+
expect(option).to be_valid
|
195
195
|
option.phone = "123123123"
|
196
|
-
option.
|
196
|
+
expect(option).to be_valid
|
197
197
|
end
|
198
198
|
|
199
199
|
class Iffy < TestActiveRecord
|
@@ -202,8 +202,8 @@ describe ValidatesFormattingOf::Validations do
|
|
202
202
|
validates :phone, :us_phone => true, :if => lambda { |iffy| iffy.name == "Matthew" }
|
203
203
|
end
|
204
204
|
it "validates the phone formatting only if a name is specified" do
|
205
|
-
Iffy.new(:phone => "(123 345-4567", :name => "Bill").
|
206
|
-
Iffy.new(:phone => "(123 345-4567", :name => "Matthew").
|
205
|
+
expect(Iffy.new(:phone => "(123 345-4567", :name => "Bill")).to be_valid
|
206
|
+
expect(Iffy.new(:phone => "(123 345-4567", :name => "Matthew")).not_to be_valid
|
207
207
|
end
|
208
208
|
|
209
209
|
class Unlessy < TestActiveRecord
|
@@ -212,8 +212,8 @@ describe ValidatesFormattingOf::Validations do
|
|
212
212
|
validates :phone, :us_phone => true, :unless => lambda { |unlessy| unlessy.name == "Matthew" }
|
213
213
|
end
|
214
214
|
it "validates the phone formatting only if a name is specified" do
|
215
|
-
Unlessy.new(:phone => "(123 345-4567", :name => "Bill").
|
216
|
-
Unlessy.new(:phone => "(123 345-4567", :name => "Matthew").
|
215
|
+
expect(Unlessy.new(:phone => "(123 345-4567", :name => "Bill")).not_to be_valid
|
216
|
+
expect(Unlessy.new(:phone => "(123 345-4567", :name => "Matthew")).to be_valid
|
217
217
|
end
|
218
218
|
end
|
219
219
|
describe "dollars" do
|
@@ -222,15 +222,15 @@ describe ValidatesFormattingOf::Validations do
|
|
222
222
|
validates :amount, :dollars => true
|
223
223
|
end
|
224
224
|
it "validates that the dollars amount provided is valid" do
|
225
|
-
Money.new(:amount => "$100.00").
|
226
|
-
Money.new(:amount => "100.00").
|
227
|
-
Money.new(:amount => "12,234,343").
|
228
|
-
Money.new(:amount => "$12.34").
|
229
|
-
Money.new(:amount => "120,123,232.32").
|
230
|
-
Money.new(:amount => "$$1111111100").
|
231
|
-
Money.new(:amount => "100;00").
|
232
|
-
Money.new(:amount => "238,3423,42..99").
|
233
|
-
Money.new(:amount => "$-233").
|
225
|
+
expect(Money.new(:amount => "$100.00")).to be_valid
|
226
|
+
expect(Money.new(:amount => "100.00")).to be_valid
|
227
|
+
expect(Money.new(:amount => "12,234,343")).to be_valid
|
228
|
+
expect(Money.new(:amount => "$12.34")).to be_valid
|
229
|
+
expect(Money.new(:amount => "120,123,232.32")).to be_valid
|
230
|
+
expect(Money.new(:amount => "$$1111111100")).not_to be_valid
|
231
|
+
expect(Money.new(:amount => "100;00")).not_to be_valid
|
232
|
+
expect(Money.new(:amount => "238,3423,42..99")).not_to be_valid
|
233
|
+
expect(Money.new(:amount => "$-233")).not_to be_valid
|
234
234
|
end
|
235
235
|
end
|
236
236
|
describe "custom messages" do
|
@@ -240,9 +240,9 @@ describe ValidatesFormattingOf::Validations do
|
|
240
240
|
end
|
241
241
|
it "are allowed and can be used in displaying error messages" do
|
242
242
|
message = Message.new(:first_name => "invalid-first-name-123")
|
243
|
-
message.
|
244
|
-
message.errors.keys.class.
|
245
|
-
message.errors.full_messages.first.
|
243
|
+
expect(message).not_to be_valid
|
244
|
+
expect(message.errors.keys.class).to eq Array
|
245
|
+
expect(message.errors.full_messages.first).to match(/is not a valid first name/)
|
246
246
|
end
|
247
247
|
end
|
248
248
|
|
@@ -253,11 +253,11 @@ describe ValidatesFormattingOf::Validations do
|
|
253
253
|
end
|
254
254
|
it "set a default error" do
|
255
255
|
problems = Problems.new(:name => "sdfs12312dfsd")
|
256
|
-
problems.
|
257
|
-
problems.errors.full_messages.first.
|
256
|
+
expect(problems).not_to be_valid
|
257
|
+
expect(problems.errors.full_messages.first).to match(/letters/i)
|
258
258
|
email = Email.new(:email => "not.an.email.address")
|
259
|
-
email.
|
260
|
-
email.errors.full_messages.first.
|
259
|
+
expect(email).not_to be_valid
|
260
|
+
expect(email.errors.full_messages.first).to match(/email/i)
|
261
261
|
end
|
262
262
|
end
|
263
263
|
|
@@ -271,13 +271,13 @@ describe ValidatesFormattingOf::Validations do
|
|
271
271
|
let(:people) { PeopleTest.new(:email => "mbridges.91@gmail.com", :email2 => "mbridges.91@gmail.com", :email3 => "mbridges.91@gmail.com") }
|
272
272
|
it "should test nil and blank values correctly" do
|
273
273
|
people.email = nil
|
274
|
-
people.
|
274
|
+
expect(people).to be_valid
|
275
275
|
people.email = "mbridges.91@gmail.com"
|
276
276
|
people.email2 = ""
|
277
|
-
people.
|
277
|
+
expect(people).to be_valid
|
278
278
|
people.email2 = "mbridges.91@gmail.com"
|
279
279
|
people.email3 = nil
|
280
|
-
people.
|
280
|
+
expect(people).not_to be_valid
|
281
281
|
end
|
282
282
|
end
|
283
283
|
end
|
@@ -15,9 +15,10 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = ValidatesFormattingOf::VERSION
|
17
17
|
|
18
|
-
gem.add_dependency "activemodel"
|
18
|
+
gem.add_dependency "activemodel"
|
19
19
|
|
20
20
|
gem.add_development_dependency "rake"
|
21
21
|
gem.add_development_dependency "rspec"
|
22
22
|
gem.add_development_dependency "simplecov"
|
23
|
+
gem.add_development_dependency "its"
|
23
24
|
end
|
metadata
CHANGED
@@ -1,69 +1,83 @@
|
|
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.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Bridges
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: simplecov
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: its
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
60
74
|
- !ruby/object:Gem::Version
|
61
75
|
version: '0'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- -
|
80
|
+
- - ">="
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
69
83
|
description: Common Rails validations wrapped in a gem.
|
@@ -73,9 +87,9 @@ executables: []
|
|
73
87
|
extensions: []
|
74
88
|
extra_rdoc_files: []
|
75
89
|
files:
|
76
|
-
- .gitignore
|
77
|
-
- .rspec
|
78
|
-
- .travis.yml
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
79
93
|
- CHANGELOG.md
|
80
94
|
- Gemfile
|
81
95
|
- LICENSE.txt
|
@@ -104,17 +118,17 @@ require_paths:
|
|
104
118
|
- lib
|
105
119
|
required_ruby_version: !ruby/object:Gem::Requirement
|
106
120
|
requirements:
|
107
|
-
- -
|
121
|
+
- - ">="
|
108
122
|
- !ruby/object:Gem::Version
|
109
123
|
version: '0'
|
110
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
125
|
requirements:
|
112
|
-
- -
|
126
|
+
- - ">="
|
113
127
|
- !ruby/object:Gem::Version
|
114
128
|
version: '0'
|
115
129
|
requirements: []
|
116
130
|
rubyforge_project:
|
117
|
-
rubygems_version: 2.
|
131
|
+
rubygems_version: 2.4.5
|
118
132
|
signing_key:
|
119
133
|
specification_version: 4
|
120
134
|
summary: Adds several convenient methods to validate things such as emails, urls,
|
@@ -125,4 +139,3 @@ test_files:
|
|
125
139
|
- spec/validates_formatting_of/validation_addition_spec.rb
|
126
140
|
- spec/validates_formatting_of/validation_spec.rb
|
127
141
|
- spec/validates_formatting_of/validator_spec.rb
|
128
|
-
has_rdoc:
|