Skip to content

Commit

Permalink
fix unexpected behavior around filters with validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaun Carlson committed May 30, 2024
1 parent 5d3d410 commit e8b0cc9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/active_interaction/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def run_validations!
filter

super if errors.empty?
errors.empty?
end

private
Expand Down
6 changes: 3 additions & 3 deletions lib/active_interaction/modules/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module Validation
class << self
# @param context [Base]
# @param filters [Hash{Symbol => Filter}]
# @param inputs [Inputs]
def validate(context, filters, inputs)
# @param _inputs [Inputs]
def validate(context, filters, _inputs)
filters.each_with_object([]) do |(name, filter), errors|
input = filter.process(inputs[name], context)
input = filter.process(context.send(name), context)

input.errors.each do |error|
errors << [error.name, error.type, error.options]
Expand Down
18 changes: 18 additions & 0 deletions spec/active_interaction/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
float :thing
end

InteractionWithFilterAndValidator = Class.new(TestInteraction) do
string :thing
validates :thing, presence: true
end

InteractionWithDateFilter = Class.new(TestInteraction) do
date :thing
end
Expand Down Expand Up @@ -105,6 +110,19 @@ def execute
expect(interaction.thing).to eql Date.new(year, month, day)
end
end

context 'and a validator' do
let(:described_class) { InteractionWithFilterAndValidator }

it 'returns false with no value given' do
expect(interaction.valid?).to be false
end

it 'returns true when value is given later' do
interaction.thing = 'potato'
expect(interaction).to be_valid
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/active_interaction/modules/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
let(:filter) { ActiveInteraction::Filter.new(:name, {}) }
let(:interaction) do
name = filter.name
klass = Class.new(ActiveInteraction::Base) { attr_writer(name) }
klass = Class.new(ActiveInteraction::Base) { attr_accessor(name) }
klass.filters[name] = filter
klass.new
end
Expand Down

0 comments on commit e8b0cc9

Please sign in to comment.