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

Fail faster when defining invalidly nested defaults #30

Closed
tfausak opened this issue Jul 15, 2013 · 2 comments
Closed

Fail faster when defining invalidly nested defaults #30

tfausak opened this issue Jul 15, 2013 · 2 comments
Assignees
Milestone

Comments

@tfausak
Copy link
Collaborator

tfausak commented Jul 15, 2013

For array and hash filters, it's invalid to set defaults on nested attributes. However, we don't catch that error until you call run. As noted in the specs, we should raise an error when the class is defined.

For instance, this code should raise an ArgumentError:

class InvalidInteraction < ActiveInteraction::Base
  array :a do
    array default: []
  end
end

As it is, you'd have to call InvalidInteraction.run(a: []) to get that to fail.

@tfausak
Copy link
Collaborator Author

tfausak commented Jul 16, 2013

For what it's worth, Mutations allows nested defaults. As far as I can tell, they don't do anything on arrays:

class ArrayMutation < Mutations::Command
  optional { array :a do; array default: [] end }
  def execute; a end
end
p ArrayMutation.run!
# => nil

Default nested hashes get merged with the input, like you'd expect:

class HashMutation < Mutations::Command
  required do
    hash :h do
      hash :x, default: { b: false } do
        boolean :b
      end
    end
  end
  def execute; h end
end
p HashMutation.run!(h: {})
# => {"x"=>{"b"=>false}}
p HashMutation.run!(h: { x: { b: true } })
# => {"x"=>{"b"=>true}}

I know I argued against defaults in nested blocks before, but I guess I'm not that opposed to them. I can see how they make sense.

@tfausak
Copy link
Collaborator Author

tfausak commented Jul 17, 2013

  • Nested defaults arrays should be silently ignored instead of throwing an error.
  • Hashes should permit nested defaults. They should be merged with the user-supplied input.

@tfausak tfausak closed this as completed Jul 28, 2013
@ghost ghost assigned tfausak Jul 28, 2013
tfausak added a commit that referenced this issue Aug 6, 2013
Nested default arrays will be ignored. Nested default hashes will be
merged with user input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant