Given the following params defined based on dry-validation:
class UserParams < Dry::Validation::Contract params do required(:email).value(:string) end end
If I want to take email param after validations I have only one choice now - it’s transform to Ruby hash and use
result = UserParams.new.call(email: ‘email@example.com’)
result[:email] # => ‘firstname.lastname@example.org’
But, If I have a type in the
nil, that’s basically not much better than just take
I want Ruby validate my mistakes in the keys. For that reason I would like to get a struct with a strict API after passed validations. By struct here I mean an object has
In short, I want the following code work:
result.email # => 'email@example.com'
But I understand that it would have an obscured API, if the validations don’t pass. For that reason there could be another method that does the transformation into struct:
result.to_struct.email # => 'firstname.lastname@example.org'
to_struct would fail if validations don’t pass.
What do you think if dry-validation implement this method?
Maybe, I’m missing something and it’s already possible to do that transition, but I’ve not found it anywhere. If it’s already there, please point me out. Thanks!