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

Error messages for groups (like the "confirmation" short-cut) can't be generated #47

Closed
timriley opened this issue Jan 12, 2016 · 3 comments

Comments

@timriley
Copy link
Member

If I have a schema like this:

class MySchema < Dry::Validation::Schema
  confirmation(:password)  
end

And call it with invalid confirmation data:

v = MySchema.new.call(password: "foo", password_confirmation: "bar")

Then it looks like it's not possible to provide YML or i18n data to provide a human-friendly error message for that validation failure.

The reason for this is that the error AST entry for that failure has a :group in it:

pry(main)> v.errors.map(&:to_ary)
# => [[:error, [:input, [:password_confirmation, ["foo", "bar"], [[:group, [:password_confirmation, [:predicate, [:eql?, []]]]]]]]]]

And the ErrorCompiler has no #visit_group method, which means it goes through to #method_missing and just returns {value: args[1]} output for the validation result's #messages, i.e.

pry(main)> v.messages
# => {:password_confirmation=>[[{:value=>:password_confirmation}], ["foo", "bar"]]}

Because it goes through #method_missing in the error compiler, it means it never actually tries to look up an i18n/yml key for the translated error message for that validation failure.

Is my understanding of this correct? Is this just a matter of updating the ErrorCompiler to support groups? Or have I missed some already existing way to actually provide human-friendly error messages for failures like this?

Thanks :)

@solnic
Copy link
Member

solnic commented Jan 12, 2016

I never added support for grouped rules to the error compiler, so yes, this is a missing feature.

@timriley
Copy link
Member Author

Cool, thanks for confirming. I can try take a stab at this, if you like (though it might have to be across the next couple of weeks). Is there anything you'd like to see here, for example around the names of the lookup keys for the messages?

On 12 Jan 2016, at 7:43 PM, Piotr Solnica notifications@github.com wrote:

I never added support for grouped rules to the error compiler, so yes, this is a missing feature.


Reply to this email directly or view it on GitHub.

@solnic
Copy link
Member

solnic commented Jan 19, 2016

@timriley I'm gonna do it tomorrow, it's more involved than I thought and I'm already working in this area with other improvements

@solnic solnic closed this as completed in f3911b1 Jan 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants