Skip to content

Commit

Permalink
validation: handle ActiveModel::UnknownAttributeError
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandro-fazzi committed Aug 12, 2024
1 parent fac20d2 commit 96b6164
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/i_do_not_need_interactor/contract/active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ module ActiveModel # rubocop:disable Style/Documentation
def self.included(descendant) # rubocop:disable Metrics/MethodLength
class << descendant
def contract(&block)
caller = self
@contract = Class.new do
set_temporary_name("#{caller.inspect.downcase}::contract")
include ::ActiveModel::API
include ::ActiveModel::Attributes
include ::ActiveModel::Validations
Expand All @@ -24,7 +26,7 @@ def self.model_name

def validate(ctx)
self.class.instance_variable_get(:@contract).new(**ctx).validate!
rescue ::ActiveModel::ValidationError => e
rescue ::ActiveModel::ValidationError, ::ActiveModel::UnknownAttributeError => e
ctx.errors << e.message
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/test_i_do_not_need_interactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ def test_contract_active_model_integration
assert_equal ["Validation failed: Test can't be blank"], outcome.errors
end

def test_contract_active_model_integration_with_unknown_attribute
outcome = InteractorWithActiveModelContract.call(foo: 1)

assert_equal ["unknown attribute 'foo' for interactorwithactivemodelcontract::contract."], outcome.errors
end

def test_contract_dry_validation_integration
outcome = InteractorWithDryValidationContract.call

Expand Down

0 comments on commit 96b6164

Please sign in to comment.