-
-
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
Add the ability for parameter values to accept a Proc #558
Conversation
Please update the CHANGELOG, too. |
@@ -212,7 +213,7 @@ def validates(attrs, validations) | |||
end | |||
|
|||
# type should be compatible with values array, if both exist | |||
if coerce_type && values && values.any? { |v| !v.instance_of?(coerce_type) } | |||
if coerce_type && values && (values.is_a?(Proc) ? values.call : values).any? { |v| !v.instance_of?(coerce_type) } |
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.
Values are evaluated above.
@dblock , @danhawkins how can I help to move on this PR? |
|
It would be nice if the example in the README was related to the general topic of Twitter feed/status and were actually "useful". Right now the example you added just returns a fixed array, so it's unclear why anyone should be using a Proc in that case. |
The README has an example for specifying values, I only copied that to illustrate a spec, I am not sure of something that could be validated like this using twitter examples |
Fyi, fixed the weird build error in master (was introduced by a Rubocop upgrade), you should rebase. |
OK on the values example, but you can see how |
@dblock, we can don't touch values = validations[:values]
values = (values.is_a?(Proc) ? values.call : values)
validations[:values] = doc_attrs[:values] = values if values Does this smell good?) (I think it's better then the double evaluating and can be encapsulated in more generic way) |
Do you think it would be a bad idea to execute the proc at runtime for every incoming request as well? Like this code. |
Merged as 764f4c0. I made a couple small changes, please check. |
The https://github.com/wpschallenger/grape/commit/1e05a437ad251fae91efd737dd172e2ef16b4dca change didn't make it in my merge. If you think it's a good idea, start a new PR, we'll need a test that behaves differently depending on when the |
And thanks for being patient with me. |
Sure, I will add that later on today, when a dynamic call is added later. Regards Danny Hawkins[email protected] On 28 January 2014 at 16:58:35, Daniel Doubrovkine (dB.) ([email protected]) wrote: The wpschallenger@1e05a43 change didn't make it in my merge. If you think it's a good idea, start a new PR, we'll need a test that behaves differently depending on when the values.call is evaluated. — |
Ok, if if default && values && !values.include?(default)
raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :values, values)
end
# type should be compatible with values array, if both exist
if coerce_type && values && values.any? { |v| !v.instance_of?(coerce_type) }
raise Grape::Exceptions::IncompatibleOptionValues.new(:type, coerce_type, :values, values)
end |
Looking forward to the next pr, @danhawkins, thx. |
But for me, it's bad idea to generate IMHO, all app context specific restrictions(model validation) should be executed at processing endpoint. Maybe I misunderstood something. |
I get your point, but in my case using grape I have dynamic values which I want to validate, and using grape-swagger I want to see those update also. The benefit of this approach is that the developer can choose to use dynamic values or not, with negligible performance implications. But for me, it's bad idea to generate default and values at runtime for every incoming request. These fields are contract between me and client, like default and values in any public API, for example https://trello.com/docs/api/action/index.html (we also generate docs using these fields..). If these fields changed well then API was changed. IMHO, all app context specific restrictions(model validation) should be executed at processing endpoint. Maybe I misunderstood something. — |
@danhawkins Isn't the required :hashtag, type: String, values: -> { Hashtag.all.map(&:tag) } Isn't this the same as required :hashtag, type: String, values: Hashtag.all.map(&:tag) In fact, the specs in |
Hey, Yes, thats right, right now it doesn’t do what is needed, but I have a branch I am working on to actually realise the solution. I imagine I will submit the pull request tomorrow, but it has been a little more tricky than I first planned! On 28 January 2014 at 21:12:26, Daniel Doubrovkine (dB.) ([email protected]) wrote: @danhawkins Isn't the lambda unnecessary in the context where you want to evaluate the values once, and then just check against that? Lets take the example of this PR: required :hashtag, type: String, values: -> { Hashtag.all.map(&:tag) } required :hashtag, type: String, values: Hashtag.all.map(&:tag) — |
I added this to fix a small problem of my own, thought it might be useful to the main project. I've added tests and README section