Access params when coercing
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 10k
- Forks
- 1.2k
- Avg merge
- 14h 38m
- Merged PRs (30d)
- 92
Description
Encountered this use-case today and feel like it would make for a nice enhancement to the coercion functionality to be able to access the params from my coercion method.
I have an API endpoint which accepts an address (street, city, state, zip) however the city and state parameters are optional. In most countries you can derive both from the zip/postal code, so I'd like to be able to do something like:
params do
requires :street, type: String
requires :zip, type: String
optional :city, type: String, coerce_with: ->(val) { val ? val : LookupService.find_city(params[:zip]) }
optional :state, type: String, coerce_with: ->(val) { val ? val : LookupService.find_state(params[:zip]) }
end
Ultimately ended up doing this with a callback, updating the params hash.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing Grape's coercion functionality and the callback-based workaround described in the issue. Determine how a coercion method could access the surrounding params, including optional city and state values, and define the expected behavior for dependent fields. Done means the supported behavior is covered by tests for the postal-code lookup example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100