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

how to add errors in nested objects #140

Open
MathieuDerelle opened this issue Mar 6, 2019 · 3 comments
Open

how to add errors in nested objects #140

MathieuDerelle opened this issue Mar 6, 2019 · 3 comments

Comments

@MathieuDerelle
Copy link

lets use this example :

optional do
  array :tags, class: String
  hash :prefs do
    boolean :smoking
    boolean :view
  end
end

I'd like to use custom validation on :smoking
I'm doing this in the validate method
How to add an error on key ":prefs -> :smoking" ?
Is it possible with add_error ?

@eugeneius
Copy link
Collaborator

The method documentation for add_error includes this example usage:

# add_error("colors.foreground", :not_a_color) # => to create errors = {colors: {foreground: :not_a_color}}

Unfortunately, it doesn't work correctly: the last key isn't converted to a symbol, so the error is added as [:colors]["foreground"] instead of [:colors][:foreground].

Both #121 and #132 would make it work as expected, but I'm not happy merging either of them because they would break backwards compatibility. I think the right way to fix this problem is with a deprecation cycle.

@MathieuDerelle
Copy link
Author

thanks

could it be possible to have a similar way of doing it for arrays ?
like add_error("colors[3].foreground", :not_a_color) for adding error to the forth hash inside array colors

(it does not seem to be handled)

@eugeneius
Copy link
Collaborator

That does seem like a natural extension to the current API 👍 are you interested in implementing it?

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