-
Notifications
You must be signed in to change notification settings - Fork 463
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 optional functionality to pre process and extract parameters. #188
Conversation
Interesting idea! It could also probably used as way to prevent multi_assignment issues, accepting only the documented params. I'm not sure about |
@@ -186,6 +187,8 @@ def _apipie_define_validators(description) | |||
|
|||
old_method = instance_method(description.method) | |||
|
|||
|
|||
# @todo we should use before_filter |
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.
Good point, I'm not sure we didn't go for it from the start. Maybe there was some reason, if there is, I bet it will be rediscovered while trying converting that into filter.
Thanks! Indeed we are using it also for multi_assignment issues. Almost all the modification I proposed in these PRs are (or will be) used in production. |
I added a description about |
I changed |
@api_params = {} | ||
if @hash_params && value | ||
@hash_params.each do |k, p| | ||
@api_params[p.as] = p.process_value(value[k]) if value.has_key?(k) |
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.
the instance param for @api_params is probably unnecessary, what about:
if @hash_params && value
return @hash_params.each_with_object({}) do |(key, param), api_params|
if value.has_key?(key)
api_params[param.as] = param.process_value(value[key])
end
end
end
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 never used each_with_object
before. It seems better with it, thanks !
Very nice PR, merging now |
sry, one more thing: could you rebase against the latest master, probably some conflict in README |
variable as suggested by @iNecas
Just rebased against master |
Thank you! |
Add optional functionality to pre process and extract parameters.
Released in 0.1.0. Thank you for your help! |
Description
The goal is to extract and pre process parameters of the request.
For example Rails, by default, transforms empty array to nil value, for some people it could be better to transform it again to an empty array.
Or if we want to support a custom parameter type, like enumeration (comma separated values), this could be used to transform automatically the string parameter, representing an enumeration, to an array.
Usage
To use it, set processing_value configuration variable to true.
In your controller action, use values variable instead of params.
To implement a custom pre processing, the idea is to write a
process_value
function in the validator:function in your validator:
as
In the same time, I added an option in parameter description:
as
.The idea is to add the possibility to have a mapping/separation between names we expose to the external world and the names we use internally.
example:
not processed params:
processed params (available through
api_params
:What do you think about this functionality in Apipie ?