Skip to content

Access params when coercing #1571

@ckorhonen

Description

@ckorhonen

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions