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

requires doc_attrs and a block #751

Open
engineersmnky opened this issue Sep 10, 2014 · 0 comments
Open

requires doc_attrs and a block #751

engineersmnky opened this issue Sep 10, 2014 · 0 comments

Comments

@engineersmnky
Copy link

I was trying to write something along the lines of

params do 
  requires :request, type:Hash, desc: 'Request Wrapper required for all API requests', documentation:{example: 'request:{}'} do 
    requires :dealer_id, type:Integer,default: 0, desc: 'Dealer Id Number', documentation:{example: 100001}
  end
end

Which seemed like it should work appropriately as I need to document the wrapper as well as the parameters inside it but I was receiving an standard ArgumentError. So I started digging through the source and traced it all down to Grape::Validations::ParamsScope#new_scope

def new_scope(attrs, optional = false, &block)
  opts = attrs[1] || { type: Array }
  raise ArgumentError unless opts.keys.to_set.subset? [:type].to_set
  ParamsScope.new(api: @api, element: attrs.first, parent: self, optional: optional, type: opts[:type], &block)
end

My issue is why are we checking to make sure that my options are a subset? of a single element wouldn't equality work better here? Unless of course we are looking to see if [:type].to_set is a #subset? of my opts which seems more like the intended behavior in which case I am proposing changing #new_scope to

def new_scope(attrs, optional = false, &block)
  opts = attrs[1] || { type: Array }
  raise ArgumentError unless opts.keys.to_set.superset? [:type].to_set
  ParamsScope.new(api: @api, element: attrs.first, parent: self, optional: optional, type: opts[:type], &block)
end

This would then allow me to use #requires with doc_attrs and a block. Does this some how cause interference elsewhere that I am overlooking? Can someone explain to me why you would check to see if {type: Array}.keys.to_set is a subset of [:type].to_set when this truely evaluates to {type: Array}.keys.to_set == [:type].to_set? because a subset of a single item must be equal to that single item.

@engineersmnky engineersmnky changed the title requires with block and and doc_attrs requires doc_attrs and a block Sep 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants