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

Closed
emaglio opened this issue Sep 28, 2019 · 8 comments
Closed

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

emaglio opened this issue Sep 28, 2019 · 8 comments

Comments

@emaglio
Copy link

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
Copy link
Contributor

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
Copy link
Member

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
Copy link
Author

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
👍

@solnic
Copy link
Member

solnic commented Dec 13, 2019

Here's a valid repro:

require 'dry/validation'

Dry::Validation.load_extensions(:hints)

class MySchema < Dry::Validation::Contract
  config.messages.load_paths << 'my_file.yml'

  params do
    required(:title).filled(:string)
  end
end

schema = MySchema.new

result = schema.call({})

puts result.errors(locale: :de).to_h.inspect
# {:title=>["wird vermisst"]}

puts result.messages(locale: :de).to_h.inspect
# {:title=>["is missing", "must be a string"]}

@solnic solnic added the hints label Dec 13, 2019
@solnic
Copy link
Member

solnic commented Dec 13, 2019

for the record, this is specific to dry-validation. I tried the same thing with standalone dry-schema and it works correctly.

@sirfilip
Copy link

Hello People, I can pick this one up

@moofkit
Copy link
Contributor

moofkit commented Oct 4, 2020

I suppose this #652 is closing the issue

@flash-gordon
Copy link
Member

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants