mutations icon indicating copy to clipboard operation
mutations copied to clipboard

how to add errors in nested objects

Open MathieuDerelle opened this issue 5 years ago • 3 comments

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 ?

MathieuDerelle avatar Mar 06 '19 14:03 MathieuDerelle

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.

eugeneius avatar Mar 10 '19 21:03 eugeneius

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)

MathieuDerelle avatar Mar 10 '19 22:03 MathieuDerelle

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

eugeneius avatar Mar 13 '19 21:03 eugeneius