-
-
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
Raise proper validation errors on invalid parameter types #543
Comments
Agreed on (1). Maybe by introducing a type? I should be able to write: requires :opts, type: Array do
requires :opt_a, type: Integer, desc: "Option A"
optional :opt_b, type: Integer, desc: "Option B"
end For both (2) and (3): these are bugs. The parameter passed is of the wrong type, so you should get an exception that says so. Leaving this open as-is. It would be a great start if you could make these into Grape tests and opened PRs with the failing ones. Of course, feel free to fix :) |
I've renamed this to "Raise proper validation errors on invalid parameter types", I think it captures the problem better. |
Agreed - it was just intended as a placeholder until I get around to either writing some tests and/or fixing it. I'll get back to you as soon as I make some progress on this. |
I've made a fair bit of progress on this, both on tests and fixing/implementing it. However, I've run into a bit of an issue with how query params are handled. In particular, if something requires an array block, an empty array should be valid. But with how query parameters work, the empty array doesn't really exist, so grape never sees it on the (GET) request. The way I've implemented it, which is by adding validations for presence and type on the 'requires', etc with block, this fails the 'presence' validation. In my opinion that's actually fair enough, since it isn't actually present even as an empty array in the request. It's just a quirk to keep in mind with query parameters and groups/requires with blocks, etc. It should work as expected with body parameters, e.g. json, where an empty array is something perfectly valid. |
I think the presence issue is much smaller, and you can open it once we have a fix for the bigger problem. |
… types * Also adds a :type option to group/requires/optional with block, which can either be Array or Hash. It defaults to Array. * Fixes (2) and (3) as mentioned in ruby-grape#543 * There is a quirk around query parameters: an empty array should pass validation if the array itself is required, but with how query parameters work, it doesn't. It does work fine with body parameters using JSON or similar, which do have a concept of an empty array.
… types * Also adds a :type option to group/requires/optional with block, which can either be Array or Hash. It defaults to Array. * Fixes (2) and (3) as mentioned in ruby-grape#543 * There is a quirk around query parameters: an empty array should pass validation if the array itself is required, but with how query parameters work, it doesn't. It does work fine with body parameters using JSON or similar, which do have a concept of an empty array.
* Also adds a :type option to group/requires/optional with block, which can either be Array or Hash. It defaults to Array. * Fixes (2) and (3) as mentioned in #543 * There is a quirk around query parameters: an empty array should pass validation if the array itself is required, but with how query parameters work, it doesn't. It does work fine with body parameters using JSON or similar, which do have a concept of an empty array.
Closing, fixed in 0.7.0. |
* upstream/master: add posibility to define reusable named params Added ability to restrict declared(params) to the local endpoint with include_parent_namespaces: false. Fix for ruby-grape#464: gracefully handle invalid version headers Rename exception classes to match README. Define errors in context/before blocks. Updated/renamed UPGRADING. README.md and UPGRADE.md update for ruby-grape#543, ruby-grape#545 Address ruby-grape#543 - raise proper validation errors on array/hash types Remove unnecessary test case. Rename children --> subclasses to match the name used in the code. Fix typo in rescue_from unit test (Communication --> Communications). Rescue subclasses in error middleware. Fix for travis-ci/travis-ci#1800. Added Ruby 2.1 support. Lock RSpec version, failing with rspec-expectations 3.x beta.
I've run into several issues with validations. For half of the things I don't have a real use case, but it should still work.
The text was updated successfully, but these errors were encountered: