Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full error messages don't include the key name #661

Closed
stind opened this issue Jul 30, 2020 · 0 comments · Fixed by #662
Closed

Full error messages don't include the key name #661

stind opened this issue Jul 30, 2020 · 0 comments · Fixed by #662
Labels
Milestone

Comments

@stind
Copy link
Contributor

stind commented Jul 30, 2020

This issue was initially reported in discourse: https://discourse.dry-rb.org/t/inconsistency-in-resolving-full-error-messages/1091

  1. Full message of a key failure does not include the key name when the key is not translated and the failure method was called with a symbol message, e.g. key.failure(:is_invalid).
    So the error message looks like " is invalid", instead of "foo is invalid".

  2. Full message of a base failure starts with a space symbol, e.g. " the message".

To Reproduce

require "i18n"
require "dry/validation"

I18n.backend.store_translations(
  :en,
  {
    dry_validation: {
      errors: {
        is_invalid: "is invalid"
      }
    }
  }
)

class MyContract < Dry::Validation::Contract
  config.messages.backend = :i18n

  params do
    required(:foo).value(:integer)
  end

  rule do
    base.failure("base failure here")
  end

  rule(:foo) do
    key.failure(:is_invalid)
  end
end

MyContract.new.call(foo: 42).errors(full: true).map(&:text)
#=> [" base failure here", " is invalid"]
# Expected:
#=> ["base failure here", "foo is invalid"]

Expected behavior

  1. Full message of a key failure always starts with the key name, even if the key is not translated
  2. Full message of a base failure includes just the message without the leading space symbol, e.g. "the message" instead of " the message"

Environment

ruby: 2.6.6
dry-validation: 1.5.2

@stind stind added the bug label Jul 30, 2020
stind added a commit to stind/dry-validation that referenced this issue Jul 30, 2020
stind added a commit to stind/dry-validation that referenced this issue Jul 30, 2020
@solnic solnic added this to the 1.5.4 milestone Jul 31, 2020
stind added a commit to stind/dry-validation that referenced this issue Aug 4, 2020
stind added a commit to stind/dry-validation that referenced this issue Aug 4, 2020
solnic added a commit that referenced this issue Aug 5, 2020
Fix full error messages

[changelog]

fixed: "Full messages work correctly with rule failures now (issue #661 fixed via #662) (@stind)"
tadeusz-niemiec pushed a commit to tadeusz-niemiec/dry-validation that referenced this issue Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants