validates_formatting_of 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/validates_formatting_of.rb +0 -2
- data/lib/validates_formatting_of/model_additions.rb +1 -6
- data/lib/validates_formatting_of/validating_methods.rb +61 -52
- data/lib/validates_formatting_of/validation_messages.rb +17 -24
- data/lib/validates_formatting_of/version.rb +1 -1
- data/spec/validates_formatting_of/model_additions_spec.rb +17 -0
- data/validates_formatting_of.gemspec +1 -1
- metadata +34 -15
- data/.rvmrc +0 -55
@@ -37,12 +37,7 @@ module ValidatesFormattingOf
|
|
37
37
|
|
38
38
|
def validate_with(method)
|
39
39
|
# Actually retrieve the regex to check against
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
def formatting
|
44
|
-
# Grab the validating methods
|
45
|
-
@formatting ||= ValidatingMethods.new
|
40
|
+
ValidatingMethods.send(method)
|
46
41
|
end
|
47
42
|
end
|
48
43
|
end
|
@@ -1,68 +1,77 @@
|
|
1
1
|
module ValidatesFormattingOf
|
2
2
|
class ValidatingMethods
|
3
|
+
class << self
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
19
22
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
23
|
+
# No numbers of symbols. allows "-"
|
24
|
+
def alpha
|
25
|
+
%r{\A([^\d\W]|[-])*\Z}
|
26
|
+
end
|
24
27
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
28
|
+
# Letters, numbers, and spaces
|
29
|
+
def alphanum
|
30
|
+
%r{\A[A-Z0-9\s]+\Z}i
|
31
|
+
end
|
29
32
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
# * (###) ###-####
|
40
|
-
# * (###)###-####
|
41
|
-
# * #########
|
42
|
-
# * ### ###-####
|
43
|
-
def us_phone
|
44
|
-
/^(\((\d{3})\)|\d{3})[ |\.|\-]?(\d{3})[ |\.|\-]?(\d{4})$/
|
45
|
-
end
|
38
|
+
# US Zip code. ##### or #####-####
|
39
|
+
def us_zip
|
40
|
+
%r{\A\d{5}(-\d{4})?\Z}
|
41
|
+
end
|
46
42
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
51
54
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
55
|
+
# IP Address validation
|
56
|
+
def ip_address
|
57
|
+
%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}
|
58
|
+
end
|
56
59
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
60
|
+
# Social Security Number pattern
|
61
|
+
def ssn
|
62
|
+
%r{\A\d{3}([-.]){1}\d{2}([-.]){1}\d{4}\Z}
|
63
|
+
end
|
61
64
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
65
|
+
# Matches CSS colors either in three or six digit formats
|
66
|
+
def hex_color
|
67
|
+
%r{\A([A-F0-9]{6}|[A-F0-9]{3})\Z}i
|
68
|
+
end
|
69
|
+
|
70
|
+
# Ensures that what value is passed is a dollar amount of some kind
|
71
|
+
def dollars
|
72
|
+
%r{\A\$?(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?\Z}
|
73
|
+
end
|
66
74
|
|
75
|
+
end
|
67
76
|
end
|
68
77
|
end
|
@@ -1,30 +1,23 @@
|
|
1
1
|
module ValidatesFormattingOf
|
2
|
-
|
2
|
+
class ValidationMessages
|
3
3
|
|
4
|
-
|
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
|
+
}
|
5
18
|
|
6
|
-
|
7
|
-
|
8
|
-
:email => "is not a valid email",
|
9
|
-
:url => "is not a valid URL",
|
10
|
-
:alpha => "must be only letters or dashes",
|
11
|
-
:alphanum => "must be letters, numbers",
|
12
|
-
:credit_card => "is not a valid credit card number",
|
13
|
-
:us_zip => "is not a valid zipcode",
|
14
|
-
:us_phone => "is not a valid phone number",
|
15
|
-
:ip_address => "is not a valid IP address",
|
16
|
-
:ssn => "is not a valid social security number",
|
17
|
-
:hex_color => "is not a valid hex color",
|
18
|
-
:dollars => "is not a valid dollar amount"
|
19
|
-
}
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
extend self
|
25
|
-
|
26
|
-
def message(message)
|
27
|
-
Messages.hash[message.to_s.downcase.to_sym]
|
19
|
+
def self.message(message)
|
20
|
+
Messages.fetch(message.to_s.downcase.to_sym, "is not valid")
|
28
21
|
end
|
29
22
|
|
30
23
|
end
|
@@ -20,6 +20,23 @@ describe ValidatesFormattingOf::ModelAdditions do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
describe "simple email for 1.8.7 and javascript validations (such as with client_side_validations)" do
|
24
|
+
class SimpleEmail < SuperModel::Base
|
25
|
+
validates_formatting_of :email, :using => :simple_email
|
26
|
+
end
|
27
|
+
it "validates that the email provided is valid" do
|
28
|
+
Email.new(:email => "example@example.com").should be_valid
|
29
|
+
Email.new(:email => "badexample.com").should_not be_valid
|
30
|
+
Email.new(:email => "mbridges.91@gmail.com").should be_valid
|
31
|
+
Email.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").should be_valid
|
32
|
+
Email.new(:email => "this__???{}|__should@be-valid.com").should be_valid
|
33
|
+
Email.new(:email => "visitorservices@vmfa.museum").should be_valid
|
34
|
+
Email.new(:email => "info@samoa.travel").should be_valid
|
35
|
+
Email.new(:email => "info@-samoa.travel").should_not be_valid
|
36
|
+
Email.new(:email => "info@samoa-.travel").should_not be_valid
|
37
|
+
Email.new(:email => "info@123-samoa.travel").should be_valid
|
38
|
+
end
|
39
|
+
end
|
23
40
|
describe "url" do
|
24
41
|
class Webpage < SuperModel::Base
|
25
42
|
validates_formatting_of :url, :using => :url
|
@@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = ValidatesFormattingOf::VERSION
|
17
17
|
|
18
|
-
gem.add_dependency "
|
18
|
+
gem.add_dependency "activerecord", "~> 3.0"
|
19
19
|
|
20
20
|
gem.add_development_dependency "rake"
|
21
21
|
gem.add_development_dependency "rspec"
|
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.6.0
|
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: 2012-
|
12
|
+
date: 2012-07-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement:
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rake
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: rspec
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: '0'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: supermodel
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ! '>='
|
@@ -54,7 +69,12 @@ dependencies:
|
|
54
69
|
version: '0'
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
58
78
|
description: Common Rails validations wrapped in a gem.
|
59
79
|
email:
|
60
80
|
- mbridges.91@gmail.com
|
@@ -64,7 +84,6 @@ extra_rdoc_files: []
|
|
64
84
|
files:
|
65
85
|
- .gitignore
|
66
86
|
- .rspec
|
67
|
-
- .rvmrc
|
68
87
|
- .travis.yml
|
69
88
|
- CHANGELOG.md
|
70
89
|
- Gemfile
|
@@ -94,7 +113,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
113
|
version: '0'
|
95
114
|
segments:
|
96
115
|
- 0
|
97
|
-
hash: -
|
116
|
+
hash: -3727749513764569067
|
98
117
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
118
|
none: false
|
100
119
|
requirements:
|
@@ -103,10 +122,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
122
|
version: '0'
|
104
123
|
segments:
|
105
124
|
- 0
|
106
|
-
hash: -
|
125
|
+
hash: -3727749513764569067
|
107
126
|
requirements: []
|
108
127
|
rubyforge_project:
|
109
|
-
rubygems_version: 1.8.
|
128
|
+
rubygems_version: 1.8.23
|
110
129
|
signing_key:
|
111
130
|
specification_version: 3
|
112
131
|
summary: Adds several convenient methods to validate things such as emails, urls,
|
data/.rvmrc
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
#!/usr/bin/env bash
|
2
|
-
|
3
|
-
# This is an RVM Project .rvmrc file, used to automatically load the ruby
|
4
|
-
# development environment upon cd'ing into the directory
|
5
|
-
|
6
|
-
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
|
7
|
-
environment_id="ruby-1.9.3-p0@validates_formatting_of"
|
8
|
-
|
9
|
-
#
|
10
|
-
# Uncomment following line if you want options to be set only for given project.
|
11
|
-
#
|
12
|
-
# PROJECT_JRUBY_OPTS=( --1.9 )
|
13
|
-
|
14
|
-
#
|
15
|
-
# First we attempt to load the desired environment directly from the environment
|
16
|
-
# file. This is very fast and efficient compared to running through the entire
|
17
|
-
# CLI and selector. If you want feedback on which environment was used then
|
18
|
-
# insert the word 'use' after --create as this triggers verbose mode.
|
19
|
-
#
|
20
|
-
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
|
21
|
-
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
|
22
|
-
then
|
23
|
-
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
|
24
|
-
|
25
|
-
if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
|
26
|
-
then
|
27
|
-
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
|
28
|
-
fi
|
29
|
-
else
|
30
|
-
# If the environment file has not yet been created, use the RVM CLI to select.
|
31
|
-
if ! rvm --create "$environment_id"
|
32
|
-
then
|
33
|
-
echo "Failed to create RVM environment '${environment_id}'."
|
34
|
-
exit 1
|
35
|
-
fi
|
36
|
-
fi
|
37
|
-
|
38
|
-
#
|
39
|
-
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
|
40
|
-
# it be automatically loaded. Uncomment the following and adjust the filename if
|
41
|
-
# necessary.
|
42
|
-
#
|
43
|
-
# filename=".gems"
|
44
|
-
# if [[ -s "$filename" ]]
|
45
|
-
# then
|
46
|
-
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
|
47
|
-
# fi
|
48
|
-
|
49
|
-
# If you use bundler, this might be useful to you:
|
50
|
-
# if command -v bundle && [[ -s Gemfile ]]
|
51
|
-
# then
|
52
|
-
# bundle install
|
53
|
-
# fi
|
54
|
-
|
55
|
-
|