Home > Software design >  Validation in ActiveInteraction not working
Validation in ActiveInteraction not working

Time:10-19

My code(for debug):

require 'active_interaction'

class Sleep < ActiveInteraction::Base

  validate :validate_u

  def execute
    puts 1
    1
  end

  private

  def validate_u
    false
  end
end

res = Sleep.run
res.result
res.valid?
res.errors.messages

My result:

  • 2
  • 1
  • true
  • {}

Why is it? I was hoping the result would be:

  • nil
  • false
  • {there are message about faild becous invalid}

CodePudding user response:

use

class Sleep < ActiveInteraction::Base

  validate :validate_u

  def execute
    puts 1
    1
  end

  private

  def validate_u
    errors.add(:base, :invalid) unless 'your validation condition'
  end
end

see doc: https://guides.rubyonrails.org/active_record_validations.html#custom-methods

record invalid when has errors in errors object

  • Related