Skip to content
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#### Fixes

* [#1365](https://github.com/ruby-grape/grape/pull/1365): Fix finding exception handler in error middleware - [@ktimothy](https://github.com/ktimothy).
* [#1380](https://github.com/ruby-grape/grape/pull/1380): `allow_blank: false` for `Time` attribute with valid value causes an error [@ipkes](https://github.com/ipkes).

0.16.2 (4/12/2016)
==================
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/validations/validators/allow_blank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def validate_param!(attr_name, params)

return unless should_validate

return if value == false || value.present?
return if false == value || value.present?
Copy link
Member

Choose a reason for hiding this comment

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

You're such a C programmer ;)


raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:blank)
end
Expand Down
5 changes: 5 additions & 0 deletions spec/grape/validations/validators/allow_blank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@ def app
get '/disallow_boolean_blank', val: false
expect(last_response.status).to eq(200)
end

it 'accepts value when time allow_blank' do
get '/disallow_datetime_blank', val: Time.now
Copy link
Member

Choose a reason for hiding this comment

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

How did this fail before, just for completeness?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here a start of the error stack:

Failure/Error: return if value == false || value.present?
     
     NoMethodError:
       undefined method `to_datetime' for false:FalseClass
     # .../.rvm/gems/ruby-2.3.0/gems/activesupport-3.2.19/lib/active_support/core_ext/date_time/calculations.rb:141:in `<=>'
     # ./lib/grape/validations/validators/allow_blank.rb:24:in `=='
     # ./lib/grape/validations/validators/allow_blank.rb:24:in `validate_param!'

Here 'active_support/core_ext/date_time/calculations.rb:141':

  # Layers additional behavior on DateTime#<=> so that Time and
  # ActiveSupport::TimeWithZone instances can be compared with a DateTime.
  def <=>(other)
    super other.to_datetime
  end

I'm not sure why ActiveSupport is calling (if you have any idea, please share, I'll be very grateful). I think the problem is much deeper, but I'm to lazy to digging it up. So, maybe solution what offered is not a solution, it's looks like hook, but it works fine and looks pretty good =)
This works fine too:

return value.present? || false == value

expect(last_response.status).to eq(200)
end
end

context 'in an optional group' do
Expand Down