Skip to content

Commit

Permalink
Added zip code and phone format validators.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiahishere committed Mar 28, 2012
1 parent bdd8f78 commit 69183a9
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
@@ -1,5 +1,8 @@
== unreleased changes

* Added a zip code format validator and tests
* Added a phone format validator and tests

== 0.0.4

* Added a currency validator
Expand Down
11 changes: 11 additions & 0 deletions app/validators/phone_format_validator.rb
@@ -0,0 +1,11 @@
# Custom Validations for Phone Number format

# e.g. validates :home_phone, :phone_format => true
# e.g. validates :cell_phone, :phone_format => { :allow_blank => true, :if => :method_returns_true }
class PhoneFormatValidator < ActiveModel::EachValidator
def validate_each record, attribute, value
format = /^1?[-\. ]?(\(\d{3}\)?[-\. ]?|\d{3}?[-\. ]?)?\d{3}?[-\. ]?\d{4}$/
message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.'
record.errors[attribute] << (options[:message] || message ) unless value =~ format
end
end
11 changes: 11 additions & 0 deletions app/validators/zipcode_format_validator.rb
@@ -0,0 +1,11 @@
# Custom Validations for Zip Code format

# e.g. validates :zip, :zipcode_format => true
# e.g. validates :zip, :zipcode_format => { :allow_blank => true, :if => :method_returns_true }
class ZipcodeFormatValidator < ActiveModel::EachValidator
def validate_each record, attribute, value
format = /^\A[\d]{5}(?:[-|\s][\d]{4})?\Z$/
message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.'
record.errors[attribute] << (options[:message] || message ) unless value =~ format
end
end
52 changes: 52 additions & 0 deletions spec/validators/phone_format_validator_spec.rb
@@ -0,0 +1,52 @@
require 'spec_helper'

describe PhoneFormatValidator do
describe ".validate_each" do
before(:each) do
@options = {:attributes => {}}
@validator = PhoneFormatValidator.new(@options)
@record = BasicRecord.new(:phone)
end

it "should validate 7 digit phone number" do
@record.errors[:phone].should_not_receive("<<")
@validator.validate_each(@record, :phone, "123-4567")
end

it "should validate 10 digit phone number without area code parentheses" do
@record.errors[:phone].should_not_receive("<<")
@validator.validate_each(@record, :phone, "321-123-4567")
end

it "should validate 10 digit phone number with area code parentheses" do
@record.errors[:phone].should_not_receive("<<")
@validator.validate_each(@record, :phone, "(321) 123-4567")
end

it "should validate 10 digit phone number with a 1 first" do
@record.errors[:phone].should_not_receive("<<")
@validator.validate_each(@record, :phone, "1-321-123-4567")
end

it "should allow dashes between numbers" do
@record.errors[:phone].should_not_receive("<<")
@validator.validate_each(@record, :phone, "1-321-123-4567")
end

it "should allow a single space between numbers" do
@record.errors[:phone].should_not_receive("<<")
@validator.validate_each(@record, :phone, "1 321 123 4567")
end

it "should allow multiple whitespace characters between numbers" do
@record.errors[:phone].should_not_receive("<<")
@validator.validate_each(@record, :phone, "123 4567")
end

it "should not validate 5 digit phone number" do
@record.errors[:phone].should_receive("<<")
@validator.validate_each(@record, :phone, "3-4567")
end

end
end
41 changes: 41 additions & 0 deletions spec/validators/zipcode_format_validator_spec.rb
@@ -0,0 +1,41 @@
require 'spec_helper'

describe ZipcodeFormatValidator do
describe ".validate_each" do
before(:each) do
@options = {:attributes => {}}
@validator = ZipcodeFormatValidator.new(@options)
@record = BasicRecord.new(:zip_code)
end

it "should validate 5 digit zip codes" do
@record.errors[:zip_code].should_not_receive("<<")
@validator.validate_each(@record, :zip_code, "12345")
end

it "should validate 9 digit zip codes with a space"do
@record.errors[:zip_code].should_not_receive("<<")
@validator.validate_each(@record, :zip_code, "12345 6789")
end

it "should validate 9 digit zip codes with a dash"do
@record.errors[:zip_code].should_not_receive("<<")
@validator.validate_each(@record, :zip_code, "12345-6789")
end

it "should not validate 9 digit zip codes with nothing between the first 5 and last 4"do
@record.errors[:zip_code].should_receive("<<")
@validator.validate_each(@record, :zip_code, "123456789")
end

it "should not validate zip codes of other lengths"do
@record.errors[:zip_code].should_receive("<<")
@validator.validate_each(@record, :zip_code, "123456")
end

it "does not have support for canadian zip codes"do
@record.errors[:zip_code].should_receive("<<")
@validator.validate_each(@record, :zip_code, "K1A OB1")
end
end
end

0 comments on commit 69183a9

Please sign in to comment.