Is there some way to rename a coerced and validated field in the
I have a web form where an input parameter is named
durationText and contains a value like
7 days. We parse this text and get a more reasonable Ruby type (in this case the duration as seconds).
We want to perform validations on the parsed value (e.g. “less than 1 month”) and then pass the parsed value to our underlying object.
This all seems reasonable, as we can create a custom type and handle that. The problem arises with the name:
In other situations where we have
myLongParam at the HTTP interface and
my_long_param in Ruby, I’ve added aliases to our data object:
class MyData include ActiveModel::Model attr_accessor :my_long_param alias :myLongParam= :my_long_param= end
That works, but “pollutes” our domain object with knowledge of the HTTP layer, even if it is only via an alternate name. However, adding an alias called
durationText that doesn’t expects to receive a number is going to be downright confusing.
It would be awesome if there a way to have a contract like:
class NewUserContract < Dry::Validation::Contract params do required(:lastName).value(:integer).as(:last_name) end end