Sha256: 3d791290b7f862d13f97254e9e7ad8bb18388132dac4d433bd1c518040ac0a83

Contents?: true

Size: 1.11 KB

Versions: 15

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Service
    module Plugins
      module HasResultParamsValidations
        module UsingActiveModelValidations
          class Middleware < Core::MethodChainMiddleware
            def next(...)
              return entity.failure(data: errors) if errors.any?

              chain.next(...)
            end

            private

            ##
            # NOTE: errors method (from Active Model Validations) does NOT trigger validations
            # that is why valid? is called here.
            #
            # Check the following link for more info:
            # https://guides.rubyonrails.org/active_record_validations.html#validations-overview-errors
            #
            # Quote (this method - errors):
            # > This method is only useful after validations have been run, because it only inspects the errors collection and does not trigger validations itself.
            #
            def errors
              @errors ||= entity.tap(&:valid?).errors.messages.transform_values(&:first)
            end
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
convenient_service-0.12.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.11.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.10.1 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.10.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.9.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.8.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.7.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.6.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.5.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.4.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.3.1 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.3.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.2.1 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.2.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb
convenient_service-0.1.0 lib/convenient_service/service/plugins/has_result_params_validations/using_active_model_validations/middleware.rb