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

Reject non-declared params #1609

Closed
dhulihan opened this issue Apr 7, 2017 · 4 comments
Closed

Reject non-declared params #1609

dhulihan opened this issue Apr 7, 2017 · 4 comments

Comments

@dhulihan
Copy link

dhulihan commented Apr 7, 2017

It would be nice if grape could reject a non-declared param. Something along the lines of:

params do
  requires :first_name, type: String

  reject_non_declared
end

If we specify age, it is rejected since because it has not been declared.

curl -d '{"first_name": "dave", "age": 32}' \
       'http://localhost:9292/statuses' \
       -H Content-Type:application/json \
       -v 
# => HTTP 400 
@dhulihan
Copy link
Author

dhulihan commented Apr 7, 2017

Here's my hacky solution for the time being:

module Helpers
  def extra_params
    @extra_params ||= begin
      declared_params = declared(params, include_missing: true).to_h
      params.to_h.reject { |k, v| declared_params.key?(k) }
    end
  end

  def reject_extra_params
    unless extra_params.empty?
      msg = "Unrecognized parameter(s) provided: #{extra_params}"
      raise Grape::Exceptions::Base.new(message: msg, status: 400)
    end
  end
end

class Foo < Grape::Api
  helpers Helpers

  post do
    reject_extra_params
    params
  end
end

@dblock
Copy link
Member

dblock commented Apr 7, 2017

This is a dup, sSee issues #810, #1603, and previous PR #1158. There's a new PR being attempted for this in #1606, you should check it out, seems like a cleaner solution than what you propose here.

@dblock dblock closed this as completed Apr 7, 2017
@dhulihan
Copy link
Author

dhulihan commented Apr 7, 2017

K, thx. sry about the dup.

@dblock
Copy link
Member

dblock commented Apr 8, 2017

No worries @dhulihan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants