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

Dry::Validation::Result#messages ignore locale option #589

Open
emaglio opened this issue Sep 28, 2019 · 3 comments
Labels

Comments

@emaglio
Copy link

@emaglio emaglio commented Sep 28, 2019

Describe the bug

After calling a Dry::Validation::Contract on errors the locale option return the correct translation instead when using messages this option seems ignored

To Reproduce

Giving a simple contract like this one:

class MySchema < Dry::Validation::Contract
  config.messages.load_paths << 'my_file.yml'
  params do
    required(:title).filled
  end
end

result = MySchema.call({})
result.errors(locale: :de) -> returns the de version
result.messages(locale: :de) -> returns the de version but it doesn't and returns the en version

Here a portion of my error file:

en:
  dry_validation:
    errors:
      filled?: "must be filled"


de:
  dry_validation:
    errors:
      filled?: "muss abgefüllt sein"

Here a screenshot from my console:
image

Expected behavior

result.messages(locale: :de) returns the correct translation

Your environment

  • adding compatibility to dry-v > 1.x in reform
@MatElGran

This comment has been minimized.

Copy link
Contributor

@MatElGran MatElGran commented Oct 2, 2019

@emaglio I had to change a bit your example to make it work for the base case (ie with default language). As I'm new to this code base, I'm not sure if I'm doing something wrong or if there were errors in your code example.

Anyway, I think the problem here is that you are not providing the correct error key (cf https://dry-rb.org/gems/dry-schema/error-messages/#finding-the-right-key). Given this translation file (with key? messages added):

en:
  dry_validation:
    errors:
      filled?: "must be filled"
      key?: "is missing"

de:
  dry_validation:
    errors:
      filled?: "muss abgefüllt sein"
      key?: "Sorry I don't speak german"

and the following schema (the same as yours):

class MySchema < Dry::Validation::Contract
  config.messages.load_paths << 'my_file.yml'
  params do
    required(:title).filled
  end
end

Here is what I get:

result = MySchema.new.call({}) # CHANGE HERE: had to instantiate the schema

errors = result.errors(locale: :de)
# =>  #<Dry::Validation::MessageSet messages=[#<Dry::Schema::Message text="Sorry I don't speak german" path=[:title] predicate=:key? input={}>] options={:source=>[#<Dry::Schema::Message text="is missing" path=[:title] predicate=:key? input={}>], :locale=>"de"}>

errors.messages # CHANGE HERE: no `messages` method on result object
# => [#<Dry::Schema::Message text="Sorry I don't speak german" path=[:title] predicate=:key? input={}>]

to get the filled? message, title key must be in params Hash, ie

result = MySchema.new.call({title: ''}) # CHANGE HERE: had to instantiate the schema

errors = result.errors(locale: :de)
# => #<Dry::Validation::MessageSet messages=[#<Dry::Schema::Message text="muss abgefüllt sein" path=[:title] predicate=:filled? input="">] options={:source=>[#<Dry::Schema::Message text="must be filled" path=[:title] predicate=:filled? input="">], :locale=>"de"}>

errors.messages # CHANGE HERE: no `messages` method on result object
=> [#<Dry::Schema::Message text="muss abgefüllt sein" path=[:title] predicate=:filled? input="">]

It's completely possible I missed something, so don't hesitate to correct me or add information, and I'll have another try at it

@solnic

This comment has been minimized.

Copy link
Member

@solnic solnic commented Oct 3, 2019

@MatElGran Result#messages is a method provided by the :hints extension, so first you gotta enable it via Dry::Validation.load_extensions(:hints).

@emaglio

This comment has been minimized.

Copy link
Author

@emaglio emaglio commented Oct 9, 2019

@MatElGran yeah as per @solnic reply we should be able to do something like:

image

So calling messages from the result object not from the errors
👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.