Skip to content

Commit

Permalink
Now also dates written in US long format are recognized
Browse files Browse the repository at this point in the history
  • Loading branch information
thesp0nge committed Feb 17, 2011
1 parent a235b60 commit e94907d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/validator/date.rb
Expand Up @@ -7,6 +7,7 @@ class Date < GenericValidator

US_FORMAT_NUMERIC = "^\\d{2}[/-]\\d{2}[/-]\\d{4}$"
US_FORMAT_STRING_SHORT = "^\\w{3} \\d{2}[,][ ]\\d{4}"
US_FORMAT_STRING_LONG = "^\\w+ \\d{2}[,][ ]\\d{4}"

def initialize(options=nil)
@matcher = ""
Expand All @@ -22,7 +23,7 @@ def valid?(date)
# let's see if this a meaningful date.
return (is_valid_month?(s[0].to_i) && is_valid_day?(s[1].to_i, s[0].to_i, s[2].to_i) && is_valid_year?(s[2].to_i))
end
if @matcher == US_FORMAT_STRING_SHORT
if @matcher == US_FORMAT_STRING_SHORT || @matcher == US_FORMAT_STRING_LONG
s = date.gsub(',', '').split(' ')
# I'm pretty sure about what's inside the array due to regular expression check.
# s[0] is the month here, written in short alphanumeric form
Expand All @@ -48,9 +49,9 @@ def month_to_digit(m)
r = 4
when 'may'
r = 5
when 'june'
when 'jun', 'june'
r = 6
when 'july'
when 'jul', 'july'
r = 7
when 'aug', 'august'
r = 8
Expand Down
46 changes: 46 additions & 0 deletions spec/owasp_esapi_ruby_date_validator_spec.rb
Expand Up @@ -80,6 +80,52 @@ module Validator
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_SHORT
validator.valid?("Jan 32, 2011").should == false
end

it "should validate a good date (US Format)" do
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_LONG
validator.valid?("January 15, 2011").should == true
end

it "should discard a bad date (US Format)" do
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_LONG
validator.valid?("January 15 2011").should == false
end

it "should discard a bad date (US Format)" do
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_LONG
validator.valid?("January, 15 2011").should == false
end

it "should discard a bad date (US Format)" do
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_LONG
validator.valid?("January a, 2011").should == false
end

it "should discard a bad date (US Format)" do
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_LONG
validator.valid?("January 32, 2011").should == false
end

it "should validate a good date (US Format)" do
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_SHORT
validator.valid?("Feb, 29 2012").should == false
end

it "should discard a bad date (US Format)" do
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_SHORT
validator.valid?("Feb, 29 2011").should == false
end

it "should validate a good date (US Format)" do
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_LONG
validator.valid?("February, 29 2012").should == false
end

it "should discard a bad date (US Format)" do
validator.matcher=Owasp::Esapi::Validator::Date::US_FORMAT_STRING_LONG
validator.valid?("February, 29 2011").should == false
end

end
end
end
Expand Down

0 comments on commit e94907d

Please sign in to comment.