Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

clean way to check required fields for api calls? #34

Closed
hsunami opened this Issue · 5 comments

3 participants

@hsunami

Often, I have cases where the required field for an api call is not included, and something like params[:x][:y] would result in a 500 error if '?x[y]=12121' is not part of the call, showing my whole stack trace. I understand that I can write a bunch of boilerplate code to check every single case for all required fields missing for a call, but is there any clean way to check for required fields using rocketpants?

Thanks Darcy!
Jeff

@Sutto
Owner

At the moment, the easiest is probably with the strong parameters plugin and then mapping the error from that to Bad Request. I've been considering ways to do this from RocketPants directly, but i'm yet to find a way I really like

@Sutto
Owner

(And apologies as usual for the late reply)

@adamburmister

I've been playing with the strong parameters plugin today, and it works fine. I can't find a great way to implement it nicely though (it's a bit ugly at the moment).

What I'd like to see is a way to register the parameter and it's description for the action. I have implemented something like this within the action...

def show
require_param :dummy, "A dummy required parameter", format: :alphanumeric
end

Then I was catching the MissingParameter exception from strong params and recasting it as a BadRequest exception, with the missing params in the metadata.messages. I can't, however, figure out if I can access the instance hash on the AppController to access the parameter description (if anyone has ideas that would be great!)

Anyhow, I think the best place to put these parameter requirements is in the routes, perhaps like https://github.com/mifo/sinatra-rabbit

It would be self describing for the API then, imho. We could use the routes to extract a documented interface.

So something like

api version: 1 do
  param :auth_token, required: true
  resources :companies do
    operation :index do
      param :page, required: false, format: :numeric, description: "Paged results index (integer)"
    end
  end
end
@Sutto
Owner

@adamburmister Interesting, I'm not sure what I think of doing it in the routes - it is partly a routing concern, but at the same time I don't feel like it fits well there (since it moves a lot of logic into routes, which is kind of bad).

The describing parameters portion I think is very important as well, but likewise not sure it belongs there.

@Sutto
Owner

For the moment, I'm going to close this and add a todo list to add Strong Parameters support. I think that's the cleanest, simplest way to understand for users. I like the idea of keeping Rocket Pants pretty minimal in this regard and building extensions for it as needed.

@Sutto Sutto closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.