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

Add validations to constrain the return from execute #275

Closed
ghost opened this issue Apr 23, 2015 · 2 comments
Closed

Add validations to constrain the return from execute #275

ghost opened this issue Apr 23, 2015 · 2 comments

Comments

@ghost
Copy link

ghost commented Apr 23, 2015

It would be very useful to be able to constrain the return type of execute.

Ideally it can be defined in a block like so:

class GetFavToppings < ActiveInteraction::Base

  returns do
     string :topping_name
     integer :weight
  end

  def execute

      prepare_return do |p|
         p.add(:topping_name, 'peppers')
         p.add(:weight, 10)
      end
  end

end
@tfausak
Copy link
Collaborator

tfausak commented Apr 23, 2015

This is interesting! I like this idea. I have a few questions, though:

  1. What should happen if the return value does not match the expected type? I guess adding an error to :base would make the most sense.
  2. Invalid interactions will soon be able to return things too. (See issue Return a result even when validation fails? #168 for some info about that.) Should those return values be type checked?

If you need this functionality now, you should be able to wrap execute with a contract. I am a fan of contracts.ruby, which would look like this:

Contract None => Num
def execute
  42
end

@tfausak tfausak self-assigned this Apr 28, 2015
@tfausak
Copy link
Collaborator

tfausak commented Apr 28, 2015

I am willing to continue discussing this with you, but I do not think that we will add this feature to ActiveInteraction. If you want this behavior, it is possible to use another library (like contracts.ruby) to get it done. ActiveInteraction is mostly concerned with validating inputs. It is common for interactions to have no useful return type at all.

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