-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Allow validation of nested parameters. #236
Allow validation of nested parameters. #236
Conversation
Passing the scope to the validators, so they can take the relevant parameters. Using the group syntax which allows for: group :user do group :admin do requires :first_name, :last_name end end
I think adding a 4'th parameter to the Validator class is starting to make it inundated. It might be better to start moving things into an options hash. One thing I can see in the future is params requirements getting out of hand. Maybe we can build on this and give the ability to give it a class for defining requirements. Example:
Where UserParams is: class UserParams < Grape::ParamsScope
group do
end
end |
Other than that, I love the concept of this and your implementation looks pretty good. I'd love to use this. |
Never use 4 arguments. Usually 2 should be enough, a primary and an options IMO. Also, I recommend not to have more than 4 lines in any method... purist! |
This looks like what doctor ordered, thanks @tim-vandecasteele. I want an update to the README, please and I'll merge it unless there're objections. |
nice implementation, glad someone found the time for it ! I used this as reference: require 'grape'
class TestAPI < Grape::API
desc "add a user"
params do
group :user do
requires :name, type: String
requires :type, type: String
end
end
post '/add_user' do
end
end
p TestAPI.endpoints[0].options[:route_options][:params] The current output of this script is: {
"name"=>{:required=>true, :type=>"String"},
"type"=>{:required=>true, :type=>"String"}
} Where the expected output would be more like this for me: {
"user" => {
"name"=>{:required=>true, :type=>"String"},
"type"=>{:required=>true, :type=>"String"}
}
} |
documentation can properly access these parameters.
In fact I hit the same thing when trying to update grape-swagger. For now I adapted this by giving a {
"name"=>{:required=>true, :type=>"String", :full_name=>"user[name]"},
"type"=>{:required=>true, :type=>"String", :full_name=>"user[type]"}
} Although I agree a nested hash would be better (for one to avoid clashes in parameters between different groups). I'd have to check if I can change the |
oh, that's funny: https://github.com/schmurfy/grape_apidoc/tree/ ;) |
I'm merging this, thank you. It also wraps up the parameter block implementation sufficiently IMO to make a release soon. Anybody things we're still missing something? |
…meters Allow validation of nested parameters.
... maybe whitelisting discussed in #219 ... |
Thanks a lot for this feature! |
I really don't like the user[name] syntax but it is better than nothing I guess. |
Yes, definitely better, i'm looking for solution like for 2 days and almost start to write it from scratch. |
what do you want to do in the before block exactly ? Do you want some global validations for your api ? |
I have same parametrs in every action of my api that should be sent, so i want to declare validations for them in one place. |
global validations for every endpoint in one api class would indeed be interesting to have. |
Is it a big feature? Maybe you could guide me a bit and i will send a pull request with it? |
+1 @dpsk I have pagination parameters that apply in many API calls, would be silly to have to repeat them everywhere. I think I would want to write something like this: params do
include pagination_params
end This could be as simple as to look for a |
You can have a look at "lib/validations.rb" if you want to give it a try, it should not be too hard to implement. My first thought would be to add another global hash holding options for all the endpoints and not only the next one and which would be merged into the endpoint specific one before the endpoint is created. we could have something like: global_params do
requires :user_id, type: Integer
end
params do
# ...
end |
@schmurfy If you take my pagination example API that won't work. I don't want all my APIs to get this, just those that support pagination. A fully global feature isn't a bad idea either, authentication is a good example where someone just wants a global parameter set. |
I agree this is not a one size fit all, maybe there is room for both ^^ |
Based on the feedback of #222, I tried my own way at validation of nested parameters:
Using the group syntax which allows for:
In this implementation, I'm passing the scope to the validators, so they can take the relevant parameters.