validates_formatting_of 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
# validates_formatting_of
|
1
|
+
# validates_formatting_of [![Build Status](https://secure.travis-ci.org/mattdbridges/validates_formatting_of.png)](http://travis-ci.org/mattdbridges/validates_formatting_of)
|
2
2
|
|
3
|
-
The
|
3
|
+
The `validates_formatting_of` gem addess several convenient methods to validating things such as emails, urls, and phones in a Rails application.
|
4
4
|
|
5
5
|
# Installation
|
6
6
|
|
7
|
-
To install
|
7
|
+
To install `validates_formatting_of`, add the following to your `Gemfile`:
|
8
8
|
|
9
9
|
gem 'validates_formatting_of'
|
10
10
|
|
@@ -26,11 +26,64 @@ This call will ensure that the user-provided email is a valid email. This way, y
|
|
26
26
|
|
27
27
|
`validates_formatting_of` has support for the following validations:
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
29
|
+
### Email
|
30
|
+
|
31
|
+
class User < ActiveRecord::Base
|
32
|
+
validates_formatting_of :email, :using => :email
|
33
|
+
end
|
34
|
+
|
35
|
+
### URL
|
36
|
+
|
37
|
+
class Sites < ActiveRecord::Base
|
38
|
+
validates_formatting_of :website, :using => :url
|
39
|
+
end
|
40
|
+
|
41
|
+
### Alpha
|
42
|
+
|
43
|
+
class Name < ActiveRecord::Base
|
44
|
+
validates_formatting_of :first_name, :using => :alpha
|
45
|
+
end
|
46
|
+
|
47
|
+
### Alphanumeric
|
48
|
+
|
49
|
+
class Sites < ActiveRecord::Base
|
50
|
+
validates_formatting_of :text, :using => :alphanum
|
51
|
+
end
|
52
|
+
|
53
|
+
### Credit Card (Visa, Mastercard, Discver, and American Express)
|
54
|
+
|
55
|
+
class Purchases < ActiveRecord::Base
|
56
|
+
validates_formatting_of :cc, :using => :credit_card
|
57
|
+
end
|
58
|
+
|
59
|
+
### US Zipcodes
|
60
|
+
|
61
|
+
class Location < ActiveRecord::Base
|
62
|
+
validates_formatting_of :zipcode, :using => :us_zip
|
63
|
+
end
|
64
|
+
|
65
|
+
### US Phone numbers
|
66
|
+
|
67
|
+
class Phones < ActiveRecord::Base
|
68
|
+
validates_formatting_of :phone, :using => :us_phone
|
69
|
+
end
|
70
|
+
|
71
|
+
### IP Address
|
72
|
+
|
73
|
+
class Location < ActiveRecord::Base
|
74
|
+
validates_formatting_of :website, :using => :url
|
75
|
+
end
|
76
|
+
|
77
|
+
# Customizable
|
78
|
+
|
79
|
+
If, for any reason, you want to use your own regex instead of Rail's built-in methods, you can specify what you want to use with the `:regex` option. For example,
|
80
|
+
|
81
|
+
|
82
|
+
class Person < ActiveRecord::Base
|
83
|
+
validates_formatting_of :first_name, :regex => /[A-Z]/i
|
84
|
+
end
|
85
|
+
|
86
|
+
# Development and Contribution
|
87
|
+
|
88
|
+
It is very easy to contribute to this gem. Full documentation to do so will be added in the near future.
|
89
|
+
|
@@ -16,13 +16,13 @@ module ValidatesFormattingOf
|
|
16
16
|
validates_format_of attribute, :with => regex_for_validation, :message => opts[:message]
|
17
17
|
end
|
18
18
|
|
19
|
-
# Actually retrieve the regex to check against
|
20
19
|
def validate_with(method)
|
20
|
+
# Actually retrieve the regex to check against
|
21
21
|
formatting.send(method)
|
22
22
|
end
|
23
23
|
|
24
|
-
# Grab the validating methods
|
25
24
|
def formatting
|
25
|
+
# Grab the validating methods
|
26
26
|
@formatting ||= ValidatingMethods.new
|
27
27
|
end
|
28
28
|
end
|
@@ -81,4 +81,49 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
81
81
|
end
|
82
82
|
|
83
83
|
end
|
84
|
+
describe "us_phone" do
|
85
|
+
|
86
|
+
class IPAddress < SuperModel::Base
|
87
|
+
validates_formatting_of :ip, :using => :ip_address
|
88
|
+
end
|
89
|
+
it "validates that the email provided is valid" do
|
90
|
+
IPAddress.new(:ip => '10.10.10').should_not be_valid
|
91
|
+
IPAddress.new(:ip => '999.10.10.20').should_not be_valid
|
92
|
+
IPAddress.new(:ip => '2222.22.22.22').should_not be_valid
|
93
|
+
IPAddress.new(:ip => '22.2222.22.2').should_not be_valid
|
94
|
+
IPAddress.new(:ip => '127.0.0.1').should be_valid
|
95
|
+
IPAddress.new(:ip => '132.254.111.10').should be_valid
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
describe "us_phone" do
|
100
|
+
|
101
|
+
class IPAddress < SuperModel::Base
|
102
|
+
validates_formatting_of :ip, :using => :ip_address
|
103
|
+
end
|
104
|
+
it "validates that the email provided is valid" do
|
105
|
+
IPAddress.new(:ip => '10.10.10').should_not be_valid
|
106
|
+
IPAddress.new(:ip => '999.10.10.20').should_not be_valid
|
107
|
+
IPAddress.new(:ip => '2222.22.22.22').should_not be_valid
|
108
|
+
IPAddress.new(:ip => '22.2222.22.2').should_not be_valid
|
109
|
+
IPAddress.new(:ip => '127.0.0.1').should be_valid
|
110
|
+
IPAddress.new(:ip => '132.254.111.10').should be_valid
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
# For clarification, NONE of the following numbers are real credit card numbers.
|
115
|
+
# They only match the pattern. These were randomly made for testing.
|
116
|
+
describe "credit_card" do
|
117
|
+
|
118
|
+
class Client < SuperModel::Base
|
119
|
+
validates_formatting_of :cc, :using => :credit_card
|
120
|
+
end
|
121
|
+
it "validates that the email provided is valid" do
|
122
|
+
Client.new(:cc => '4264-2879-1230-0000').should be_valid # Visa style
|
123
|
+
Client.new(:cc => '6011-1111-0000-2391').should be_valid # Discover style
|
124
|
+
Client.new(:cc => '5422434400828888').should be_valid # Mastercard style
|
125
|
+
Client.new(:cc => '1233444444444444').should_not be_valid # fake
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
84
129
|
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.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70104641807780 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70104641807780
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70104641807320 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70104641807320
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: supermodel
|
38
|
-
requirement: &
|
38
|
+
requirement: &70104641806840 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70104641806840
|
47
47
|
description: Common Rails validations for different types of data
|
48
48
|
email:
|
49
49
|
- mbridges.91@gmail.com
|