Skip to content

Commit

Permalink
Raise on non-existent kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
smcabrera committed Feb 17, 2025
1 parent c633e62 commit 0d2ab86
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
17 changes: 17 additions & 0 deletions lib/active_interaction/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ class << self
include Hashable
include Missable

ALLOWED_KWARGS = %i[
desc
default
index_errors
strip
format
digits
base
from
methods
class
converter
].freeze

# @!method run(inputs = {})
# @note If the interaction inputs are valid and there are no runtime
# errors and execution completed successfully, {#valid?} will always
Expand Down Expand Up @@ -105,6 +119,9 @@ def method_missing(*args, &block)
# @param options [Hash]
def add_filter(klass, name, options, &block)
raise InvalidFilterError, %("#{name}" is a reserved name) if Inputs.reserved?(name)
if (invalid_options = options.keys - ALLOWED_KWARGS).any?
raise InvalidFilterError, "invalid options: #{invalid_options.join(', ')}"
end

initialize_filter(klass.new(name, options, &block))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_interaction/filters/hash_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def convert(value)

# rubocop:disable Style/MissingRespondToMissing
def method_missing(*args, &block)
super(*args) do |klass, names, options|
super do |klass, names, options|
raise InvalidFilterError, 'missing attribute name' if names.empty?

names.each do |name|
Expand Down
8 changes: 8 additions & 0 deletions spec/active_interaction/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ def execute
end.to raise_error NoMethodError
end

it 'raises an error for a non-existent kwarg' do
expect do
Class.new(TestInteraction) do
string :beverage, type: :sparkling_wine, bubbles: true
end
end.to raise_error ActiveInteraction::InvalidFilterError
end

it do
expect do
Class.new(TestInteraction) do
Expand Down
4 changes: 2 additions & 2 deletions spec/active_interaction/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

let(:klass) do
Class.new(ActiveInteraction::Base) do
string :attribute, defualt: nil
array :array, defualt: nil
string :attribute, default: nil
array :array, default: nil

def self.name
@name ||= SecureRandom.hex
Expand Down

0 comments on commit 0d2ab86

Please sign in to comment.