Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/decanter/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class Error < StandardError; end
class UnhandledKeysError < Error; end
class MissingRequiredInputValue < Error; end
class ParseError < Error; end
class ValueFormatError < Error; end
end
4 changes: 4 additions & 0 deletions lib/decanter/parser/date_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class DateParser < ValueParser
raise Decanter::ParseError.new 'Expects a single value' if val.is_a? Array
next if (val.nil? || val === '')
parse_format = options.fetch(:parse_format, '%m/%d/%Y')
begin
::Date.strptime(val, parse_format)
rescue ArgumentError => e
Comment thread
oroth8 marked this conversation as resolved.
Outdated
raise Decanter::ValueFormatError.new 'invalid Date value for format' if e.message == "invalid date"
Comment thread
oroth8 marked this conversation as resolved.
Outdated
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/decanter/parser/datetime_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class DateTimeParser < ValueParser
raise Decanter::ParseError.new 'Expects a single value' if val.is_a? Array
next if (val.nil? || val === '')
parse_format = options.fetch(:parse_format, '%m/%d/%Y %I:%M:%S %p')
begin
::DateTime.strptime(val, parse_format)
rescue ArgumentError => e
raise Decanter::ValueFormatError.new 'invalid DateTime value for format' if e.message == "invalid date"
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion spec/decanter/parser/date_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
context 'with an invalid date string' do
it 'raises an Argument Error' do
expect { parser.parse(name, '2-21-1990') }
.to raise_error(ArgumentError, 'invalid date')
.to raise_error(Decanter::ValueFormatError, 'invalid Date value for format')
end
end

context 'with a invalid date string and custom format' do
it 'raises an Argument Error' do
expect { parser.parse(name, '2-21-1990', parse_format: '%d-%m-%Y') }
.to raise_error(Decanter::ValueFormatError, 'invalid Date value for format')
end
end

Expand Down
9 changes: 8 additions & 1 deletion spec/decanter/parser/datetime_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
context 'with an invalid date string' do
it 'raises an Argument Error' do
expect { parser.parse(name, '2-21-1990') }
.to raise_error(ArgumentError, 'invalid date')
.to raise_error(Decanter::ValueFormatError, 'invalid DateTime value for format')
end
end

context 'with a invalid date string and custom format' do
it 'raises an Argument Error' do
expect { parser.parse(name, '2-21-1990', parse_format: '%d-%m-%Y') }
.to raise_error(Decanter::ValueFormatError, 'invalid DateTime value for format')
end
end

Expand Down