0

I have a simple Trailblazer Operation as follow:

class User::Delete < Trailblazer::Operation
  extend Contract::DSL

  contract 'params' do
    property :token

    validates :token, presence: true
  end

  step    Contract::Validate(name: 'params'), before: 'operation.new'
  success :kill_zombie

  def kill_zombie(options, params:, **)
    options['killed'] = true
  end

end

When I run the operation under the rails console, that's the output:

irb(main):003:0> User::Delete.()
NoMethodError: undefined method `call' for nil:NilClass
    from (irb):3

If I remove the Contract line:

class User::Delete < Trailblazer::Operation
  extend Contract::DSL

  contract 'params' do
    property :token

    validates :token, presence: true
  end

  success :kill_zombie

  def kill_zombie(options, params:, **)
    options['killed'] = true
  end

end

It works as expected:

irb(main):005:0> User::Delete.()
=> <Result:true <Skill {"killed"=>true} {"params"=>{}} {"pipetree"=>[>operation.new,>kill_zombie], "contract.params.class"=>#<Class:0x0000000003e54e50>}> >
irb(main):006:0>

Any thoughts?

antiqe
  • 1,125
  • 8
  • 17
Jeremie Ges
  • 2,747
  • 3
  • 22
  • 37

2 Answers2

2

You need a Contract::Build before Contract::Validate when using a Reform contract. With a Dry schema, the build step isn't needed.

apotonick
  • 520
  • 2
  • 9
1

I would dig deep in the documentation http://trailblazer.to/gems/operation/2.0/contract.html

The message means that you are trying to call a method on a nil object.

The Object is User::Delete and the method is .()

User::Delete.()

User::Delete could be undefined because it is not passing your validation?

step    Contract::Validate(name: 'params'), before: 'operation.new'

Just posting to give you some input, I don't have a solution. I would inspect User::Delete before calling the method.

User::Delete will be nil and you need to change the validation or make sure that object is correctly instantiated.

Fabrizio Bertoglio
  • 5,890
  • 4
  • 16
  • 57
  • Just dug in the documentation, I feel like I have to use Dry::Validation for linear validations – Jeremie Ges Dec 29 '17 at 09:04
  • @JeremieGes yes something like `(Dry::Validation.Schema do required(:id).filled end)` from http://trailblazer.to/gems/operation/2.0/contract.html#dry-schema – Fabrizio Bertoglio Dec 29 '17 at 09:09