format_validators 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,12 @@
1
1
  == unreleased changes
2
2
 
3
+ == 0.0.5
4
+
5
+ * Added a zip code format validator and tests
6
+ * Added a phone format validator and tests
7
+ * Added a days of the week validator
8
+ * Added support for >= rails 3.0.7
9
+
3
10
  == 0.0.4
4
11
 
5
12
  * Added a currency validator
data/Gemfile CHANGED
@@ -3,7 +3,7 @@ source "http://rubygems.org"
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
- gem "rails", "~> 3.0.7"
6
+ gem "rails", ">= 3.0.7"
7
7
 
8
8
  # Add dependencies to develop your gem here.
9
9
  # Include everything needed to run rake, tests, features, etc.
data/Gemfile.lock CHANGED
@@ -143,7 +143,7 @@ DEPENDENCIES
143
143
  bundler (~> 1.0.0)
144
144
  capybara (>= 0.4.0)
145
145
  jeweler (~> 1.6.4)
146
- rails (~> 3.0.7)
146
+ rails (>= 3.0.7)
147
147
  rspec (~> 2.6.0)
148
148
  rspec-rails (~> 2.6.1)
149
149
  ruby-debug19
data/README.rdoc CHANGED
@@ -1,6 +1,49 @@
1
- = format_validators
1
+ == format_validators
2
+
3
+ A consolidation of a few of the most common validations.
4
+
5
+ == What do we validate
6
+
7
+ * Currency
8
+ * Florida Counties (a little too custom but someone requested it so here it is)
9
+ * Phone Numbers
10
+ * Social Security Number
11
+ * ZIP code
12
+
13
+ == Install
14
+
15
+ gem install format_validators
16
+
17
+ == Examples:
18
+
19
+ Like all validations these lines go in the model.
20
+
21
+ === Currency
22
+
23
+ validates :total, :currency_format => true
24
+ validates <field>, :currency_format => true
25
+
26
+ === Florida Counties
27
+
28
+ validates :county, :florida_counties => true
29
+ validates <field>, :florida_counties => true
30
+
31
+ === Phone Numbers
32
+
33
+ validates :phone_number, :phone_format => true
34
+ validates <field>, :phone_format => true
35
+
36
+ === Social Security Number
37
+
38
+ validates :social, :ssn_format => true
39
+ validates <field>, :ssn_format => true
40
+
41
+ === ZIP code
42
+
43
+ validates :zip, :zipcode_format => true
44
+ validates <field>, :zipcode_format => true
45
+
2
46
 
3
- Description goes here.
4
47
 
5
48
  == Contributing to format_validators
6
49
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -0,0 +1,30 @@
1
+ class DaysOfWeekValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ value = value.to_s.downcase
4
+ record.errors[attribute] << value + " is not a valid day of the week or abbreviation" unless DAYS.include?(value)
5
+ end
6
+ DAYS = [
7
+ "monday",
8
+ "mon",
9
+ "m",
10
+ "tuesday",
11
+ "tues",
12
+ "tu",
13
+ "t",
14
+ "wednesday",
15
+ "wed",
16
+ "w",
17
+ "thursday",
18
+ "thurs",
19
+ "th",
20
+ "friday",
21
+ "fri",
22
+ "f",
23
+ "saturday",
24
+ "sat",
25
+ "s",
26
+ "sunday",
27
+ "sun",
28
+ "su"
29
+ ]
30
+ end
@@ -0,0 +1,55 @@
1
+ # Custom Validations for Phone Number format
2
+ #
3
+ # Currently only has support for north american phone numbers. Additional support will be added later
4
+ #
5
+ # e.g. validates :home_phone, :phone_format => true
6
+ # e.g. validates :cell_phone, :phone_format => { :allow_blank => true, :if => :method_returns_true }
7
+ #
8
+ # Validates 7, 10, and 11 digit hone numbers to the NANP standard
9
+ # Handles parentheses, +, - and white space in the phone number
10
+ class PhoneFormatValidator < ActiveModel::EachValidator
11
+
12
+ # validates the phone number
13
+ # defaults to north america region
14
+ #
15
+ # removes parens, dash, space, and plus from numbers
16
+ # checks for non digit characters in the number
17
+ # checks against regional format validator
18
+ # adds errors if necessary
19
+ def validate_each record, attribute, value
20
+ region = options[:region] || :north_america
21
+
22
+ formatted_value = value.gsub(/[\s\.\+\(\)-]*/, "")
23
+ is_number = !(formatted_value =~ /^[-+]?[0-9]+$/).nil? # it returns 0 if it is a number with digits 0-9 or nil if not
24
+ unless is_number && validate_by_region(region, formatted_value)
25
+ message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.'
26
+ record.errors[attribute] << (options[:message] || message )
27
+ end
28
+ end
29
+
30
+ # @param [Symbol] region The phone number region to check against
31
+ # @param [String] formatted_value Formatted phone number, guaranteed to be all digits
32
+ # @return [Boolean] Whether the formatted value passes region validations
33
+ def validate_by_region(region, formatted_value)
34
+ if region == :north_america
35
+ valid_eleven_digit?(formatted_value) || valid_ten_digit?(formatted_value) || valid_seven_digit?(formatted_value)
36
+ else
37
+ false
38
+ end
39
+ end
40
+
41
+ # @return [Boolean] True if it is seven digits long and the first digit is not 1 or 0
42
+ def valid_seven_digit?(input)
43
+ input.length == 7 && input[0] != "0" && input[0] != "1"
44
+ end
45
+
46
+ # @return [Boolean] True if it is 10 digits long, first digit not 1 or 0 and the last 7 digits validate
47
+ def valid_ten_digit?(input)
48
+ input.length == 10 && input[0] != "0" && input[0] != "1" && valid_seven_digit?(input.slice(-7, 7))
49
+ end
50
+
51
+ # @return [Boolean] Same as the ten digit with a 1 in front
52
+ def valid_eleven_digit?(input)
53
+ input.length == 11 && input[0] == "1" && valid_ten_digit?(input.slice(-10, 10))
54
+ end
55
+ end
@@ -0,0 +1,11 @@
1
+ # Custom Validations for Zip Code format
2
+
3
+ # e.g. validates :zip, :zipcode_format => true
4
+ # e.g. validates :zip, :zipcode_format => { :allow_blank => true, :if => :method_returns_true }
5
+ class ZipcodeFormatValidator < ActiveModel::EachValidator
6
+ def validate_each record, attribute, value
7
+ format = /^\A[\d]{5}(?:[-|\s][\d]{4})?\Z$/
8
+ message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.'
9
+ record.errors[attribute] << (options[:message] || message ) unless value =~ format
10
+ end
11
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "format_validators"
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jeremiah Hemphill"]
12
- s.date = "2012-03-21"
12
+ s.date = "2012-04-04"
13
13
  s.description = "Complex format validators"
14
14
  s.email = "jeremiah@cloudspace.com"
15
15
  s.extra_rdoc_files = [
@@ -27,8 +27,11 @@ Gem::Specification.new do |s|
27
27
  "Rakefile",
28
28
  "VERSION",
29
29
  "app/validators/currency_format_validator.rb",
30
+ "app/validators/days_of_week_validator.rb",
30
31
  "app/validators/florida_counties_validator.rb",
32
+ "app/validators/phone_format_validator.rb",
31
33
  "app/validators/ssn_format_validator.rb",
34
+ "app/validators/zipcode_format_validator.rb",
32
35
  "format_validators.gemspec",
33
36
  "lib/format_validators.rb",
34
37
  "lib/format_validators/engine.rb",
@@ -73,9 +76,12 @@ Gem::Specification.new do |s|
73
76
  "spec/spec_helper.rb",
74
77
  "spec/support/basic_record.rb",
75
78
  "spec/validators/currency_format_validator_spec.rb",
79
+ "spec/validators/days_of_week_validator_spec.rb",
76
80
  "spec/validators/florida_counties_integration_spec.rb",
77
81
  "spec/validators/florida_counties_spec.rb",
78
- "spec/validators/ssn_format_validator_spec.rb"
82
+ "spec/validators/phone_format_validator_spec.rb",
83
+ "spec/validators/ssn_format_validator_spec.rb",
84
+ "spec/validators/zipcode_format_validator_spec.rb"
79
85
  ]
80
86
  s.homepage = "http://github.com/jeremiahishere/format_validators"
81
87
  s.licenses = ["MIT"]
@@ -87,7 +93,7 @@ Gem::Specification.new do |s|
87
93
  s.specification_version = 3
88
94
 
89
95
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
90
- s.add_runtime_dependency(%q<rails>, ["~> 3.0.7"])
96
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.7"])
91
97
  s.add_development_dependency(%q<capybara>, [">= 0.4.0"])
92
98
  s.add_development_dependency(%q<sqlite3>, [">= 0"])
93
99
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -100,7 +106,7 @@ Gem::Specification.new do |s|
100
106
  s.add_development_dependency(%q<ZenTest>, [">= 0"])
101
107
  s.add_development_dependency(%q<autotest-rails>, [">= 0"])
102
108
  else
103
- s.add_dependency(%q<rails>, ["~> 3.0.7"])
109
+ s.add_dependency(%q<rails>, [">= 3.0.7"])
104
110
  s.add_dependency(%q<capybara>, [">= 0.4.0"])
105
111
  s.add_dependency(%q<sqlite3>, [">= 0"])
106
112
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -114,7 +120,7 @@ Gem::Specification.new do |s|
114
120
  s.add_dependency(%q<autotest-rails>, [">= 0"])
115
121
  end
116
122
  else
117
- s.add_dependency(%q<rails>, ["~> 3.0.7"])
123
+ s.add_dependency(%q<rails>, [">= 3.0.7"])
118
124
  s.add_dependency(%q<capybara>, [">= 0.4.0"])
119
125
  s.add_dependency(%q<sqlite3>, [">= 0"])
120
126
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe DaysOfWeekValidator do
4
+
5
+ describe ".validate_each" do
6
+ before(:all) do
7
+ @options = {:attributes => {}}
8
+ @validator = DaysOfWeekValidator.new(@options)
9
+ @record = BasicRecord.new(:days)
10
+ end
11
+
12
+ it "should return true for valid day names" do
13
+ @record.errors[:days].should_not_receive("<<")
14
+ @validator.validate_each(@record, :days, "MONDAY")
15
+ end
16
+
17
+ it "should return true for valid day abbreviation" do
18
+ @record.errors[:days].should_not_receive("<<")
19
+ @validator.validate_each(@record, :days, "WED")
20
+ end
21
+
22
+ it "should return true for valid day abbreviation" do
23
+ @record.errors[:days].should_not_receive("<<")
24
+ @validator.validate_each(@record, :days, "TU")
25
+ end
26
+
27
+ it "should return false for invalid days" do
28
+ @record.errors[:days].should_receive("<<")
29
+ @validator.validate_each(@record, :days, "kajhef")
30
+ end
31
+
32
+ it "should return false for invalid days" do
33
+ @record.errors[:days].should_receive("<<")
34
+ @validator.validate_each(@record, :days, "GDHSKFBF HGVHEHJF 5636464 FHFGGG")
35
+ end
36
+
37
+ it "should return false for blank string" do
38
+ @record.errors[:days].should_receive("<<")
39
+ @validator.validate_each(@record, :days, "")
40
+ end
41
+
42
+ it "should return false for nil" do
43
+ @record.errors[:days].should_receive("<<")
44
+ @validator.validate_each(@record, :days, nil)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ describe PhoneFormatValidator do
4
+ before(:each) do
5
+ @options = {:attributes => {}}
6
+ @validator = PhoneFormatValidator.new(@options)
7
+ end
8
+
9
+ describe ".validate_seven_digit?" do
10
+ it "should return false if the length isn't 7" do
11
+ @validator.valid_seven_digit?("4321").should be_false
12
+ end
13
+
14
+ it "should return false if the first digit is 0" do
15
+ @validator.valid_seven_digit?("0123456").should be_false
16
+ end
17
+
18
+ it "should return false if the first digit is 1" do
19
+ @validator.valid_seven_digit?("1234567").should be_false
20
+ end
21
+
22
+ it "should return true if all requirements are met" do
23
+ @validator.valid_seven_digit?("2345678").should be_true
24
+ end
25
+ end
26
+
27
+ describe ".validate_ten_digit?" do
28
+ before(:each) do
29
+ @validator.stub!(:valid_seven_digit?).and_return(true)
30
+ end
31
+
32
+ it "should return false if the length isn't 10" do
33
+ @validator.valid_ten_digit?("4321").should be_false
34
+ end
35
+
36
+ it "should return false if the first digit is 0" do
37
+ @validator.valid_ten_digit?("0123456789").should be_false
38
+ end
39
+
40
+ it "should return false if the first digit is 1" do
41
+ @validator.valid_ten_digit?("1234567890").should be_false
42
+ end
43
+
44
+ it "should return false if it fails seven digit validation" do
45
+ @validator.should_receive(:valid_seven_digit?).and_return(false)
46
+ @validator.valid_ten_digit?("2345678901").should be_false
47
+ end
48
+
49
+ it "should return true if all requirements are met" do
50
+ @validator.valid_ten_digit?("2345678901").should be_true
51
+ end
52
+ end
53
+
54
+ describe ".validate_ten_digit?" do
55
+ before(:each) do
56
+ @validator.stub!(:valid_ten_digit?).and_return(true)
57
+ end
58
+
59
+ it "should return false if the length isn't 11" do
60
+ @validator.valid_eleven_digit?("4321").should be_false
61
+ end
62
+
63
+ it "should return false if the first digit is not 1" do
64
+ @validator.valid_eleven_digit?("22234567890")
65
+ end
66
+
67
+ it "should return false if it fails ten digit validation" do
68
+ @validator.should_receive(:valid_ten_digit?).and_return(false)
69
+ @validator.valid_eleven_digit?("12234567890").should be_false
70
+ end
71
+
72
+ it "should return true if all requirements are met" do
73
+ @validator.valid_eleven_digit?("12234567890").should be_true
74
+ end
75
+ end
76
+
77
+ describe ".validate_by_region" do
78
+ it "it should call the three north american methods" do
79
+ @validator.should_receive(:valid_seven_digit?)
80
+ @validator.should_receive(:valid_ten_digit?)
81
+ @validator.should_receive(:valid_eleven_digit?)
82
+ @validator.validate_by_region(:north_america, "2345678901")
83
+ end
84
+ it "should return false if the region does not match any known region" do
85
+ @validator.validate_by_region(:other_region, "2345678901").should be_false
86
+ end
87
+ end
88
+
89
+ describe ".validate_each" do
90
+ before(:each) do
91
+ @record = BasicRecord.new(:phone)
92
+ end
93
+
94
+ it "should call validate_by_region with north america by default" do
95
+ @validator.should_receive(:validate_by_region).with(:north_america, "123456789")
96
+ @validator.validate_each(@record, :phone, "123-45-6789")
97
+ end
98
+
99
+ it "should not validate_by_region if number is not numeric" do
100
+ @validator.should_not_receive(:validate_by_region)
101
+ @validator.validate_each(@record, :phone, "123-xyz-6789")
102
+ end
103
+
104
+ it "should not add an error due to allowed special characters" do
105
+ @record.errors[:phone].should_not_receive("<<")
106
+ @validator.validate_each(@record, :phone, "()+.- 12345678901")
107
+ end
108
+
109
+ it "should add an error on a bad phone number" do
110
+ @record.errors[:phone].should_receive("<<")
111
+ @validator.validate_each(@record, :phone, "123abc")
112
+ end
113
+
114
+ it "should not add an error on a good phone number" do
115
+ @record.errors[:phone].should_not_receive("<<")
116
+ @validator.validate_each(@record, :phone, "555-555-5555")
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZipcodeFormatValidator do
4
+ describe ".validate_each" do
5
+ before(:each) do
6
+ @options = {:attributes => {}}
7
+ @validator = ZipcodeFormatValidator.new(@options)
8
+ @record = BasicRecord.new(:zip_code)
9
+ end
10
+
11
+ it "should validate 5 digit zip codes" do
12
+ @record.errors[:zip_code].should_not_receive("<<")
13
+ @validator.validate_each(@record, :zip_code, "12345")
14
+ end
15
+
16
+ it "should validate 9 digit zip codes with a space"do
17
+ @record.errors[:zip_code].should_not_receive("<<")
18
+ @validator.validate_each(@record, :zip_code, "12345 6789")
19
+ end
20
+
21
+ it "should validate 9 digit zip codes with a dash"do
22
+ @record.errors[:zip_code].should_not_receive("<<")
23
+ @validator.validate_each(@record, :zip_code, "12345-6789")
24
+ end
25
+
26
+ it "should not validate 9 digit zip codes with nothing between the first 5 and last 4"do
27
+ @record.errors[:zip_code].should_receive("<<")
28
+ @validator.validate_each(@record, :zip_code, "123456789")
29
+ end
30
+
31
+ it "should not validate zip codes of other lengths"do
32
+ @record.errors[:zip_code].should_receive("<<")
33
+ @validator.validate_each(@record, :zip_code, "123456")
34
+ end
35
+
36
+ it "does not have support for canadian zip codes"do
37
+ @record.errors[:zip_code].should_receive("<<")
38
+ @validator.validate_each(@record, :zip_code, "K1A OB1")
39
+ end
40
+ end
41
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: format_validators
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jeremiah Hemphill
@@ -10,14 +10,14 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-03-21 00:00:00 Z
13
+ date: 2012-04-04 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
17
17
  requirement: &id001 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
- - - ~>
20
+ - - ">="
21
21
  - !ruby/object:Gem::Version
22
22
  version: 3.0.7
23
23
  type: :runtime
@@ -164,8 +164,11 @@ files:
164
164
  - Rakefile
165
165
  - VERSION
166
166
  - app/validators/currency_format_validator.rb
167
+ - app/validators/days_of_week_validator.rb
167
168
  - app/validators/florida_counties_validator.rb
169
+ - app/validators/phone_format_validator.rb
168
170
  - app/validators/ssn_format_validator.rb
171
+ - app/validators/zipcode_format_validator.rb
169
172
  - format_validators.gemspec
170
173
  - lib/format_validators.rb
171
174
  - lib/format_validators/engine.rb
@@ -210,9 +213,12 @@ files:
210
213
  - spec/spec_helper.rb
211
214
  - spec/support/basic_record.rb
212
215
  - spec/validators/currency_format_validator_spec.rb
216
+ - spec/validators/days_of_week_validator_spec.rb
213
217
  - spec/validators/florida_counties_integration_spec.rb
214
218
  - spec/validators/florida_counties_spec.rb
219
+ - spec/validators/phone_format_validator_spec.rb
215
220
  - spec/validators/ssn_format_validator_spec.rb
221
+ - spec/validators/zipcode_format_validator_spec.rb
216
222
  homepage: http://github.com/jeremiahishere/format_validators
217
223
  licenses:
218
224
  - MIT
@@ -226,7 +232,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
226
232
  requirements:
227
233
  - - ">="
228
234
  - !ruby/object:Gem::Version
229
- hash: 4008772419598118185
235
+ hash: 2589333216613871779
230
236
  segments:
231
237
  - 0
232
238
  version: "0"