-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Open
Labels
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.