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
Issue with same param name in different given blocks #1885
Comments
Looks like a non-feature of supporting two Turn this into a spec, maybe implement/fix? |
@dblock I will add a spec. |
@dblock can you point me to the best place for adding the test case? |
I would say spec/grape/validations/params_scope_spec.rb has most of the |
^^ My workaround should NOT be used. It appears to work by breaking parameter validation. |
I spent time looking into this today, and I think I understand the problem, but I believe the fix would require a non-trivial change to how this library works. Parameter declarations are set when the class is loaded, not when a request is made. This manifests as a problem related to Suppose you were to do this: requires :type, type: String
given type: ->(val) { val == 'typeA' } do
requires :attributes, type: Hash do
requires :foo, type: String, values: ['foo-a', 'foo-b']
requires :bar, type: String
end
end
given type: ->(val) { val == 'typeB' } do
requires :attributes, type: Hash do
requires :foo, type: Integer, values: [1, 2]
end
end In terms of validations, this actually works as expected:
This is good, but the good news stops there. If we try to use
The reason is that declared params are determined when the class is loaded, so the second In order to solve this problem, I think we would need for two things to happen:
|
Seems like the check on the values param in the second block override the first. When both the blocks are executed in the same call the second check on
values
fails as invalid.Sounds like a bug?
The text was updated successfully, but these errors were encountered: