-
Notifications
You must be signed in to change notification settings - Fork 142
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 nested attributes are misleading #36
Comments
Similarly: class Interaction < ActiveInteraction::Base
hash :h do; boolean :b end
end
p Interaction.run(h: { b: 0 }).errors.messages
# => {:h=>["is not a valid hash"]} |
I'm not too sure how to fix this one. What would a good solution look like? We can't use the nested attribute's name as the key because that could be even more misleading: class Interaction < ActiveInteraction::Base
hash :h do; boolean :b end
boolean :b
end
p Interaction.run(h: {}, b: true).errors.messages
# => {:b=>["is required"]} |
For what it's worth, Mutations uses the nested attribute's name: Class.new(Mutations::Command) do
required { hash :h do boolean :b end }
end.run!(h: {})
# .../mutations-0.6.0/lib/mutations/command.rb:84:in `run!': B is required (Mutations::ValidationException) |
I think the best option here is to use a different error message when a nested attribute fails validation. Something like this: class Interaction < ActiveInteraction::Base
hash :h do; boolean :b end
end
p Interaction.run(h: {}).errors.messages
# => {:h=>["is invalid"]} That way the message is attached to the right attribute, but it's not misleading. I feel like this might be a hard change to make, though. If it turns out to be too gnarly, I'm ok with leaving it as-is. |
I'm good with throwing invalid as an error message in these cases. |
Did this fix get undone? I have a hash with 13 expected attributes. Here's an example of what I'm getting: class Interaction < ActiveInteraction::Base
hash :h do
string :one
string :two
string :three
end
end
p Interaction.run(h: { one: '1', two: '2', three: 3 }).errors.messages
# {:h=>["is not a valid hash"]} I'm using version Is there a way to get an error message that tells you which nested attribute is invalid? This would make active_interaction much more TDD-friendly. |
It certainly seems that way. class Issue32 < ActiveInteraction::Base
hash :a
hash :b do; hash :c end
end
Issue32.run(a: 0, b: { c: 0 }).errors.messages
# => {:a=>["is not a valid hash"], :b=>["is not a valid hash"]} Looks like this regression was introduced in 6b165d5. |
If you don't supply a required nested attribute, you'll get a confusing error message.
The text was updated successfully, but these errors were encountered: