-
-
Notifications
You must be signed in to change notification settings - Fork 189
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
Comments
@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 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 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 |
@MatElGran |
@MatElGran yeah as per @solnic reply we should be able to do something like: So calling |
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"]} |
for the record, this is specific to dry-validation. I tried the same thing with standalone dry-schema and it works correctly. |
Hello People, I can pick this one up |
I suppose this #652 is closing the issue |
thanks |
Describe the bug
After calling a
Dry::Validation::Contract
onerrors
thelocale
option return the correct translation instead when usingmessages
this option seems ignoredTo Reproduce
Giving a simple contract like this one:
Here a portion of my error file:
Here a screenshot from my console:

Expected behavior
result.messages(locale: :de)
returns the correct translationYour environment
The text was updated successfully, but these errors were encountered: