mutations
mutations copied to clipboard
how to add errors in nested objects
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
?
The method documentation for add_error
includes this example usage:
https://github.com/cypriss/mutations/blob/87d6ecb2d69484751297e42ecc4263f0e31e2bfa/lib/mutations/command.rb#L109
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 https://github.com/cypriss/mutations/pull/121 and https://github.com/cypriss/mutations/pull/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.
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)
That does seem like a natural extension to the current API 👍 are you interested in implementing it?