Closed
Description
strange thing.
in my params block
params do
optional :date_range, date_range_proper_order: true, type: Hash do
requires :from, type: Integer # epoch
requires :to, type: Integer # epoch
end
end
post 'filtered_customer_groups' do
filtered_customer_group_service = FilteredCustomerGroupsService.new(params)
FilteredCustomerGroupsSerializer.new(filtered_customer_group_service).as_json
end
I'm trying to use custom validator date_range_proper_order
in order to validate following JSON:
"date_range": {
"from": 14123244920000,
"to": 14137932920000
}
so the from
wouldn't be lower than to
.
Here's the validators code
class DateRangeProperOrder < Grape::Validations::Validator
def validate_param!(attr_name, params)
unless params[attr_name][:from] <= params[attr_name][:to]
# Grape::API.logger.info params.inspect
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "'from' must be lower or equal to 'to'"
end
end
end
That works as expected. However, if I change optional
into requires
for :date_range
, the Grape won't run and it will throw
ArgumentError: ArgumentError
new_scope at /Users/michalsiemionczyk/.rvm/gems/jruby-1.7.15/gems/grape-0.9.0/lib/grape/validations.rb:149
requires at /Users/michalsiemionczyk/.rvm/gems/jruby-1.7.15/gems/grape-0.9.0/lib/grape/dsl/parameters.rb:31
FilteredCustomerGroups at /Users/michalsiemionczyk/Projects/yeti-backend/app/filtered_customer_groups.rb:22
Which points directly to the optional
and requires
line of code.
Why is that? Why cannot I use custom validator for required paramer (a Hash?)
I've created the issue on StackOverflow (http://bit.ly/1oPHxQX) but no help frmo there so far, so I published it in here