dry-validation icon indicating copy to clipboard operation
dry-validation copied to clipboard

Errors without messages

Open BroiSatse opened this issue 4 years ago • 2 comments

At the moment validation contract returns nice human-readable errors via translation backend. Being a robot, I do not like it and I'd prefer pure data object/hash to be returned.

Justification: I don't think it is contract responsibility to translate it's errors. When used within API, all the i18n normally happens on the client side, so API can be completely translation-free.

Examples

class MyContract < Dry::Validation::Contract
  json do
    required(:name).filled(:string)
    required(:age).filled(:integer)
  end

  rule(:age) do
    key.failure(:too_young, min: 32) if age < 32
  end
end

contract = MyContract.new
errors = MyContract.new(age: 28).errors

errors.to_h # => {
                   name: [{error: :missing}]
                   age: [{error: :too_young, min: 32}]
                 }

(The above is only example response, I have no strong preference/opinion on how that data should be represented)

Resources

Discourse "discussion": https://discourse.dry-rb.org/t/dry-validation-and-error-translations/919

BroiSatse avatar Nov 21 '19 00:11 BroiSatse

Any plans to revisit this issue?

guilherme-andrade avatar Mar 07 '22 15:03 guilherme-andrade

@guilherme-andrade it's marked as help wanted which means that PRs are welcome

solnic avatar Mar 13 '22 10:03 solnic