Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
decanter (5.0.0)
decanter (5.1.0)
actionpack (>= 7.1.3.2)
activesupport
rails (>= 7.1.3.2)
Expand Down
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
15 changes: 11 additions & 4 deletions lib/decanter/parser/date_parser.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
module Decanter
module Parser
class DateParser < ValueParser

allow Date

parser do |val, options|
next if (val.nil? || val === '')
raise Decanter::ParseError, 'Expects a single value' if val.is_a? Array
next if val.nil? || val === ''
parse_format = options.fetch(:parse_format, '%m/%d/%Y')
::Date.strptime(val, parse_format)
begin
::Date.strptime(val, parse_format)
rescue Date::Error => e
if e.message == 'invalid date'
raise Decanter::ValueFormatError, 'invalid Date value for format'
else
raise Decanter::ValueFormatError, e.message
end
end
end
end
end
end

12 changes: 9 additions & 3 deletions lib/decanter/parser/datetime_parser.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
module Decanter
module Parser
class DateTimeParser < ValueParser

allow DateTime

parser do |val, options|
next if (val.nil? || val === '')
raise Decanter::ParseError, 'Expects a single value' if val.is_a? Array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line can be removed since this parser inherits from ValueParser, which raises an error if the value is an array

next if val.nil? || val === ''
parse_format = options.fetch(:parse_format, '%m/%d/%Y %I:%M:%S %p')
::DateTime.strptime(val, parse_format)
begin
::DateTime.strptime(val, parse_format)
rescue ArgumentError => e
raise Decanter::ValueFormatError, 'invalid DateTime value for format' if e.message == 'invalid date'

raise ArgumentError, e.message
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/decanter/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Decanter
VERSION = '5.0.0'.freeze
VERSION = '5.1.0'.freeze
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest from main may need to be pulled in here - we're at 5.1.0 now so this can be bumped up to 5.2.0

end
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