validates_formatting_of 0.2.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/.gitignore +17 -0
- data/.rspec +1 -0
- data/.rvmrc +55 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +7 -0
- data/README.markdown +36 -0
- data/Rakefile +6 -0
- data/lib/validates_formatting_of/model_additions.rb +29 -0
- data/lib/validates_formatting_of/railtie.rb +10 -0
- data/lib/validates_formatting_of/validating_methods.rb +44 -0
- data/lib/validates_formatting_of/version.rb +3 -0
- data/lib/validates_formatting_of.rb +6 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/validates_formatting_of/model_additions_spec.rb +84 -0
- data/validates_formatting_of.gemspec +21 -0
- metadata +97 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1,55 @@
|
|
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
|
+
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Matt Bridges
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# validates_formatting_of
|
2
|
+
|
3
|
+
The *validates_formatting_of" gem addess several convenient methods to validating things such as emails, urls, and phones in a Rails application.
|
4
|
+
|
5
|
+
# Installation
|
6
|
+
|
7
|
+
To install *validates_formatting_of*, add the following to your `Gemfile`:
|
8
|
+
|
9
|
+
gem 'validates_formatting_of'
|
10
|
+
|
11
|
+
Then bundle install:
|
12
|
+
|
13
|
+
bundle install
|
14
|
+
|
15
|
+
# Usage
|
16
|
+
|
17
|
+
Using validates_formatting_of is as simple as using Rails' built-in validation methods in models.
|
18
|
+
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
validates_formatting_of :email, :using => :email
|
21
|
+
end
|
22
|
+
|
23
|
+
This call will ensure that the user-provided email is a valid email. This way, you will not need to find or write your own regex to validate. All of that logic is contained within `validates_formatting_of`
|
24
|
+
|
25
|
+
# Available Formatting Validations
|
26
|
+
|
27
|
+
`validates_formatting_of` has support for the following validations:
|
28
|
+
|
29
|
+
* Email
|
30
|
+
* URL
|
31
|
+
* Alpha-only
|
32
|
+
* Alphanumeric
|
33
|
+
* Credit Card (Visa, Mastercard, Discver, and American Express)
|
34
|
+
* US Zipcodes
|
35
|
+
* US Phone numbers
|
36
|
+
* IP Address
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module ValidatesFormattingOf
|
2
|
+
module ModelAdditions
|
3
|
+
def validates_formatting_of(attribute, opts = {})
|
4
|
+
|
5
|
+
# Here is where the input is formatted before it is validated.
|
6
|
+
# Not yet implemented.
|
7
|
+
#
|
8
|
+
# unless opts[:preformat].nil?
|
9
|
+
# before_validation do
|
10
|
+
# # add formatting changes here
|
11
|
+
# end
|
12
|
+
# end
|
13
|
+
|
14
|
+
regex_for_validation = opts[:regex] || validate_with(opts[:using])
|
15
|
+
|
16
|
+
validates_format_of attribute, :with => regex_for_validation, :message => opts[:message]
|
17
|
+
end
|
18
|
+
|
19
|
+
# Actually retrieve the regex to check against
|
20
|
+
def validate_with(method)
|
21
|
+
formatting.send(method)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Grab the validating methods
|
25
|
+
def formatting
|
26
|
+
@formatting ||= ValidatingMethods.new
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module ValidatesFormattingOf
|
2
|
+
class ValidatingMethods
|
3
|
+
|
4
|
+
# This method is very close to allowing what is specified in RFC 5322 and RFC 5321
|
5
|
+
def email
|
6
|
+
/^[0-9A-Z!#$\%&'*+-\/=?^_`{|}~]+@[0-9A-Z\-.]+\.[A-Z]{2,4}$/i
|
7
|
+
end
|
8
|
+
|
9
|
+
# Taken from Ryan Bates' screencast on extracting gems. Works extremely well. Thanks Ryan!
|
10
|
+
def url
|
11
|
+
/^https?:\/\/([^\s:@]+:[^\s:@]*@)?[-[[:alnum:]]]+(\.[-[[:alnum:]]]+)+\.?(:\d{1,5})?([\/?]\S*)?$/iux
|
12
|
+
end
|
13
|
+
|
14
|
+
# No numbers of symbols. allows "-"
|
15
|
+
def alpha
|
16
|
+
/^([^\d\W]|[-])*$/
|
17
|
+
end
|
18
|
+
|
19
|
+
# Letters, numbers, and spaces
|
20
|
+
def alphanum
|
21
|
+
/^[A-Z0-9\s]+$/i
|
22
|
+
end
|
23
|
+
|
24
|
+
# Visa, Mastercard, Discver, and American Express
|
25
|
+
def credit_card
|
26
|
+
/^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/
|
27
|
+
end
|
28
|
+
|
29
|
+
# US Zip code. ##### or #####-####
|
30
|
+
def us_zip
|
31
|
+
/^\d{5}(-\d{4})?$/
|
32
|
+
end
|
33
|
+
|
34
|
+
# US Phone numbers. (###) ###-#### | (###)###-#### | ###-###-####
|
35
|
+
def us_phone
|
36
|
+
/^([\(]{1}[0-9]{3}[\)]{1}[ |\-]{0,1}|^[0-9]{3}[\-| ])?[0-9]{3}(\-| ){1}[0-9]{4}$/
|
37
|
+
end
|
38
|
+
|
39
|
+
def ip_address
|
40
|
+
/^(\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])$/
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
# This file does all of the preloading. Little to no actual code should go in this file.
|
2
|
+
|
3
|
+
require "validates_formatting_of/version"
|
4
|
+
require "validates_formatting_of/validating_methods"
|
5
|
+
require "validates_formatting_of/model_additions"
|
6
|
+
require "validates_formatting_of/railtie" if defined? Rails
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidatesFormattingOf::ModelAdditions do
|
4
|
+
|
5
|
+
describe "email" do
|
6
|
+
|
7
|
+
class Email < SuperModel::Base
|
8
|
+
validates_formatting_of :email, :using => :email
|
9
|
+
end
|
10
|
+
it "validates that the email provided is valid" do
|
11
|
+
Email.new(:email => "example@example.com").should be_valid
|
12
|
+
Email.new(:email => "badexample.com").should_not be_valid
|
13
|
+
Email.new(:email => "mbridges.91@gmail.com").should be_valid
|
14
|
+
Email.new(:email => "some-random%%%strangely-formatted-email@lots.of.subdomains.com").should be_valid
|
15
|
+
Email.new(:email => "this__???{}|__should@be-valid.com").should be_valid
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
describe "url" do
|
20
|
+
|
21
|
+
class Webpage < SuperModel::Base
|
22
|
+
validates_formatting_of :url, :using => :url
|
23
|
+
end
|
24
|
+
it "validates that the email provided is valid" do
|
25
|
+
Webpage.new(:url => 'http://something.com').should be_valid
|
26
|
+
Webpage.new(:url => "something else").should_not be_valid
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
describe "us_zip" do
|
31
|
+
|
32
|
+
class USZip < SuperModel::Base
|
33
|
+
validates_formatting_of :zipcode, :using => :us_zip
|
34
|
+
end
|
35
|
+
it "validates that the email provided is valid" do
|
36
|
+
USZip.new(:zipcode => '92348').should be_valid
|
37
|
+
USZip.new(:zipcode => '23434-2348').should be_valid
|
38
|
+
USZip.new(:zipcode => '234').should_not be_valid
|
39
|
+
USZip.new(:zipcode => '23408234').should_not be_valid
|
40
|
+
USZip.new(:zipcode => 'invalid').should_not be_valid
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
describe "alpha" do
|
45
|
+
|
46
|
+
class Alpha < SuperModel::Base
|
47
|
+
validates_formatting_of :letters, :using => :alpha
|
48
|
+
end
|
49
|
+
it "validates that the email provided is valid" do
|
50
|
+
Alpha.new(:letters => 'abscdsofjsdpfahdsofkajlsdfaspdhjfads').should be_valid
|
51
|
+
Alpha.new(:letters => 'asdfalskdfjhas-dlfhasdksdfaldhfadsfasdfa').should be_valid
|
52
|
+
Alpha.new(:letters => 'adsufasodfksadjfskjdfha98').should_not be_valid
|
53
|
+
Alpha.new(:letters => 'asdf ausdpf98hasdfo alsdf ja8 sd').should_not be_valid
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
describe "alphanum" do
|
58
|
+
|
59
|
+
class Alphanum < SuperModel::Base
|
60
|
+
validates_formatting_of :letters_and_numbers, :using => :alphanum
|
61
|
+
end
|
62
|
+
it "validates that the email provided is valid" do
|
63
|
+
Alphanum.new(:letters_and_numbers => 'numbersandlettersarevalid1234567890').should be_valid
|
64
|
+
Alphanum.new(:letters_and_numbers => 'justletters').should be_valid
|
65
|
+
Alphanum.new(:letters_and_numbers => 'letters and numbers 123 with spaces').should be_valid
|
66
|
+
Alphanum.new(:letters_and_numbers => 'adding ; some special ** chars').should_not be_valid
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
describe "us_phone" do
|
71
|
+
|
72
|
+
class USPhone < SuperModel::Base
|
73
|
+
validates_formatting_of :phone_number, :using => :us_phone
|
74
|
+
end
|
75
|
+
it "validates that the email provided is valid" do
|
76
|
+
USPhone.new(:phone_number => '(234) 234-3456').should be_valid
|
77
|
+
USPhone.new(:phone_number => '123 123 3456').should be_valid
|
78
|
+
USPhone.new(:phone_number => '(223)123-2347').should be_valid
|
79
|
+
USPhone.new(:phone_number => '2349870238').should_not be_valid
|
80
|
+
USPhone.new(:phone_number => '12349870238').should_not be_valid
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/validates_formatting_of/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Matt Bridges"]
|
6
|
+
gem.email = ["mbridges.91@gmail.com"]
|
7
|
+
gem.description = %q{Common Rails validations for different types of data}
|
8
|
+
gem.summary = %q{Adds common data validations to save development time. (i.e. phone and url)}
|
9
|
+
gem.homepage = "https://github.com/mattdbridges/validates_formatting_of"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "validates_formatting_of"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ValidatesFormattingOf::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rake"
|
19
|
+
gem.add_development_dependency "rspec"
|
20
|
+
gem.add_development_dependency "supermodel"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validates_formatting_of
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matt Bridges
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70227849493380 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70227849493380
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70227849492920 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70227849492920
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: supermodel
|
38
|
+
requirement: &70227849492420 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70227849492420
|
47
|
+
description: Common Rails validations for different types of data
|
48
|
+
email:
|
49
|
+
- mbridges.91@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rspec
|
56
|
+
- .rvmrc
|
57
|
+
- .travis.yml
|
58
|
+
- CHANGELOG.md
|
59
|
+
- Gemfile
|
60
|
+
- LICENSE.txt
|
61
|
+
- README.markdown
|
62
|
+
- Rakefile
|
63
|
+
- lib/validates_formatting_of.rb
|
64
|
+
- lib/validates_formatting_of/model_additions.rb
|
65
|
+
- lib/validates_formatting_of/railtie.rb
|
66
|
+
- lib/validates_formatting_of/validating_methods.rb
|
67
|
+
- lib/validates_formatting_of/version.rb
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
- spec/validates_formatting_of/model_additions_spec.rb
|
70
|
+
- validates_formatting_of.gemspec
|
71
|
+
homepage: https://github.com/mattdbridges/validates_formatting_of
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.10
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Adds common data validations to save development time. (i.e. phone and url)
|
95
|
+
test_files:
|
96
|
+
- spec/spec_helper.rb
|
97
|
+
- spec/validates_formatting_of/model_additions_spec.rb
|