-
-
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
Params validations do not run for route_param #1278
Comments
Routes are a little special. Route parameters are required to match the route and that their format is driven by |
It was a typo in my sample code. Now the sample works as expected but in the project I am working on it seems that validation is not run. Let me double check and try to reproduce the error. I will eventually close this if I cannot reproduce the error. |
I reproduced the problem. It seems that validations do not run when using Here is a sample code: # file: route_param.rb
require 'grape'
class Bang < Grape::Validations::Base
def validate_param!(attr_name, params)
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)],
message: 'Bang!'
end
end
class Params < Grape::API
get do
{ params: params }
end
end
class RouteParam < Grape::API
format :json
default_format :json
version 'v1', using: :path do
params do
requires :name, type: String, allow_blank: false, bang: true
end
route_param :name do
get do
{ params: params }
end
end
end
version 'v2', using: :path do
params do
requires :name, type: String, allow_blank: false, bang: true
end
route_param :name do
mount Params
end
end
end
run RouteParam Here is the error: $ curl localhost:9292/v1/foo
{"error":"name Bang!"} This is correct because the validation always fails. $ curl localhost:9292/v2/foo
{"params":{"name":"foo"}} This is not correct because the validation didn't run. |
You should turn this into a minimal spec next. We have other issues around |
route_param :party_id, type: Integer do
mount Tracks => 'tracks'
end
# generates parties/tracks/:party_id on grape 0.12.0 That is really required feature (it's how I got here). Subscribing. |
Thanks @vanburg, looking forward to some PRs! |
Hi guys. Any news about this issue? Because it's really uncomfortable to use(in case of last example) constructions like declared(params).merge(party_id: params.party_id) inside mounted class instead of regular declared(params). |
you can close this issue because all needed changes is already in latest master. |
How was it fixed? What was the PR? |
Oh I see, this was resolved via #1422. |
@namusyaka you might want to double-check this issue in case I missed something |
I check some specs on my project and I find out that this functionality work little bit wrong with class Artists
namespace :artists do
route_param :id, type: Integer do
get do
Artist.find(declared(params))
end
end
route_param :artist_id, type: Integer do
mount Compositions
end
end
end In |
@Arkanain Reopen a new issue please with hopefully a spec? Thanks. |
@dblock I will create new issue little bit later. params do
requires :id, type: Integer
end
namespace ':id' do
# some endpoints
end That's why when we have two class Artists
namespace :artists do
namespace ':id' do
params do
requires :id, type: Integer
end
get do
Artist.find(declared(params))
end
end
route_param :artist_id, type: Integer do
mount Compositions
end
end
end and bug disappeared. In inner namespace |
We do document the first behavior is legal, but I bet |
@dblock As I promise previously I create pull request with specs for issue about I told you. Also I find one more bug in |
Hi, I found that validations do not run for
route_param
, here is a minimal example:Run with
rackup route_param.ru
and thencurl localhost:9292/foo
.I expect a validation failed message, but instead I get
{"name":"foo"}
.I am using version 0.14.0.
Updated: fix sample code typos
The text was updated successfully, but these errors were encountered: