-
-
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 custom coercion failure messages #1203
Conversation
Great, can you squash these commits please? |
756e9ff
to
fa6b90d
Compare
Ok done. Not sure what's up with those failed Rubinius builds in Travis. |
@@ -11,7 +11,12 @@ def validate_param!(attr_name, params) | |||
if valid_type?(new_value) | |||
params[attr_name] = new_value | |||
else | |||
fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message_key: :coerce | |||
bad_value = new_value | |||
if bad_value.is_a?(Types::InvalidValue) && !bad_value.message.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not in love with this. We're now explicitly checking what kind of exception we got. Any better suggestions?
I think there's a bigger problem here. Coercion and validation take separate paths but are really converging towards the same thing (i.e. we'll eventually end up needing full-fledged validation inside |
fa6b90d
to
242ecea
Compare
Custom param types can now report why coercion failed in their .parse method by returning an InvalidValue initialized with an error message. For example: Grape::Validations::Types::InvalidValue.new "is too short"
242ecea
to
ce9f494
Compare
I encountered something related in #1675. I think our validation + coercion needs some serious revisiting. Just thinking out loud. |
I agree :) We've been using our own fork of Grape with the above change, periodically rebasing to origin. It works, but it's pretty hacky. There's probably a bigger architectural issue starting to surface here, but like I said before, it feels like too much for me to try to take on. |
Currently if something goes wrong inside a custom param type's
.parse
method, the only feedback in the resulting validation failure response is a generic"____ is invalid"
message. This pull request allows custom coercion failure messages by having the.parse
method return anInvalidValue
initialized with the failure message.For example: