Skip to content

Commit

Permalink
Prevent parsing of empty string for Date & DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Moy committed Feb 15, 2017
1 parent b3ddbbd commit 6bab0b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/happymapper/supported_types.rb
Expand Up @@ -110,11 +110,11 @@ def apply(value)
end

register_type DateTime do |value|
DateTime.parse(value.to_s) if value
DateTime.parse(value.to_s) if value && !value.empty?
end

register_type Date do |value|
Date.parse(value.to_s) if value
Date.parse(value.to_s) if value && !value.empty?
end

register_type Boolean do |value|
Expand Down
11 changes: 11 additions & 0 deletions spec/happymapper/item_spec.rb
Expand Up @@ -107,6 +107,11 @@ class Bar; end
item.typecast(nil).should == nil
end

it "should handle empty string Dates" do
item = HappyMapper::Item.new(:foo, Date)
item.typecast("").should == nil
end

it "should work with DateTimes" do
item = HappyMapper::Item.new(:foo, DateTime)
item.typecast('2000-01-01 00:00:00').should == DateTime.new(2000, 1, 1, 0, 0, 0)
Expand All @@ -117,6 +122,12 @@ class Bar; end
item.typecast(nil).should == nil
end

it "should handle empty string DateTimes" do
item = HappyMapper::Item.new(:foo, DateTime)
item.typecast("").should == nil
end


it "should work with Boolean" do
item = HappyMapper::Item.new(:foo, HappyMapper::Boolean)
item.typecast('false').should == false
Expand Down

0 comments on commit 6bab0b0

Please sign in to comment.