Sha256: b70ca8b709b6422cae5a5b4dc356602a44f778b2b90eba6d03948e492e51478e

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

module Dry
  module Validation
    class Result
      include Dry::Equalizer(:output, :messages)
      include Enumerable

      attr_reader :output
      attr_reader :errors
      attr_reader :error_compiler
      attr_reader :hint_compiler

      alias_method :to_hash, :output
      alias_method :to_h, :output # for MRI 2.0, remove it when drop support

      EMPTY_MESSAGES = {}.freeze

      def initialize(output, errors, error_compiler, hint_compiler)
        @output = output
        @errors = errors
        @error_compiler = error_compiler
        @hint_compiler = hint_compiler
      end

      def each(&block)
        output.each(&block)
      end

      def [](name)
        output.fetch(name)
      end

      def success?
        errors.empty?
      end

      def failure?
        !success?
      end

      def messages(options = {})
        @messages ||=
          begin
            return EMPTY_MESSAGES if success?
            hints = hint_compiler.with(options).call
            msg_set = error_compiler.with(options).(error_ast).with_hints!(hints)

            as_hash = options.fetch(:as_hash, true)

            if as_hash
              hash = msg_set.to_h
              hash.key?(nil) ? hash.values.flatten : hash
            else
              msg_set
            end
          end
      end

      def to_ast
        [:set, error_ast]
      end

      private

      def error_ast
        errors.map { |error| error.to_ast }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-validation-0.9.2 lib/dry/validation/result.rb
dry-validation-0.9.1 lib/dry/validation/result.rb
dry-validation-0.9.0 lib/dry/validation/result.rb