Sha256: 069a48c1a0ba71da65f823292950cb053af39af2970cc73b6768666ce874e040

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

module Reform
  class Contract < Disposable::Twin
    # a "fake" Dry schema object to add into the @results array
    # super ugly hack required for 2.3.x version since we are creating
    # a new Reform::Errors instance every time we call form.errors
    class CustomError
      def initialize(key, error_text, results)
        @key        = key
        @error_text = error_text
        @errors     = {key => Array(error_text)}
        @messages   = @errors
        @hint       = {}
        @results    = results

        merge!
      end

      attr_reader :messages, :hint

      def success?
        false
      end

      def failure?
        true
      end

      # dry 1.x errors method has 1 kwargs argument
      def errors(**_args)
        @errors
      end

      def merge!
        # to_h required for dry_v 1.x since the errors are Dry object instead of an hash
        @results.map(&:errors)
                .detect { |hash| hash.to_h.key?(@key) }
                .tap { |hash| hash.nil? ? @results << self : hash.to_h[@key] |= Array(@error_text) }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
reform-2.6.2 lib/reform/contract/custom_error.rb
reform-2.6.1 lib/reform/contract/custom_error.rb
reform-2.6.0 lib/reform/contract/custom_error.rb
reform-2.5.0 lib/reform/contract/custom_error.rb
reform-2.3.3 lib/reform/contract/custom_error.rb
reform-2.3.2 lib/reform/contract/custom_error.rb
reform-2.3.1 lib/reform/contract/custom_error.rb
reform-2.3.0.rc2 lib/reform/contract/custom_error.rb