-
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
Newbie question #255
Comments
Try with one call to string for each symbol, the example here shows: https://github.com/orgsync/active_interaction#how-do-i-define-an-interaction class CreateUser < ActiveInteraction::Base
hash :user_params, default: {} do
string :first_name
string :last_name
end
def execute
# ...
end
end |
Hashes behave differently than other filters when it comes to defaults. Setting
Here's how they would look: require 'active_interaction' # 1.5.0
class CreateUser < ActiveInteraction::Base
hash :user_params_1, default: nil do
string :first_name, :last_name
end
hash :user_params_2, default: {} do
string :first_name, :last_name, default: ''
end
def execute
inputs
end
end
CreateUser.run!
# => {:user_params_1=>nil, :user_params_2=>{:first_name=>"", :last_name=>""}} |
Tried both the approaches, does not work. My requirement is that |
If that's the case, you don't want a default value for the hash at all. class CreateUser < ActiveInteraction::Base
hash :user_params do
string :first_name, :last_name
end
def execute
inputs
end
end
CreateUser.run!
# ActiveInteraction::InvalidInteractionError: User params is required
CreateUser.run!(user_params: {})
# ActiveInteraction::InvalidInteractionError: User params has an invalid nested value ("first_name" => nil)
CreateUser.run!(user_params: { first_name: 'Jane' })
# ActiveInteraction::InvalidInteractionError: User params has an invalid nested value ("last_name" => nil)
CreateUser.run!(user_params: { first_name: 'Jane', last_name: 'Doe' })
# => {:user_params=>{:first_name=>"Jane", :last_name=>"Doe"}} |
@tfausak Thanks. Yea those errors are a little weird, why not show "is required" message for hash params - first & last name? |
Yea I dont even care about if |
If that's the case, why not make class CreateUser < ActiveInteraction::Base
string :first_name, :last_name
def execute
# ...
end
end If you use it in a controller, you could do this: class UserController < ApplicationController
def create
outcome = CreateUser.run(params[:user])
# ...
end
end |
I could :) but the use case I am dealing with - requires to send a user param hash. Basically I am sending in multiple hashes. |
Are you sure they should be going in as multiple hashes and not a merged hash? |
If they do have to be multiple hashes, I think this is the best we can do in terms of error handling:
|
So our existing business logic is encapsulated inside https://github.com/collectiveidea/interactor (another great project) -- most of our existing interactors accept multiple hashes (user_params, address_params etc) in the interface (and not one merged hash). So lot of existing code is written that way. The fact the user details is nested inside I am looking into the possibility of migrating to |
I don't think we can support your use case. It's not possible in general to lift errors from inside a hash to outside it. (See #257 for why.) The error messages we generate should give you all the information you need to handle this in the controller. class Example < ActiveInteraction::Base
hash :user_params do
string :first_name, :last_name
end
end
outcome = Example.run(user_params: {})
puts outcome.errors.messages[:user_params]
# has an invalid nested value ("first_name" => nil) |
I'm going to close this issue. We can't support your use case. If inputs inside hashes have to keep their errors inside the hash. If they didn't, you'd have the potential for collisions. For example, what do you imagine the errors would look like here? class Example < ActiveInteraction::Base
hash :a do
boolean :x
end
hash :b do
string :x
end
def execute
# ...
end
end |
When I run
CreateUser.run
, I get the following error -ActiveInteraction::InvalidNestedValueError: ActiveInteraction::InvalidNestedValueError
Is this expected behavior? I was hoping to see required validation errors for
first_name
andlast_name
Unfortunately its unclear how Hash filters work.
The text was updated successfully, but these errors were encountered: