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 nested attributes are misleading #36

Closed
tfausak opened this issue Jul 22, 2013 · 7 comments · Fixed by #158
Closed

Error messages for nested attributes are misleading #36

tfausak opened this issue Jul 22, 2013 · 7 comments · Fixed by #158
Assignees
Labels
Milestone

Comments

@tfausak
Copy link
Collaborator

tfausak commented Jul 22, 2013

If you don't supply a required nested attribute, you'll get a confusing error message.

class Interaction < ActiveInteraction::Base
  hash :h do; boolean :b end
end
p Interaction.run(h: {}).errors.messages
# => {:h=>["is required"]}
@tfausak
Copy link
Collaborator Author

tfausak commented Jul 22, 2013

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"]}

@tfausak
Copy link
Collaborator Author

tfausak commented Aug 6, 2013

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"]}

@tfausak
Copy link
Collaborator Author

tfausak commented Aug 6, 2013

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)

@tfausak
Copy link
Collaborator Author

tfausak commented Aug 7, 2013

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.

@AaronLasseigne
Copy link
Owner

I'm good with throwing invalid as an error message in these cases.

@ghost ghost assigned tfausak Aug 7, 2013
@tfausak tfausak closed this as completed in f352618 Aug 7, 2013
@gilbert
Copy link

gilbert commented Mar 13, 2014

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 1.1.2

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.

@tfausak
Copy link
Collaborator Author

tfausak commented Mar 13, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants