Dry-validation and error translations

It always strikes me a bit odd that all validation libraries are set on returning validation messages instead of validation errors. This usually mean, that we loose any information about the cause of the error in exchange for translated string.

I believe it strictly view concern to translate messages, which is why I’d like to propose a new messages backend which will not try to generate user friendly messages but return ValidationError instead:

class MyContract < Dry::Validation::Contract
  params do
    required(:number).filled(:integer)
  end

  rule :number do
    key.failure(:too_small, minimum: 10) if value && value < 10
  end
end

MyContract.new.({}).errors.to_h #=> { number: [<#Dry::Validation::ValidationError: error: :required>] }

result = MyContract.new.({number: '5'}).errors
result.to_h #=> { number: [<#Dry::Validation::ValidationError: error: :too_small, meta: { minimum: 10 }>] }
result.as_json #=> { number: [{ error: 'too_small', minimum: 10 }]

Feel free to report a feature request about it. This could be easily added by adding a configuration setting for message coercion. Currently message set uses Message#dump and it’s hardcoded, if this is made configurable then you’ll have what you need.