Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot write custom validator for required parameter #778

Closed
siemionides opened this issue Oct 8, 2014 · 4 comments
Closed

cannot write custom validator for required parameter #778

siemionides opened this issue Oct 8, 2014 · 4 comments
Labels

Comments

@siemionides
Copy link

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

@dblock
Copy link
Member

dblock commented Oct 9, 2014

This smells like a bug. Would be helpful if you could turn this into an RSpec spec in a pull request, please.

@dblock dblock added the bug? label Oct 9, 2014
@dm1try
Copy link
Member

dm1try commented Oct 13, 2014

Here some specs - https://github.com/intridea/grape/blob/master/spec/grape/validations_spec.rb#L230
https://github.com/intridea/grape/blob/master/spec/grape/validations_spec.rb#L284

/cc @bwalex
Why @siemionides cannot use custom validator for required paramer with Hash type?)
Thanks!

PS if we'll skip this condition(single argument) all specs are green.

bwalex added a commit to bwalex/grape that referenced this issue Nov 2, 2014
 * I'm not sure what I was thinking originally, but this obviously
   breaks the use of custom validators.

 * Add a spec for ruby-grape#778 whilst on it.
bwalex added a commit to bwalex/grape that referenced this issue Nov 2, 2014
dblock added a commit that referenced this issue Nov 2, 2014
Address #778 - don't restrict options to Hash/Array types
@bwalex
Copy link
Contributor

bwalex commented Nov 2, 2014

@siemionides things should work now that #799 has been merged. Give that a spin and let us know if it works for you.

@dblock
Copy link
Member

dblock commented Nov 28, 2014

I believe this is fixed, closing.

@dblock dblock closed this as completed Nov 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants